0

I really looked up and down lots of questions, here and at "AskDifferent", but could not find a valid answer to this fundamental question:

HOW do you "activate" programatically a menu item that's only visible when the option key is depressed and that hasn't got a shortcut of its own ?
I know how to use:

click menu item "Save as …" ...  [or:]  perform action of menu item "..." ...

... quite well.
I've also learned about previously-set commands like:

key down option    [or:]  keystroke "whatever" using option down

But none of these will perform a simple click on a menu item that "regularly" isn't visible.


Admitted, using an existing shortcut like "option shift command s" will "Save as …" as expected:

keystroke "s" using {option down, shift down, command down}



(Farther down I was proven wrong by pbell; so here is my latest "simplified" solution:

tell application "System Events" to perform action "AXPress" of menu item ¬
      "Arrange in front" of menu "Window" of menu bar item "Window" of ¬
      menu bar 1 of application process "Script Editor"

(As I found out by now, this code will do the exact same thing.)

clemsam lang
  • 440
  • 3
  • 11

3 Answers3

3

Actually all menu items are accessible via System Events / GUI scripting even they are not visible. Altering modifier keys is not needed.

This code is sufficient. The line to make the application frontmost can be omitted as the menu items can be accessed even the application is not in the foreground.

tell application "System Events"
    tell process "Script Editor"
        set frontmost to true -- replaces 'activate application "Script Editor"'
        perform action "AXPress" of menu item "Arrange in front" of menu "Window" of menu bar item "Window" of menu bar 1
    end tell
end tell
vadian
  • 274,689
  • 30
  • 353
  • 361
  • You are right . . . BUT: your twice called tell-commands are already "integrated" in my "lean" one-liner that uses "perform action ...", as "Script Editor" is called there at the end of line. – clemsam lang Dec 21 '15 at 09:57
  • I know, it's just for better readability. By the way: `of application "System Events` is redundant in your one-liner – vadian Dec 21 '15 at 09:59
  • Thanks, vadian. (The redundance stems from System Events' self-information to my query: . . . actions of menu item "Arrange in front" of … ) – clemsam lang Dec 21 '15 at 10:33
  • 1
    `perform action "AXPress" of menu item "Arrange in front"` -- that's very clever. Thanks for sharing. – JMichaelTX Dec 21 '15 at 19:36
  • A "post question" to this answer: . . . could **perform action** even access one of the listed "Recently used folders" of the pop up list in any app's "Open file" dialog window? (kind of drop down menu itself, with user-specific folder names) . . . I couldn't get _THAT_ to work, i used **click** instead ... :-( – clemsam lang Dec 22 '15 at 07:42
  • `perform action` works only if the particular UI element supports that action. For a popup button usually you `click` the button, add a short delay (`delay 0.2`) and then click the requested menu item of menu 1 of the popup button to perform the associated action. – vadian Dec 22 '15 at 07:48
1

You must use key down and key up instructions. the script bellow does what's you're looking for . It shows the menu Window of the Script-Editor with option key down.

tell application "System Events"
tell process "Script Editor"
    key down option
    click menu bar item 9 of menu bar 1 --using {option down}
    delay 2 -- just to show ! to be replaced by your click on your menu item
    key up option -- don't forget this : if you do, it keeps option down until you force to quit the process !!!
end tell    
end tell
pbell
  • 2,965
  • 1
  • 16
  • 13
  • Thanks a lot! Actually what I wanted to do is nealy what you advised. So the solution is all yours, but I'll post a last answer in which I write "my" desired solution. – clemsam lang Dec 20 '15 at 21:30
0

I would use this:

activate application "Script Editor"
tell application "System Events"
    tell process "Script Editor"
        click menu item "Arrange in Front" of menu 1 of menu bar item "Window" of menu bar 1
    end tell
end tell

vadian's method is an acceptable approach as well.

adayzdone
  • 11,120
  • 2
  • 20
  • 37