Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tibi08

macrumors 6502a
Original poster
Sep 17, 2007
703
75
Brighton, UK
I have modified the Slimserver iTunes script originally written by Anton F. van der Kraaij in 2004, I thought I should post it in case it's of interest since all the other copies I found through Google no longer work on the current version of iTunes. Enjoy.


Code:
-- 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.
					--return "http://" & server_address & ":" & server_port & "/status.html?p0=stop"
					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 "\\\\"
						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 2
	
end idle
-- end of script
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.