Announcements

Help Wizard

Step 1

NEXT STEP

Commands to play songs and playlists from terminal

Solved!

Commands to play songs and playlists from terminal

Hello,

so I want to wake up listening to some music and I've found an app that lets me execute a command at a given time. I want that app to start spotify and automatically play a song or a playlist but I can't figure out a command to do that from the terminal (so that I can paste it on my app).

I've tried spotify --playpause but it doesn't play anything. Typing spotify "playlist URI" seems to open spotify on the given playlist but I don't know how to make it start. Do you know any way to operate Spotify via terminal? What are the commands and syntax?

Any help is very appreciated.

Reply

Accepted Solutions
Marked as solution


@Fraxav wrote:

Thank you olejon, both your command and the script worked great. I wonder if the result of the script (open spotify and play URI) could be obtained with a single command from terminal


So you have two options. Either create and save the script I mentioned, and then run that script, or use a single command that combines all of them.

 

Your example won't work, because the two last commands won't run before you quit Spotify. Adding & puts Spotify in the background, while 1>/dev/null 2>&1 ignores any output. We must use paranthesis to daemonize (put in background) when combining multiple commands. && is used to run a command after the previous succeeds. So & and && are two completely different things.

 

This should work:

 

(spotify 1>/dev/null 2>&1 &) && sleep 3 && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

 Remember, all on one line.

 

Depending on how long time Spotify uses to cold start (reboot to check), you can increase or decrease the sleep time (in seconds).

 


The shell is very powerful. You can run a second command if the first one fails also, like this:

 

command1 || command2

 Or run the second regardless:

 

command1; command2

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

View solution in original post

19 Replies

I don't believe there is a way to operate Spotify from the terminal... I think there is some sort of command that can be sent to it (Toastify must use it), but I'm unsure as to what it is.

 

Actually, thinking about it, I'm sure I remember seeing a post about this somewhere. I'll see if I can dig it out (or work out the commands myself).

 

Liam

This is indeed possible on Linux. You can start playing any URI by running this command, with Spotify already running:

 

qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri URI

 Replace that last URI with the actual URI. It should be on one line.

 

Example script to start Spotify and start playing an album:

 

#!/bin/bash

spotify 1>/dev/null 2>&1 &

sleep 3

qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

Thank you olejon, both your command and the script worked great. I wonder if the result of the script (open spotify and play URI) could be obtained with a single command from terminal, i.e:

spotify && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri URI

 I tried all the connectors I could find but none of them worked. I'm not good with commands...

Marked as solution


@Fraxav wrote:

Thank you olejon, both your command and the script worked great. I wonder if the result of the script (open spotify and play URI) could be obtained with a single command from terminal


So you have two options. Either create and save the script I mentioned, and then run that script, or use a single command that combines all of them.

 

Your example won't work, because the two last commands won't run before you quit Spotify. Adding & puts Spotify in the background, while 1>/dev/null 2>&1 ignores any output. We must use paranthesis to daemonize (put in background) when combining multiple commands. && is used to run a command after the previous succeeds. So & and && are two completely different things.

 

This should work:

 

(spotify 1>/dev/null 2>&1 &) && sleep 3 && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

 Remember, all on one line.

 

Depending on how long time Spotify uses to cold start (reboot to check), you can increase or decrease the sleep time (in seconds).

 


The shell is very powerful. You can run a second command if the first one fails also, like this:

 

command1 || command2

 Or run the second regardless:

 

command1; command2

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

And it works indeed. I had to increase the sleep time a little to play my playlist, but for some reason 3 was enough when playing your album. Anyway, that's exactly what I needed. Thanks so much for your help!

You're welcome 🙂

 

If this is for playing music when you wake up, setting the sleep to a high value, like 10, can be a good idea, to make sure Spotify is up and running and ready to receive commands. One could of course create a script that loops to check if D-Bus is ready, and then play the URI, but that is not necessary in this case, just complicated.

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

Yes 10 seems to work fine right now. I'm going to test the whole thing on startup just to make sure.

On a side note, it seems that the playlist I'm playing always starts (of course) with the first song. I was wondering if I could add one of the spotify commands ('next') to make it skip to the next song (which would be a random one since I have shuffling enabled), and I found this:

 

org.mpris.MediaPlayer2.Player.Next() -> ()

 

I tried to add that at the end of your magic formula but it didn't work (didn't even start playing). Do you mind a last tip about this one?


I tried to add that at the end of your magic formula but it didn't work (didn't even start playing). Do you mind a last tip about this one?


This should do it:

 

(spotify 1>/dev/null 2>&1 &) && sleep 3 && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.Next

 You may have to put a sleep between the OpenUri and Next. You'll see. For me the above works.

 

Replace the album URI with your playlist URI.

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

It works for me too, without the sleep. I was trying to do it without all the necessary code, of course...

Thank you again!

Try on mac osascript -e 'tell application "spotify" to play'

@RadioactiveRadz, Do you know if this can take parameters for a playlist or song name?

Deleted

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

Mac

For me, the command given by olejon worked on my Debian (Jessie) machine but not on one running Fedora 23. For the Fedora computer, here's what worked:

 

qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri spotify:track:1WNPappMd13lY5o9POZ4gU

Weirdly enough this works for me for individual tracks for the Fedora solution, but as soon as I attempt to use for an album or playlist it just opens the respective playlist/album and doesn't start playing ala the original OP's problem.

 

Any use of PlayPause just starts the last track that was waiting on Spotify when I opened it. Anyone any idea why this would be?


@pictomtr wrote:

Weirdly enough this works for me for individual tracks for the Fedora solution, but as soon as I attempt to use for an album or playlist it just opens the respective playlist/album and doesn't start playing ala the original OP's problem.

 

Any use of PlayPause just starts the last track that was waiting on Spotify when I opened it. Anyone any idea why this would be?


 

If you are using the new v 1.0 this is a bug in the D-Bus MPRIS implementation. In fact there are several, like it is now not possible to get PlaybackStatus. No response from Spotify yet. The behaviour has been the same from at least v 0.6 to v 0.9, so why the have broken it now I don't know.

 

Would be nice if some of the Spotify Linux devs could look at this, please!

 

You can downgrade to v 0.9, which shouldn't make you lose any important features. Check out this guide:

 

Remember to read the section below the guide link ("If you have already installed Spotify Beta for Linux...").

 

https://github.com/olejon/spotcommander/wiki/InstallOnFedora#install-the-latest-spotify-stable-versi...

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

have you been learning to code since kindergarten? How can you code so well, thats awsome

That URL command doesn't work.

 

To open an URL: 

qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

Suggested posts