I "just" need a cancel button. darn it.

I have mentioned this in another thread, but I think it gets its own, since I’m still clueless on it:

I have a little applescript loop that iterates through some procedures on every track in my iTunes library. It works.

It also takes about 60 seconds…and longer if people have more tracks than I. Therefore, I want a cancel button so I can stop the thing at my whim. Problem is, the applescript loop just holds everything up, so the cancel button cant be clicked until it’s done anyway: utterly useless.

This thread has some ideas, but I can’t make them work:

http://bbs.applescript.net/viewtopic.php?p=31635#31635

I think I may want to run the loop in a separate thread. How do I do that with Applescript? Or even can it be done? A cancel button seemed so simple…

THANKS!

I’m not sure if this will help you, but if you use the “Apple + .” keys – that will stop a script dead in it’s tracks. (The "Apple + . " is also known as the “command key & the period key”. So just hold down the “Apple” key and then hit the “Period” key.)

Take a look at this topic for some information that may help:

http://bbs.applescript.net/viewtopic.php?t=5711

Jon

Hey, thanks for the tip. That thread addresses exactly what I needed, and the example app seems to work properly. Nonetheless, I can’t seem to make it work within mine. Can someone else take a look at this and try to implement it themselves…and hopefully come up with a tip on what I might still be doing wrong? I’d really appreciate it.

Can you post some source code? That will make trying to figure out what’s going wrong a bit easier.

Jon

Everything here works beautifully, including the progress indicator (which I hadn’t even started working on until I looked at the script in the above posted thread). Only problem: can’t click Cancel.


------------------------------------------------script properties

property helpOpen : false
property tuning : false
property tuneupCanceled : false

------------------------------------------------interface handlers

on idle
	if tuning is equal to true then
		set tuning to false
		runTuneups()
	end if
	return 1
end idle

on choose menu item theObject
	tell window "main"
		if helpOpen is equal to false then
			tell drawer "HelpDrawer" to open drawer on right edge
			set helpOpen to true
		else
			tell drawer "HelpDrawer" to close drawer
			set helpOpen to false
		end if
	end tell
end choose menu item

on clicked theButton
	if theButton is equal to button "Start" of window "main" then
		load nib "Confirmation"
		set confirmPanel to window "ConfirmationPanel"
		display panel confirmPanel attached to window "main"
	else if theButton is equal to button "Cancel" of window "Progress" then
		set tuneupCanceled to true
		show window "main"
		hide window "Progress"
	end if
end clicked

on panel ended thePanel with result theResult
	close panel thePanel
	if theResult is equal to 1 then
		hide window "main"
		show window "Progress"
		my progressInit()
		set tuning to true
	end if
end panel ended

------------------------------------------------progress bar display

on progressInit()
	tell progress indicator "ProgressBar" of window "Progress"
		set indeterminate to true
		set uses threaded animation to true
		set content to 0
		start
	end tell
end progressInit

on progressMakeDeterminate(nIterations)
	tell progress indicator "ProgressBar" of window "Progress"
		stop
		set indeterminate to false
		set maximum value to nIterations
	end tell
end progressMakeDeterminate

on progressUpdate(iteration)
	tell progress indicator "ProgressBar" of window "Progress"
		set content to iteration
	end tell
end progressUpdate

on progressTerminate()
	tell progress indicator "ProgressBar" of window "Progress"
		set content to 0
		stop
	end tell
	update window "Progress"
	hide window "Progress"
end progressTerminate

------------------------------------------------main tune-up loop

on runTuneups()
	tell application "iTunes"
		
		set root to null -- initialize binary tree
		set lib to library playlist 1 -- main library
		set n to count file tracks of lib -- number of songs.
		
		my progressMakeDeterminate(n)
				
		try
			repeat with i from 1 to n
				if tuneupCanceled is equal to true then
					my progressTerminate()
					return
				else
					my progressUpdate(i)
				end if
				
				--set thisTrack to (file track i of lib) -- get a track
				
				-- do stuff with each track...
				
			end repeat
		end try
	end tell
	
	my progressTerminate()
	
	show window "main"
	hide window "Progress"
	
end runTuneups

I meant post the project as a downloadable–without the nib and everything else it’s very difficult to recreate. I modified the project I created in the thread I mentioned above to use iTunes like you are and it works fine for me. You can see it here:

http://homepage.mac.com/jonn8/as/dist/Stop_Loop_iT.sit

Jon

OK, this is strange. I’ve found the solution to the confusion, but I have NO idea why this is so…perhaps you could enlighten me so I understand better.

I allowed my window to be visible at launch, so I didn’t use any of the handlers like “activate,” “will launch,” etc. It turns out that the “launched” handler is a necessity. Most of these, even in your code, were maily there to supply log traces so one could see what was going on.

Well, I clicked on the application, launched checkbox in the applescript panel of IB and everything I had worked instantly. There’s not even any code in the handler, and I changed nothing else. But IT WORKS!

Any clue why? Thank you kindly for your assistance (even if you don’t know why either, he he.)

WAUW! I had the very same problem!
And this really helped me to!

So strange… Well at least it worked!