2008-03-21 Send iTunes Tracks To SqueezeBox

Ok, I finally solved my problem! I mentioned it on 2007-12-02 iTunes vs. Squeezebox –– I like how iTunes lets me quickly collect the stuff I want. But then I can’t play it on my SqueezeBox. Suckage! I tried some suggestions from the Slim Devices forum [1] and I finally figured out what I wanted: I want to select some files and “send them to the Squeezebox” – I don’t want to play the same song in iTunes and on the Squeezebox, for example.

Brad Mohr’s script did exactly what I wanted it to do. Except that it assumed an old version of the server software, and it required me to change a little security setting, or it required me to figure out the “cauth” key. No good! Some looking at documentation that came with the new server, some looking at AppleScript examples elsewhere, some fiddling with netcat (nc), and soon I had something that seems to work.

Yes, netcat is the awesome! :)

Anyway, here it is. To use:

  1. Open the AppleScript editor → Applications → AppleScript → Script Editor.
  2. Paste the code below.
  3. Hit ⌘R to run it. Good for testing.

To install it, you have two options. Here’s how to have a stand-alone program:

  1. Save, pick “Program” as the format to use. I used “Send iTunes Tracks To Squeezebox” as the name.
  2. Select some tracks in iTunes.
  3. Run the program. It will ask you whether you want to “Play” or “Append”.

Or to have it available from the scripts menu in iTunes:

  1. Create the directory ~LibraryiTunes/Scripts
  2. Save, pick “Script” as the format to use. I used “Send To Squeezebox” as the name.
  3. Select the tracks in iTunes.
  4. From the iTunes Script menu, pick “Send To Squeezebox”. It will ask you whether you want to “Play” or “Append”.
  5. ...
  6. Unfortunately, that doesn’t always seem to work, and I haven’t figured out why. :( If you uncomment the display dialog result buttons {"Ok"} at the very end, you’ll see that when run from the Script Editor, everything works as expected, and when run from within iTunes, the result is the empty string. Always.


-- SlimPlay
-- by Alex Schroeder, kensanata@gmail.com
-- March 2008
-- by Brad Mohr, bmohr (AT) seanet (DOT) com
-- 22 January 2003
-- This script is absolutely free and may be used by anyone for any purpose.

-- Known Limitations:
-- Currently only supports a single Squeezebox.
-- Very little error checking

-- edit these properties to match your setup.
property squeezecenter_host : "localhost" -- URL or IP address of the SlimServer
property squeezecenter_port : "9090" -- server port number for the command line interface, default is 9090

tell application "iTunes"
	set these_tracks to the selection of browser window 1
	if these_tracks is {} then error "No tracks are selected in the front window."
	set should_play to false
	display dialog "What would you like to do with these tracks?" buttons {"Play", "Append", "Cancel"} default button 2
	if the button returned of the result is not "Cancel" then
		if the button returned of the result is "Play" then set should_play to true
		set is_first_file to true
		repeat with a_track in these_tracks
			set carbon_filepath to the location of a_track
			set track_title to the name of a_track
			tell application "Finder" to set filepath to URL of carbon_filepath
			set escaped_path to escape_path(filepath) of me
			if should_play then
				if is_first_file then
					playlist_play_track(escaped_path) of me
					set is_first_file to false
					playlist_append_track(escaped_path) of me
				end if
				playlist_append_track(escaped_path) of me
			end if
		end repeat
		if should_play then
			begin_playing() of me
		end if
	end if
end tell

-- apostrophe causes trouble when sent to the shell
on escape_path(this_text)
	set the escaped_text to ""
	repeat with this_char in this_text
		if this_char as text is equal to "'" then
			set the escaped_text to the escaped_text & "%27"
			set the escaped_text to the escaped_text & this_char
		end if
	end repeat
	return the escaped_text
end escape_path

-- append the new track to the playlist
on playlist_append_track(escapedTrackPath)
	send_command("playlist add " & escapedTrackPath)
end playlist_append_track

-- replace the existing playlist with the new track
on playlist_play_track(escapedTrackPath)
	send_command("playlist play " & escapedTrackPath)
end playlist_play_track

-- tell the default (or only) server to begin playing
on begin_playing()
end begin_playing

on send_command(command)
	tell application "Finder"
		set shell_command to "echo -e '" & command & "\\nexit' | /usr/bin/nc " & squeezecenter_host & " " & squeezecenter_port & "; exit 0"
		-- display dialog shell_command buttons {"Ok"}
		set result to do shell script shell_command
		-- display dialog result buttons {"Ok"}
	end tell
end send_command



Please make sure you contribute only your own work, or work licensed under the GNU Free Documentation License. Note: in order to facilitate peer review and fight vandalism, we will store your IP number for a number of days. See Privacy Policy for more information. See Info for text formatting rules. You can edit the comment page if you need to fix typos. You can subscribe to new comments by email without leaving a comment.

To save this page you must answer this question:

Please say HELLO.