Jake.codes

Now Playing with Track URLs, for TextExpander

Sunday, January 27, 2013

-- Now Playing with URL for TextExpander
-- by Jake Bilbrey http://jakebilbrey.com
--
-- Checks iTunes, Rdio, and Spotify to
-- determine the artist and track you
-- are listening to and then prints it.

on isRunning(appName)
    tell application "System Events"
        return (count of (every process whose bundle identifier contains appName))
    end tell
end isRunning

on rdioPlaying()
    if (isRunning("Rdio") > 0) then
        tell application "Rdio"
            if player state is playing then
                set trackName to (get name of current track)
                set artistName to (get artist of current track)
                set trackUrl to (" http://rdio.com" & (get rdio url of current track))
                return (artistName & " - " & trackName & trackUrl) as text
            end if
        end tell
    end if
end rdioPlaying

on iTunesPlaying()
    if (isRunning("iTunes") > 1) then
        -- Assumes you have not disabled iTunes Helper
        tell application "iTunes"
            if player state is playing then
                set trackName to (get name of current track)
                set ispodcast to (get podcast of current track)
                -- Checks to see if you are listening to a podcast
                if ispodcast is true then
                    set artistName to (get album of current track)
                else
                    set artistName to (get artist of current track)
                end if
                return (artistName & " - " & trackName) as text
            end if
        end tell
    end if
end iTunesPlaying

on spotifyPlaying()
    if (isRunning("Spotify") > 0) then
        tell application "Spotify"
            if player state is playing then
                set trackName to (get name of current track)
                set artistName to (get artist of current track)
                set trackId to (get id of current track)
                set origDel to AppleScript's text item delimiters
                set AppleScript's text item delimiters to ":"
                set trackId to third text item of trackId
                set AppleScript's text item delimiters to origDel
                set trackUrl to (" http://open.spotify.com/track/" & trackId)
                return (artistName & " - " & trackName & trackUrl) as text
            end if
        end tell
    end if
end spotifyPlaying

if (count of (iTunesPlaying() as text)) > 0 then
    return (iTunesPlaying() as text)
else if (count of (rdioPlaying() as text)) > 0 then
    return (rdioPlaying() as text)
else if (count of (spotifyPlaying() as text)) > 0 then
    return (spotifyPlaying() as text)
else
    return "silence"
end if