As part of my job I have to switch networks a lot. One of these networks requires Spotify to run through an HTTP proxy. To change proxy settings on Spotify requires that you head into the settings, then the advanced settings, select to put the proxy on or off and then restart the app. This gives me a headache.
Where To Begin
Through searching the Spotify community forums, I managed to find my way to a golden ticket, in the form of a prefs
file located at: ~/Library/Application\ Support/Spotify/prefs
. This is where your Spotify preferences are stored.
Once your HTTP Proxy settings are set up inside the Spotify GUI Preferences, switching it on and off is relatively simple. Back in prefs
the line network.proxy.mode=1
is controlling the value of the dropdown (0 = Auto-detect, 1 = No Proxy, 2 = HTTP). So what we want is a script that quits Spotify, finds that line, alters it, and relaunches Spotify.
Bash To The Rescue
At the core of the script is this:
pkill Spotify
sed -i -e "s/network.proxy.mode=2/network.proxy.mode=1/" ~/Library/Application\ Support/Spotify/prefs
open -a Spotify
This kills the process called Spotify, makes the change to the prefs file using sed
to do an in place edit (-i
-e
) where it looks for the proxy mode and replaces the number with our target. Then you can use open
with a -a
to open the Spotify app.
As I wanted to pull this into the commands that I use to switch networks, I set this up as an alias in .bash_profile
. It worked! But it meant that Spotify would open every time I switched networks, regardless of whether it was open to begin with.
I could live with this, and I have Spotify open most of the time anyway, but thought I might as well make the solution a touch more elegant and learn a bit more bash in the process.
After some quick Googling, the simplest (but not infallible) solution was using pgrep
to check if Spotify is a running process. Wrap it all up in an if / then
statement and we’re good to go!
Solution
To turn the proxy on:
if pgrep -x "Spotify" > /dev/null
then
echo 'Spotify is running. Restarting.'
pkill Spotify
sed -i -e "s/network.proxy.mode=1/network.proxy.mode=2/" ~/Library/Application\ Support/Spotify/prefs
open -a Spotify
else
echo 'Spotify is not running.'
fi
And off again:
if pgrep -x "Spotify" > /dev/null
then
echo 'Spotify is running. Restarting.'
pkill Spotify
sed -i -e "s/network.proxy.mode=2/network.proxy.mode=1/" ~/Library/Application\ Support/Spotify/prefs
open -a Spotify
else
echo 'Spotify is not running.'
fi
As aliases:
alias sproxon='
if pgrep -x "Spotify" > /dev/null
then
echo "Spotify is running. Restarting."
spotify_kill; spotify_proxy_on && spotify_open
else
echo "Spotify is not running."
fi'alias sproxoff='
if pgrep -x "Spotify" > /dev/null
then
echo "Spotify is running. Restarting."
spotify_kill; spotify_proxy_off && spotify_open
else
echo "Spotify is not running."
fi'alias spotify_kill='pkill Spotify'
alias spotify_open='open -a Spotify'alias spotify_proxy_on='sed -i -e "s/network.proxy.mode=1/network.proxy.mode=2/" ~/Library/Application\ Support/Spotify/prefs'alias spotify_proxy_off='sed -i -e "s/network.proxy.mode=2/network.proxy.mode=1/" ~/Library/Application\ Support/Spotify/prefs'
Bonus
The on/off commands can be saved as .command
files, then (once made executable) can be run from Spotlight as a more user friendly/command line-phobic alternative.
Shortcomings
At the time of writing I’m a Junior Dev and still pretty new to bash. Stackoverflow/Superuser taught me almost everything to write the above. What I learned is that:
- Using
pgrep
to determine if a process called “Spotify” is running is not great. There could, for some reason, be an alternative process with the same name, which you quit instead. - Using
pkill
to exit the application isn’t necessarily graceful. I began by usingosascript
to quit the application, but this was very slow and meant usingsleep
to wait for Spotify to finish closing & saving. - Syntax. I still feel like this isn’t written as elegantly as possible.
If / then
inside an alias doesn’t feel right to me. In addition, I’m pretty sure thatsed
is smart enough to turn these two separate commands into one single command. This would make the starting point of the Spotify GUI Proxy Selection irrelevant.
Any pointers and improvements the community can give, I’d be really grateful to receive, and happy network switching to one and all.