How do I let a user terminate a handler w/o waiting for it to finish?

I would like to use AppleScript Studio to create a script to tell iTunes to reset the finish times of my tracks. I have a big music library and it takes a few minutes for the script to run, so I was hoping to create a progress bar to provide some feedback and a way to cancel/interrupt/break out of the process without waiting for it to finish.

The problem is that when I call the on launch handler, nothing else runs until that script is finished. Is there a way to run scripts, so that I can have other GUI things in my script running and updating as the main script process is doing it’s work? Is there a way for the user to “cancel” a handler?

Essentially I need to start a script to process the iTunes tracks, display a progress bar showing the progress, and allow the user to break out of the process without waiting for it to finish.

Here is what I have so far:


on launched theObject
	tell application "iTunes"
		set theTracks to every track of the front browser window's view
	end tell
	set nTracks to count of theTracks
	set n to 1
	set maximum value of progress indicator "Progress Bar" of window "Progress Window" to nTracks
	set content of progress indicator "Progress Bar" of window "Progress Window" to n
	set content of text field "Progress Text" of window "Progress Window" to "Reset Finish Times:  " & n & " of " & nTracks & " tracks..."
	set indeterminate of progress indicator "Progress Bar" of window "Progress Window" to false
	set visible of window "Progress Window" to true
	
	repeat with aTrack in theTracks
		tell aTrack
			try
				set {start, finish} of aTrack to {0, -1}
			end try
		end tell
		set content of progress indicator "Progress Bar" of window "Progress Window" to n
		set content of text field "Progress Text" of window "Progress Window" to "Reset Finish Times:  " & n & " of " & nTracks & " tracks..."
		update window "Progress Window"
		set n to n + 1
	end repeat
end launched

on clicked theObject
	quit
end clicked

on should quit after last window closed theObject
	return true
end should quit after last window closed

Hi,

as AppleScript is single threaded, you could build up the interface in a different language.
A workaround in AppleScript is to use the idle handler


property nTracks : 0
property counter : 0
property theTracks : {}

on launched theObject
	tell application "iTunes"
		set theTracks to every track of the front browser window's view
	end tell
	set nTracks to count of theTracks
	set counter to 1
	tell window "Progress Window"
		tell progress indicator "Progress Bar"
			set maximum value to nTracks
			set content to counter
			set indeterminate to false
		end tell
		set content of text field "Progress Text" to "Reset Finish Times:  " & counter & " of " & nTracks & " tracks..."
		set visible to true
	end tell
end launched

on idle
	if counter > nTracks then quit
	set oneTrack to item counter of theTracks
	try
		tell application "iTunes" to set {start, finish} of oneTrack to {0, -1}
	end try
	set counter to counter + 1
	tell window "Progress Window"
		set content of progress indicator "Progress Bar" to counter
		set content of text field "Progress Text" to "Reset Finish Times:  " & counter & " of " & nTracks & " tracks..."
		update
	end tell
end idle

on clicked theObject
	quit
end clicked

on should quit after last window closed theObject
	return true
end should quit after last window closed