Launch any playlist, speakable script, need little help

I got excited about applescript after realising that i can write scripts to be used with speech recognition allowing me to control my computer with voice.

I came up with a simple yet useful script which allows me to launch a playlist in iTunes with a spoken command. The idea is that the script refers to its filename so when named “play Black Sabbath” the script would tell iTunes to play a playlist called “Black Sabbath” A same script with a filename like “gimme Beatles” would play a playlist “Beatles.” When placed in the Speakable items folder this script can be launched with a spoken command.

The script works most of the time but it’s not quite finished yet since I don’t know how to properly refer to a filename of a script.

Please check the comments on the script.


on run {}

 (* --I was using this when I started working with the script in Script Editor
tell application "Script Editor"
set aname to name of front document of me as string --get the name of a script in script editor
end tell
*)
	
	tell application "System Events"

(*
so, this is the part I haven't figured out yet, shouldn't be too hard for someone who knows his way around applescript. Although this works most of the time, it is obvious that the script is not always the last process that has been launched. Could someone who knows applescript a bit better help me on this so i can finish the script. How to refer to a filename of this script so that it really works?
*)
		
		set aname to name of the last process as string --get the name of this script, not foolproof


		
	end tell
		
	set n to (number of word 1 in aname) + 2 --ignore 1st word
	
	set aplaylist to ""
	
	repeat with i from n to (number of characters in aname)
		
		set aplaylist to aplaylist & character i of aname
		
	end repeat
	
	playsome(aplaylist)
		
end run

on playsome(aplaylist)
	
	tell application "iTunes"
		
		if exists playlist aplaylist then
			
			set theTrack to some track of playlist aplaylist
			
			if the location of theTrack is missing value then
				
				my playsome(aplaylist) --if the track is missing try again
				
			else
				
				play theTrack
				set view of browser window 1 to playlist aplaylist --view the playlist
				activate
				
			end if
			
		else --playlist doesn't exist
			
			display dialog "Cannot find a playlist called " & "'" & the aplaylist & "'" buttons {"OK"} default button 1
			
			
		end if
		
	end tell
	
end playsome

So, I’m almost there, few lines away from a properly working script. Can anyone help?

The only way I can figure is saving the script as application, then:

display dialog name of (info for (path to me))

Otherwise, a simple compiled script will be run by SpeakableItems.app, and the name of “me” will be “SpeakableItems.app”. If this is too slow (app launches everytime), you may consider keep the compiled scripts and hardcode the playlist name inside each one.

Works fine, thanks.

I don’t want to hardcode anyhthing because it would compromise the whole idea of the script being easily configurable just by changing the file name.

Would be somehow possible to use aliases whit different names to launch the sript?