iTunes script to tell user songs playing hangs

I’m making a script that tells the user what songs are being listening to over iTunes (either by the user or by others connected to your shared library). Only problem is, I’m getting a runtime error message about variable types that I can’t figure out.

Here’s the code (the error is about 12 lines down):

--hardcode the directory of your iTunes music library to 'dir' 
set dir to "~/Music/iTunes/'iTunes Music'" as string
--take output of all open system files and search for only audio in dir 
set shell_script to "readout= lsof +D" & " " & dir & " | grep [mp3,m4p,m4a]; echo -n $readout;"
set song_list to do shell script shell_script as list
set result to do shell script shell_script as list

if song_list is {} then
	set message to "There are no songs being listened to right now." as string
else
	set song_list_index to 0 as integer
	set message to "Songs people are listening to:" as string
	--THE ERROR MESSAGE POINTS TO 'length' IN THE LINE BELOW 
	repeat while song_list_index is less than or equal to the length of song_list
		--for each list item, trim the .xxx file suffix, then the everything until the file name off the string 
		set suffix to characters (-4) thru end of item song_list_index of song_list as string
		set item song_list_index of song_list to trim_line(item song_list_index of song_list, suffix, 1)
		set song_list to trim_line(item song_list_index of song_list, "/", 0)
		song_list_index = song_list_index + 1
	end repeat
end if

display dialog message & return & song_list buttons {"OK"} default button 1 with icon 1

--lightly modified version of apple's trim subroutine 
on trim_line(song_list, trim_chars, trim_indicator)
	-- 0 = beginning, 1 = end, 2 = both 
	set x to the length of the trim_chars
	-- TRIM BEGINNING 
	if the trim_indicator is 0 then
		repeat while song_list contains trim_chars
			try
				set song_list to characters (x + 1) thru -1 of song_list as string
			on error
				-- the text contains nothing but the trim characters 
				return ""
			end try
		end repeat
	end if
	-- TRIM ENDING 
	if the trim_indicator is in 1 then
		repeat while song_list ends with the trim_chars
			try
				set song_list to characters 1 thru -(x + 1) of song_list as string
			on error
				-- the text contains nothing but the trim characters 
				return ""
			end try
		end repeat
	end if
	return song_list
end trim_line

Here’s the error message I get: Can’t make Can’t make number into a some object. *Note: the Can’t make Can’t make isn’t a typo.

Can someone give me a hand?

Gracias! :smiley:

Some slight modifications work for me:

property music_types : {"mp3", "m4p", "m4a", "aiff"}

try
	my atid(",")
	set music_type_string to music_types as string
	my atid("")
	set itunes_folder to ((path to "cusr" as string) & "Music:iTunes:iTunes Music:")
	set shell_script to "readout= lsof +D " & (quoted form of POSIX path of itunes_folder) & " | grep [" & music_type_string & "]"
	set song_list to paragraphs of (do shell script shell_script)
	repeat with i from 1 to (count of song_list)
		set item i of song_list to (my get_song_name(item i of song_list))
	end repeat
	my atid(return)
	set message to {("Songs people are listening to:" & return), song_list} as string
	my atid("")
on error
	my atid("")
	set message to "There are no songs being listened to right now."
end try
display dialog message buttons {"OK"} default button 1 with icon 1

on get_song_name(the_name)
	my atid("/")
	set the_name to text item -1 of the_name
	my atid("")
	repeat with the_extension in music_types
		if the_name ends with the_extension then
			set the_name to (text 1 thru ((count of the_name) - (count of the_extension) - 1) of the_name)
			exit repeat
		end if
	end repeat
	return the_name
end get_song_name

on atid(the_delim)
	set AppleScript's text item delimiters to the_delim
end atid

Jon

Cool cool. I think I can handle it from here :smiley: