"cancel" and escape key

Quick question.

Does the escape key only work on a button if it’s called “cancel”. I renamed the buttons on a dialog box to “quit” and “back” with “back” set to default and it would have been nice if the escape key activated the “quit” button.

It’s not a big deal and the script is working fine as it is, just like to make things as user friendly as possible.



If you leave the key you intend to have quit named “Cancel” then pressing that button will exit the script. Otherwise, you have to get the buttons returned of the dialog and if it’s “Back”, say, then return. “return” in the main script body will exit the script.

set x to display dialog "This is a test" buttons {"Outta Here", "Yea Team"} default button 1
if button returned of x is "Outta Here" then
	display dialog "I'm still here"
end if

Thanks, but that is what I am already doing.

set {text returned:job_name, button returned:button_returned} to (display dialog "Job title?" default answer "" buttons {"Quit", "Back", "OK"} default button 3)
if button_returned is "quit" then
else if button_returned is "back" then
	-- go back
	-- do stuff that's OK
end if

You did answer my question though in that the escape key only works if the button is called “cancel”.

FYI, the method Jacques used was added in OS X v10.4 (AppleScript 1.10). (You could also use: cancel button 1)

Thanks, very useful.

Still on 10.3.9 here at home, but on 10.4 at work so will be able to integrate it tomorrow.

Just out of interest, if you add code that only works in 10.4 (like cancel button 1) then make an application that runs under 10.3.9, does it crash or does it just ignore the code it can’t use?

With that in mind is there a way to question the Finder as to what operating system is being used?


The AppleScript version would be easier to check:

AppleScript's version


Enjoy your new MacBook Pro :slight_smile:

the system version check is only a few characters more :wink:

do shell script "defaults read /System/Library/CoreServices/SystemVersion ProductVersion"

Thanks Stefan.

But without worrying about the OS version for now, a problem I see with comparing Applescript version numbers is that in a comparison it sees 1.9.3 as being greater than 1.10.3.

I’ve written the following which does the job stripping off the “1.” from the start but this would soon come undone if Applescript ever goes to version 2.1.1 in which case 2.1.1 would be seen as less than 1.10.3

set current_version to characters 3 through (number of characters in (AppleScript's version as string)) of (AppleScript's version as string) as string
if current_version < "10.3" then
	display dialog "You have an older version" buttons {"Boo hoo"} default button 1
end if

I know a far simpler way is just outside my reach. Damn being such a newbie :frowning:

Convert the pieces to numbers:

set CV to AppleScript's version as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set P to text items of CV
set AppleScript's text item delimiters to tid
repeat with V in P
	set contents of V to V as integer
end repeat
V --> {1, 10, 7}

Then test whatever you want.

OR, do the whole test this way:

set CV to AppleScript's version as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
tell CV to set {a, b, c} to text items
set AppleScript's text item delimiters to tid
if 1 ≤ a and 10 ≤ b and 3 ≤ c then display dialog "OK"

Of course the logic needs tweaking to account for 2.1.1, for example, but I’ve got to run.

a very sophisticated way is:

on AppleScript1_10_3orNewer()
	system attribute "ascv"
	return (result is greater than or equal to 17826211)
end AppleScript1_10_3orNewer

AppleScript1_10_3orNewer() --> true (e.g. on a machine with OS 10.4.8)

the version can be calculated with:

tell 17826211 to ((it mod 4096 div 256 & "." & it mod 256 div 16 & "." & it mod 16) as string)
--> 1.10.3

(I found this here)

but I really don’t know how to calculate it reverse :wink:

Update: I know it now :slight_smile: (Thank you so much, Apple, for the programmer mode of calculator.app!)
the “ascv” value is a 32 bit long word. For the version of AppleScript only the lower 16 bits are valid.
So system attribute “ascv” mod 4096 determines the requested value, which is 423 for the current 1.10.7 version.
423 = 1 * 256 + 10 * 16 + 7
For the example above you can use also

on AppleScript1_10_3orNewer()
	(system attribute "ascv") mod 4096
	return (result is greater than or equal to 419)
end AppleScript1_10_3orNewer

AppleScript1_10_3orNewer() --> true (e.g. on a machine with OS 10.4.8)

While I have no idea if this works in earlier systems than Tiger, this is a way to get a system version name:

set SysNames to {"Cheetah", "Puma", "Jaguar", "Panther", "Tiger", "Leopard"}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set {x, y, z} to text items of (system info)'s system version
set AppleScript's text item delimiters to tid
set VN to item (1 + y) of SysNames

I have been using this which I got from Kai a while back. Short and sweet.

if ((system attribute "sysv") ≥ 4160) then
				--You are in Tiger 10.4
				--You are in an OS before Tiger
			end if

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

unfortunately not, because system info is new in Tiger, but this works:

set SysNames to {"Cheetah", "Puma", "Jaguar", "Panther", "Tiger", "Leopard"}
tell (system attribute "sysv") to set VN to item ((it mod 256 div 16) + 1) of SysNames

:lol::lol::lol: Great

Which is why AS 1.10 added a considering numeric strings statement (see the release notes).

Some people don’t like a command that calls a scripting addition, which starts a shell, which starts an executable, which reads an external file. (Not me though, I use shell stuff all the time!) I would use the full path to defaults though.

If you just want your application (bundle from Script Editor, or from Studio) to check for a minimum version of OS X, then you can let LaunchServices do the work. Just add a [url=http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/PListKeys.html#//apple_ref/doc/uid/20001431-113253]LSMinimumSystemVersion[/url] key to your info.plist file. (I did this with my Screenshot Settings app.)

hm, this is IMHO much more complicated then read the key ProductVersion of /System/Library/CoreServices/SystemVersion
or the gestalt code from system attribute “sysv” which really works in every version of OS X

I forget which method I was using before, but someone who should have been able to use my app was unable to; This was probably a mistake on my end. By taking the work out of my hands, I lessened the potential for error. shrugs

It would be more useful if someone made a handler for this stuff. oh, wait: Check for System version

o.k. here is a minimum handler which might be easy to comprehend :wink:

property minVersion : 0 + 4 * (16 ^ 1) + 0 * (16 ^ 2) + 1 * (16 ^ 3) -- (10.4.0)


on check_Sytem_version(minVersion)
	return not (system attribute "sysv") < minVersion
end check_Sytem_version

Thanks, StefanK. You get a bonus point for returning the value of the comparison directly. :cool: