New to AppleScript, Code is Driving me Crazy

I am trying to develop some QS/iTunes script and am totally new to scripting. I can’t see why in the following script, I can get to the “itunes is running” debug, but not to the “you are here” debug line. The script seems to stop at that point.

I’ve tested all the code in the player state section externally and it works fine.

Any ideas or help for someone new to the mac?


global iTunes_is_running

using terms from application "Quicksilver"
	on process text qsTermsS
		tell application "System Events"
			set running_apps to (get name of processes)
		end tell
		if running_apps contains "iTunes" then
			set iTunes_is_running to true
		end if
		if iTunes_is_running then
			debug("itunes is running")
			tell application "iTunes"
				debug("you are here")
				if qsTermsS is "" then
					activate
					display dialog "No text entered. Please enter one or more tags and try again." buttons {"OK"} default button 1
				else
					if player state is playing then
						set theTrack to current track
						set theCommentS to theTrack's comment
						set theCommentL to String2List(theCommentS, " ")
						set theCommentL to ManageTags(theCommentL, "¢", "filter")
						set theCommentL to theCommentL & String2List(qsTermsS, " ")
						set theCommentL to ManageTags(theCommentL, "¢", "create")
						Qsort(theCommentL, 1, -1)
						set theTrack's comment to change_case_of(List2String(theCommentL, " "), "lower")
					end if
				end if
			end tell
		end if
	end process text
end using terms from

-- Functions

on debug(message)
	activate
	display dialog message as text
end debug

on ManageTags(iList, delimiter, action)
	set oList to {}
	repeat with i from 1 to length of iList
		set theItem to item i of iList
		if character 1 of theItem is delimiter then
			set end of oList to theItem
		else if action is "create" then
			set end of oList to "¢" & theItem
		end if
	end repeat
	return oList
end ManageTags

on PushTid(delimiter)
	-- Treats the Text Item Delimiters as a stack
	set beginning of text item delimiters to delimiter
end PushTid

on PopTid()
	-- Treats the Text Item Delimiters as a stack
	set text item delimiters to rest of text item delimiters
end PopTid

on List2String(iList, delimiter)
	PushTid(delimiter)
	set oString to iList as text
	PopTid()
	return oString
end List2String

on String2List(iString, delimiter)
	PushTid(" ")
	set oList to iString's text items
	PopTid()
	return oList
end String2List

on change_case_of(this_text, this_case)
	(*
Change Case of Item Names
Copyright 2001 Apple Computer, Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
	if this_case is "lower" then
		set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		set the source_string to "abcdefghijklmnopqrstuvwxyz"
	else
		set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
		set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	end if
	set the new_text to ""
	repeat with thisChar in this_text
		set x to the offset of thisChar in the comparison_string
		if x is not 0 then
			set the new_text to (the new_text & character x of the source_string) as string
		else
			set the new_text to (the new_text & thisChar) as string
		end if
	end repeat
	return the new_text
end change_case_of
on Qsort(theList, L, r)
	script o
		property cutoff : 10
		property p : theList
		
		on qsrt(L, r)
			set i to L
			set j to r
			set v to my p's item ((L + r) div 2)
			
			repeat while (j > i)
				set u to my p's item i
				repeat while (u < v)
					set i to i + 1
					set u to my p's item i
				end repeat
				
				set w to my p's item j
				repeat while (w > v)
					set j to j - 1
					set w to my p's item j
				end repeat
				
				if (i > j) then
				else
					set my p's item i to w
					set my p's item j to u
					set i to i + 1
					set j to j - 1
				end if
			end repeat
			
			if (j - L < cutoff) then
			else
				qsrt(L, j)
			end if
			
			if (r - i < cutoff) then
			else
				qsrt(i, r)
			end if
		end qsrt
		
		on isrt(L, r)
			set x to L
			set z to L + cutoff - 1
			if (z > r) then set z to r
			
			set v to my p's item x
			repeat with y from (x + 1) to z
				if (my p's item y < v) then
					set x to y
					set v to my p's item y
				end if
			end repeat
			
			tell my p's item L
				set my p's item L to v
				set my p's item x to it
			end tell
			
			set u to my p's item (L + 1)
			repeat with i from (L + 2) to r
				set v to my p's item i
				if (v < u) then
					set my p's item i to u
					repeat with j from (i - 2) to L by -1
						if (v < my p's item j) then
							set my p's item (j + 1) to my p's item j
						else
							set my p's item (j + 1) to v
							exit repeat
						end if
					end repeat
				else
					set u to v
				end if
			end repeat
		end isrt
	end script
	
	set listLen to (count theList)
	if (listLen > 1) then -- otherwise the handler will error
		-- Translate negative indices
		if (L < 0) then set L to listLen + L + 1
		if (r < 0) then set r to listLen + r + 1
		
		if (r = L) then
			-- No point in sorting just one item
		else
			-- Transpose transposed indices
			if (L > r) then
				set temp to L
				set L to r
				set r to temp
			end if
			
			if (r - L < o's cutoff) then
				-- Skip the Quicksort if cutoff or less items
			else
				o's qsrt(L, r)
			end if
			o's isrt(L, r)
		end if
	end if
	
	return -- nothing
end Qsort


Hi,

no application knows, what a handler is, a handler call belongs to AppleScript itself,
therefore within an application tell block you must reference AppleScript with the my keyword, e.g

my debug("you are here")

PS: the change_case_of() handler can be replaced with


on change_case_of(this_text, this_case)
    return (do shell script "echo " & quoted form of this_text & " | tr " & item (((this_case is "lower") as integer) + 1) of {"a-z A-Z", "A-Z a-z"})
end change_case_of

Thanks!

That was it. Makes perfect sense. I suppose I should RTFM before starting coding, but ya know…

:slight_smile:

I recommend to debug code never in application action handlers,
because in most of the cases the script fails silently without any error message when an error occurs.

Comment out the action handler and simulate the action parameter variables with predefined ones.
I don’t use QuickSilver, but for example a Mail.app action handler

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		.
	end perform mail action with messages
end using terms from

can be easily debugged with

-- using terms from application "Mail"
	-- on perform mail action with messages theMessages for rule theRule
		tell applicaton "Mail" to set theMessages to selection
		.
	-- end perform mail action with messages
-- end using terms from