Key command/s for non-default dialog button/s [newbie]

Hi all,

I'm challenged in the geek department, so please be nice ... I just want to know how to provide keyboard commands for non-default button/s in an applescript dialog. I know you hit 'return' or 'enter' for the default button. But I have a script I'm trying to develop for something I do with my e-mails (Mail.app), and it involves frequent dialogs with two buttons, and the non-default actually applies maybe 40% of the time. So I'd like to be able to press either button without needing to reach for the mouse and move the cursor.

Is there a simple line or two of code to enable this? Or is it not that simple? Thanks.

macbook2.16, Mac OS X (10.5.4), Also on home LAN: G4 macmini 1.5G and eMac 1G, both w/ 10.4.11; one PC with XP

Posted on Jul 30, 2008 10:20 PM

Reply
7 replies

Jul 31, 2008 6:36 PM in response to Lance Lawton

Camelot's right, but either you're not implementing it correctly or not observing closely what's going on.

If you write this line of code:

display dialog "My dialog" buttons {"A","B","C") default button 1

you will get three buttons with "A" TOTALLY highlighted with a color (the color depending on appearance preferences, I think) and that highlighted button and ONLY that highlighted button will respond to the "RETURN" key. If you had written "default button 2," then "B" will be the highlighted button, and so on.

BUT to see clearly what can be done, and use Camelot's suggestion, write:

display dialog "My dialog" buttons {"A","B","C"} -- NO button will be totally highlighted, as no button has been specified as the default button

HOWEVER, if you now press tab (single presses) you will move a "halo" along from button to button, and the one with the halo will respond to the SPACE BAR. (Or, as Camelot said, you can move the "halo" in reverse order by pressing space and tab together.)

NOW, combine the two approaches. Specify the default button (which will respond to RETURN) and move the halo along with TAB (or SPACE/TAB); you'll see that the "halo" is distinct from the default highlighting, and the button with the halo will respond to the SPACE BAR.

Try it, you'll like it (maybe).

Jul 31, 2008 7:23 PM in response to osimp

Thanks, osimp. But sadly I'm no closer. See below ...

display dialog "My dialog" buttons {"A","B","C") default button 1

you will get three buttons with "A" TOTALLY highlighted with a color (the color depending on appearance preferences, I think) and that highlighted button and ONLY that highlighted button will respond to the "RETURN" key. If you had written "default button 2," then "B" will be the highlighted button, and so on.


I'm fine with that, and that's how my dialogs work now.

display dialog "My dialog" buttons {"A","B","C"} -- NO button will be totally highlighted, as no button has been specified as the default button


So far so good, but ...

HOWEVER, if you now press tab (single presses) you will move a "halo" along from button to button, and the one with the halo will respond to the SPACE BAR. (Or, as Camelot said, you can move the "halo" in reverse order by pressing space and tab together.)


well that's where it comes unstuck. Tried exactly what you said, tried it again ... does not work. No amount of tab clicking produces any halo on any button; tried spacebar just in case I could have missed an invisible halo - nothing there either.

NOW, combine the two approaches. Specify the default button (which will respond to RETURN) and move the halo along with TAB (or SPACE/TAB); you'll see that the "halo" is distinct from the default highlighting, and the button with the halo will respond to the SPACE BAR.


Well obviously I didn't get that far, although I did try adding a default button .. but nup, nothing doing 😟

Wondered whether it may be something broken in Leopard. But just tried it on another computer running 10.4.11 - same results.

What now???

Jul 31, 2008 8:52 PM in response to Andrew99

You're a legend, Andrew. That did it 🙂 I can now do what camelot & osimp suggested.

But to resume the original question ... I'm still interested in knowing whether it's possible to script for something zippier than the tab/spacebar option (although that's pretty good). What I'd absolutely dream of is being able to hit a key or command shortcut to press the (or a) non-default button. Can that be scripted?

Aug 1, 2008 6:24 AM in response to Lance Lawton

Hello

If you have only two buttons, you may try something like the following CODE 1 (for ApplesScript 1.10 or later). It will allow you to select button "B" by enter/return key and button "A" by escape key. (Note that you cannot make the cancel button the default button in this method.)

--CODE 1
return choose2("Choose A or B.", {"A", "B"}, 2)
on choose2(t, {a, b}, i)
(*
string t: dialogue text
string a, b: button names
integer i: default button's index in {a, b}
return string: name of button selected
* enter/return key invokes button i and escape key invokes another
*)
try
display dialog t buttons {a, b} default button i cancel button (i mod 2 + 1)
return button returned of result
on error number -128
return {a, b}'s item (i mod 2 + 1)
end try
end choose2
--END OF CODE 1



If you have more than two buttons, you may use 'choose from list' command like CODE 2. In AppleScript 1.10 or later, you can select a list item by typing the first few letters of its name. You may also use up and down arrow keys to navigate through the list. (After all, you need one key stroke (return/enter) to choose the default item, and two or more key strokes to choose the others.)

--CODE 2
return chooseN("Choose A or B or C", {"A", "B", "C"}, 2)
on chooseN(t, ll, i)
(*
string t: prompts
list ll : choices
integer i : default item's index in ll
return string : name of chosen item
*)
tell (choose from list ll default items (ll's item i as list) with prompt t)
if it is false then error number -128
return item 1
end tell
end chooseN
--END OF CODE 2

Hope this may be of some help,
H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Key command/s for non-default dialog button/s [newbie]

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.