Skip to content

Instantly share code, notes, and snippets.

@webdevbrian
Last active May 1, 2019 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevbrian/858f040e6d845e78296aec0ccfc2beef to your computer and use it in GitHub Desktop.
Save webdevbrian/858f040e6d845e78296aec0ccfc2beef to your computer and use it in GitHub Desktop.
BK's Script for setting currently playing song from spotify in slack

You can have this run automatically on your mac using the native launchd system if you want ...(see the spotify_song.sh file below). Or just start it manually to share what you're listening to. 🤷🏼‍♂️

Drop the following PLIST into ~/Library/LaunchAgents/com.user.slack-spotify.plist:

The filename must match the string under Label, replace /PATH/TO/SCRIPT.sh with an actual path to the .sh script above.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.user.slack-spotify</string>
        <key>ProgramArguments</key>
        <array><string>/PATH/TO/SCRIPT.sh</string></array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>60</integer>
    </dict>
</plist>

Since this also runs on its own timer, remove the following 3 lines from the .sh script:

while true; do
    sleep 60
done

Register this daemon with launchd by running

launchctl load ~/Library/LaunchAgents/com.user.slack-spotify.plist

start it by either logging out and back in or running

launchctl start com.user.slack-spotify

#!/bin/bash
# Get it from here : https://api.slack.com/custom-integrations/legacy-tokens
APIKEY="XXXXXX"
trap onexit INT
function reset() {
echo 'Resetting status'
TEXT='Eating%20healthy%20foods'
EMOJI='eggplant'
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22"$TEXT"%22%2C%22status_emoji%22%3A%22%3A"$EMOJI"%3A%22%7D" > /dev/null
}
function onexit() {
echo 'Exiting'
reset
exit
}
while true; do
state=$(osascript -e 'tell application "Spotify" to player state')
date
echo "Spotify: "$state
if [[ "$state" != "playing" ]]; then
reset
else
SONG=$(osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track')
URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
echo $SONG
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22"$URLSONG"%22%2C%22status_emoji%22%3A%22%3Aheadphones%3A%22%7D" > /dev/null
fi
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment