Trouble calling iTunes script outside of iTunes

Hello. I am using a script to play a random album in iTunes. It works fine when selected from the iTunes Scripts menu. I would like to call it from my phone using MouseDown’s EventScripts, but it fails every time. This happens whether I call it from EventScripts, Hazel, or Script Editor. The error lies in the bit of code in the first repeat loop that says:

of (some playlist whose special kind is music)

If I remove it, the script works from everywhere.

Is there a reason why that bit of code would work when the script is run from the iTunes menu and not when the script is run from elsewhere? I’d prefer to keep it in if possible.


"Play Random Album" for iTunes
originally written by Paul Withey
updated by Doug Adams

v3.1 nov 30 '12
-- fixes problem with iTunes 11.0 whereby only first track of playlist would play

v3.0 jan 20 '12
-- maintenance update
-- addresses previous version's bias towards larger albums
-- prevents re-copying of un-track numbered tracks to playlist
-- speed improvement

v2.0 apr 14 '10
-- maintenance release
-- universal binary
-- will include un-track numbered tracks (sort of)
-- selects only a single disc from multi-disc albums

v1.0-1.5 aug 11 '02
-- initial release

Get more free AppleScripts and info on writing your own
at Doug's AppleScripts for iTunes

This program is free software released "as-is"; 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.

Get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

or visit


-- if you like, you can change these:
property randomAlbumName : "[-Some Random Album>"
property minimumTracksRequired : 5

	set spareTracks to {}

	tell application "iTunes"
				set someTrack to some track of (some playlist whose special kind is music) whose album is some item of my getAlbums()
				set theAlbumTracks to tracks whose album is (get album of someTrack) and disc number is (get disc number of someTrack)
				if length of theAlbumTracks ≥ minimumTracksRequired then exit repeat
			end try
		end repeat
			set myRandomPlaylist to user playlist randomAlbumName
			delete every track of myRandomPlaylist
		on error
			set myRandomPlaylist to make new playlist with properties {name:randomAlbumName} -- , shuffle:true}
		end try
		repeat with n from 1 to length of theAlbumTracks
			set chk to false
			repeat with thisTrack in theAlbumTracks
					if track number of thisTrack = n then
						set chk to true
						(my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
						exit repeat
					end if
				end try
			end repeat
			if (chk is false) then set end of spareTracks to thisTrack
		end repeat
		if spareTracks is not {} then
			repeat with thisTrack in spareTracks
				(my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
			end repeat
		end if
		play myRandomPlaylist
			reveal myRandomPlaylist
		end try
	end tell

to copyTrack:aTrack toPlaylist:thePlaylist
	tell application "iTunes"
			if not (exists (some track of thePlaylist whose database ID is (get aTrack's database ID))) then
				duplicate aTrack to thePlaylist
			end if
		on error m
			log m
		end try
	end tell
end copyTrack:toPlaylist:

to getAlbums()
	set theCommand to "perl -e 'local $/=undef;my $s=<>;my @al=();my %seen=();while ($s=~m:<key>Album</key><string>(.*?)</string>:sg){$g = $1;$g=~ s:\\&\\#(\\d*);:chr($1):ge;$g=($g.\"" & (ASCII character 198) & "\");push(@al,$g)unless $seen{$g}++;}print @al;
' " & theDatabase()
	set g to textToList((do shell script theCommand), (ASCII character 198))
	if last item of g is "" then set g to items 1 thru -2 of g
	return g
end getAlbums

on theDatabase()
		set x to (do shell script "perl -e'open(T,\"defaults read iTunesRecentDatabasePaths|\");while(<T>){$t.= $_;}close(T);$t=~m|\"(.*?)\"|s;$t=$1;$t=~s| |\\\\ |g;print $t;'")
		return quoted form of (do shell script "ls " & replaceChars(escapeForUnix(x), "'", "\\'"))
	on error m
		log m
	end try
end theDatabase

on replaceChars(txt, srch, repl)
	set text item delimiters to the srch
	set the item_list to every text item of txt
	set text item delimiters to the repl
	set txt to the item_list as string
	set text item delimiters to ""
	return txt
end replaceChars

on textToList(txt, delim)
	set saveD to text item delimiters
		set text item delimiters to {delim}
		set theList to every text item of txt
	on error errStr number errNum
		set text item delimiters to saveD
		error errStr number errNum
	end try
	set text item delimiters to saveD
	return (theList)
end textToList

to escapeForUnix(n)
	set badlist to "!@#$%^&*()+=-{}[]:;?<>"
	set filled to ""
	repeat with i from 1 to (length of n)
		set t to (text i of n)
		if t is in badlist then
			set filled to (filled & "\\" & t)
			set filled to (filled & t)
		end if
	end repeat
	return filled
end escapeForUnix