Control Your Slim Server via iTunes

I’ve used my SliMP3 for quite a while now and have enjoyed streaming music on my home network as well as remotely since even before I purchased my player. The typical manner to control what plays has been through the hardware remote, the software player (now built in) or via the Web UI. Things just got very interesting and you can now control what plays on your slim players by choosing the easiest method I can think of – iTunes.

A guy named Anton F. van der Kraaij wrote this AppleScript which when saved as an application and left to run, will queue the same selection on your hardware.

Here’s the script – Thanks Anton!


-- Begin of Script
-- iSlimServer: Control SlimServer from iTunes
-- Plays whatever song is currently playing in iTunes on the SlimServer
-- Copyright Anton F. van der Kraaij 2004
-- Thanks to a script by Barry Brown for inspiration and help from Oscar Marsch
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- INSTRUCTIONS:
-- Set your variables server_address and server_port to your situation
-- Save this script as an application, making sure to click 'stay open'

global server_address, server_port, current_file_track

set server_address to "127.0.0.1" -- This is the slimserver's address
set server_port to "9000" -- This is the slimserver's port

set current_file_track to ""

on idle
try
tell application "Finder"
if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
-- iTunes is running. Check if it is playing a song.
tell application "iTunes"
if player state is paused or player state is stopped then
-- iTunes is not playing. Stop slimserver.
do shell script ("curl "http://" & server_address & ":" & server_port & "/status.html?p0=stop"")
set okflag to false
return 10
end if
end tell
end if
if okflag then
tell application "iTunes"
if class of current track is file track then
try
copy current track's location to file_path_of_track
on error errText
display dialog errText buttons {"Cancel"} with icon 0
end try
end if -- not a file track
-- return file_path_of_track
if (current_file_track is not file_path_of_track) then
-- File in iTunes is different than file on SlimServer.
-- Thus update variable
set current_file_track to file_path_of_track

-- convert mac path to unix path for use in URL with curl (be careful with special characters here)
set mac_path to (file_path_of_track as text)
set root to (offset of ":" in mac_path)
set rootdisk to (characters 1 thru (root - 1) of mac_path)
tell application "Finder"
if (disk (rootdisk as string) is the startup disk) then
set unixpath to "%2f" & (characters (root + 1) thru end of mac_path)
else
set unixpath to "%2fVolumes:" & mac_path
end if
end tell
set chars to every character of unixpath
repeat with i from 2 to length of chars
if item i of chars as text is equal to "/" then
set item i of chars to ":"
else if item i of chars as text is equal to ":" then
set item i of chars to "%2f"
else if item i of chars as text is equal to """ then
set item i of chars to "" & """
else if item i of chars as text is equal to "*" then
set item i of chars to "\*"
else if item i of chars as text is equal to "?" then
set item i of chars to "\?"
else if item i of chars as text is equal to " " then
set item i of chars to "%20"
else if item i of chars as text is equal to "+" then
set item i of chars to "%2b"
else if item i of chars as text is equal to "&" then
set item i of chars to "%26"
else if item i of chars as text is equal to "" then
set item i of chars to ""
end if
end repeat
set filetoplay to every item of chars as string
-- end conversion
-- Tell slimserver to stop current song and play the song currently playing in iTunes
do shell script ("curl "http://" & server_address & ":" & server_port & "/status.html?p0=stop"")
do shell script ("curl "http://" & server_address & ":" & server_port & "/status.html?p0=playlist&p1=play&p2=" & filetoplay & """)
end if
end tell
end if
end try
return 10
end idle
-- end of script

3 comments for “Control Your Slim Server via iTunes

  1. 10/4/2004 at 5:50 am

    I am glad to see you enjoyed the script! Hope it works well on your setup. 🙂

    I hope somebody will come up with a better way: I would like to catch the apple events that iTunes is sending somehow and then “send” these events to the slimserver. Don’t know how to catch them though, so help will be appreciated! Such an improved script will be cleaner I think than the current one, which works well if the delay between iTunes play and slimserver play is not too large.

    Greetings, Anton.

  2. 10/4/2004 at 6:42 pm

    So far so good… Love it. It’s the functionality of AirTunes via Slim…

  3. 1/12/2008 at 4:56 am

    Hmm. Doesn’t work for me. Applescript doesn’t seem to like nested doublequotes such as “””

    Anyone with a solution that works?

    note: I have also found that CLUTTER is supposed to integrate with iTunes and SlimServer (or SqueezeCenter as it is now called).

Leave a Reply

%d bloggers like this: