Archive

Posts Tagged ‘scripting’

Using Spotify Links in Linux

I’ve yet to officially start my tutorials on Linux, but I’ve been playing around with Spotify recently and I’ve been annoyed by the problem of getting Spotify playlists to load from my web browser directly into Spotify. For those who don’t know about Spotify, I suggest you read my previous post, and if you still want invites, I am getting them on a daily basis, so please apply. Spotify playlists can be shared easily in the form of a link, and there have been several websites set up to publish these links. The problem of using these links in Linux consists of two issues:

  1. Getting the browser to recognize a Spotify link (in the form: spotify:user:jacken:playlist:5CUB76CWjf2vFxZymgO3cW).
  2. Getting the browser to execute the link as an argument on a program running through wine, the Linux windows compatibility layer.

The first problem can be solved quite easily in Firefox, but it does involve messing around with the configuration a bit. Open a new tab, type “about:config” (without the quotes) and press enter. If you are running a recent version of Firefox, you should click the button accepting the “this might void your warranty” message. Right click anywhere on the page (which should have filled with a table of options) and create a new boolean. In the preference name box, type “network.protocol-handler.external.spotify” (again without quotes), and then choose the value true for the option. This has now enabled the spotify protocol in Firefox, meaning that whenever the browser is sent a request to open something with the prefix spotify: it will ask the user what to do with the request. Of course, the 2nd problem is getting Firefox to communicate to a program that does not run natively within Linux. To bypass this, we can create a simple script that Firefox can execute. The script will then do all the complicated bits that Firefox can’t do. Open up a terminal, and execute this command:

cd ~/.wine/drive_c/Program\ Files/Spotify/

If you have installed wine with default settings, this is where all the Spotify files are located. Otherwise, you will have to modify the command to cd you into the correct Program Files directory. Once in the directory, you should find two files, spotify.exe and Uninstall.exe (use the ls command to check). Create a file called “spotify.sh” in this directory, and edit it with your favourite text editor. Copy and paste the following into the file:

1
2
#!/bin/bash
wine "$HOME/.wine/drive_c/Program Files/Spotify/spotify.exe" /uri "$1"

Save the file and exit the text editor. What does this code mean? Well, the first line (known as a sha-bang!) tells the system what type of file our script is, and which interpreter to use when executing it. In this case, we want to run the script through the bash shell, so we give it the location of bash on the system (which is 99.9999% always /bin/bash, but you can find out by typing which bash into a terminal and alter accordingly). The next line is the command we want the script to execute, which calls on wine to run spotify.exe file with the argument /uri and the link url given by Firefox when executing ($1). $HOME is simply a shell variable that stores your home directory location (usually in the form: /home/username). Now all you have to do is make the file executable:

chmod +x spotify.sh

Now try it out on a spotify link! If spotify is already open, the playlist should appear on the left, otherwise spotify will open up and display the playlist for you. When you click on the link, Firefox should ask you what you want to do with it, and all you have to do is select the spotify.sh file we just wrote. If it doesn’t ask when you click on a link, you will need to go into Edit -> Preferences -> Applications and find “spotify” in the list, then select the file using the “use other” action.

Categories: linux, personal Tags: , , , ,
The Atheist Blogger