Send your current iTunes track to Slack

July 18 2016

This is version two of a script I built a while ago that sends what you're listening to in iTunes to a Slack outgoing webhook. It was working well until Apple released iTunes 12 which broke the hooks I was using to get the current track of iTunes. Looks like Apple has fixed this as of at least iTunes 12.4 (perhaps earlier).

The original version of this script used Applescript and JSON Helper. This new one still uses those, but I also decided to use Python as well since writing out the curl command with the proper JSON formatting in Applescript was difficult. I've rebuilt it with nicer formatting. When you're done, this is what it'll look like:

Screenshot of the output from the scripts

To get this working you'll need:

Once you have your tools together, create a new folder wherever you like for the two bits to this.

The first part of this is the sender. It builds and sends the message to Slack from some url arguments we'll send to it from the Applescript. Let's call it sendToSlack.py

#!/usr/bin/python

import requests, sys

songTitle = sys.argv[1]
albumArtist = sys.argv[2]
msgStr = "{} by {}".format(songTitle, albumArtist)

payload = {
    "attachments": [
        {
            "fallback": msgStr,
            "color": "#181819",
            "fields": [
                {
                    "title": "Artist",
                    "value": albumArtist,
                    "short": True
                },
                {
                    "title": "Track",
                    "value": songTitle,
                    "short": True
                }
            ],
            "image_url": sys.argv[3]
        }
    ]
}

requests.post(
    url="YOUR SLACK HOOK URL HERE",
    headers={"Content-Type":"application/json"},
    json=payload
).content

The second bit of this is the Applescript which is what we'll call which will first get information about the currently playing track in iTunes, fetch the artwork, and then send all of the data to our sender script for dispatch. In the same folder as sendToSlack.py create an ApplesScript with the contents:

tell application "iTunes"
    set albumTitle to the album of the current track
    set songTitle to the name of the current track
    set albumArtist to the artist of the current track

    set queryStr to albumTitle & " " & albumArtist

    tell application "JSON Helper"
        set albumArtDetails to fetch JSON from "https://itunesartwork.dodoapps.io/?query=" & queryStr & "&entity=album&country=us"
        set artURL to |url| of item 1 of albumArtDetails

        set msgStr to "Listening to: *" & songTitle & "* by *" & albumArtist

        do shell script "/usr/local/bin/python ~/Documents/Development/Other/applescripts/sendToSlack.py \"" & songTitle & "\" \"" & albumArtist & "\" \"" & artURL & "\""
    end tell
end tell

You'll have to modify the location in the script of where you put your python file, but other than that things should just work! Play a song in iTunes and try it out. Let me know on Twitter if you get it working.