Broadcast iTunes AirPlay Tracks to Campfire Chat with TrackFire

Broadcast iTunes AirPlay Tracks to Campfire Chat with TrackFire

Update: TrackFire is now on GitHub for your forking pleasure.

TrackFire is an AppleScript "app" that posts iTunes track titles played over an AirPlay device to a Campfire chat room.

In the office we often play music from iTunes over an Airport Express device for the entire floor to hear. Inevitably, someone asks, "What band is this?" Being the avid users of Campfire that we are, we thought it would be perfect if iTunes AirPlay tracks could automatically have their name and album info posted to a Campfire chatroom. And so TrackFire took form.

The AppleScript runs every few seconds checking for a running version of iTunes, then if iTunes is in play mode and broadcasting to a specific AirPlay device, the track information is posted to Campfire.

For the most part, the script runs without issue, but an error is thrown every once in a while. "Can't make «class cFIT» id 10219 of «class cUsP» id 10192 of «class cSrc» id 65 of application 'iTunes' into the expected type." (Still tracking down the cause.) Thanks to the folks over at the Apple Discussion Boards for all their help.

  1. Paste the script into AppleScript Editor replacing the Campfire variables with your own information.
  2. Save as an Application with "Stay Open" checked. Double-click your new script and it will run in the background.
  3. Use iTunes as you normally would and the script does the rest.

(* Begin user defined settings ************)

property campfire_token : "1234567" (* Your Campfire API authentication token *)
property airplay_device : "Apple TV" (* The name of your AirPlay device *)
property campfire_room : "" (* The Campfire room you'd like to post to *)

(* End user defined settings *************)

global current_track, last_track, current_device

on run
	(* init at runtime*)
	set current_track to ""
	set current_device to ""
	set last_track to ""
end run

on idle
	if application "iTunes" is not running then return 10
	tell application "iTunes"
		if (player state is not playing) or (current track is equal to last_track) then return 5

		set last_track to current track

		set minimized of front browser window to false
		set visible of front browser window to true
		set current_device to my getDevice()
		if current_device as string is not equal to airplay_device & " AirPlay" then return 5

		set track_info to my mungeText({name, artist, album} of last_track, "", " :: ")
		set track_info to track_info as string
		set track_info to my mungeText(track_info, "&", "&") -- Replace ampersands
		set track_info to my mungeText(track_info, "\"", """) -- Replace quotation marks
		set track_info to my mungeText(track_info, "'", "'") -- Replace apostrophes

		set shellCommand to ("curl -u " & campfire_token & ":X -H 'Content-Type: application/xml' -d 'TextMessage" & track_info & "' " & campfire_room)
		set shellCommand to shellCommand as string
		do shell script shellCommand
		(*display dialog shellCommand*)
		(*log "Posting to Campfire:" & shellCommand*)
		return 5
	end tell
end idle

on getDevice()
	tell application "System Events"
		tell process "iTunes"
			return description of button 8 of window "iTunes"
		end tell
	end tell
end getDevice

on mungeText(itxt, stxt, rtxt)
	set tid to AppleScript's text item delimiters
	if class of itxt is text then
		set AppleScript's text item delimiters to stxt
		set itxt to text items of itxt
	end if
	set AppleScript's text item delimiters to rtxt
	set otxt to itxt as text
	set AppleScript's text item delimiters to tid
	return otxt
end mungeText