Question concerning "choose folder" Cancel button

This is a rather embarassing question after all the scripts I have made so far :oops:

In my Applescript I ask for a folder with:

set folderSelected to choose folder "Select a folder"

Every single script I have seen sofar forgets that there is a Cancel button!

How do I quit/exit my script when the user clicks Cancel?

Just trying this:

try
		set folderSelected to choose folder "Select a folder"
	on error
		quit
	end try

Wont work and generates an error…

Googling around…
Come on Apple, is there no way to abort/exit/quit the script itself??

If your script is linear (that is, you choose a folder, then make some operations in the same scope), you can keep it as is:

set f to choose folder

If user hits “Cancel”, it will generate a silent error -128 (user cancelled), and execution will be automatically aborted.

Thanks jj!

Here I have no scope problems so it wil work.
But in other situations I might have to do-some-stuff before aborting and then “abort script” would be nice!

But guess what: just before you answered me I found this:

The statement:


error number -128

will let me abort at any time!

Now why didn’t Apple just put an “abort script” in the language.??

Oh, well. This works. But this is not documented anywhere… :evil:

Hi,

Check out the AppleScriptLanguageGuide.pdf, pg. 210 on ‘error’:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/index.html

gl,

Thanks, gl !

I have had AppleScriptLanguageGuide.pdf a while and I reference it all the time.

I have found how to trap error codes and how to respond to them.
But nowhere is it mentioned that you can use “error number -128” as a statement do actively do something!

Just one of those things you pick up while learning!

You can also use the statement “return”. Eg:

beep
return
display dialog "I will not reach this!"

This “return” will make the script jump to its “caller”. In the example above, we have a implicit “run” handler. Since there is no caller, the script will simply stop execution. If you place, though, the “return” in this example:

doSomething()
display dialog "I should reach this!"

to doSomething()
	beep
	return
	display dialog "I will not reach this!"
end doSomething

Here, the “return” will stop the execution of the “doSomething” handler, then the implicit “run” handler will continue its tasks.
And, as you guess, this is the typical way to handle errors -128 (if you need it):

try
	display dialog "foo" --> its the same for choose-whatever
on error msg number n
	if n is -128 then --> user cancelled
		display dialog "do not cancel, please" buttons {"OK"} default button 1
		return run
	end if
end try
display dialog "Thanks for hitting "OK"…"

Very educational, jj ! Thanks!