Applescript to show Apple menu bar items

2019-07-03 01:50发布

问题:

I am an Applescript newbie. I want to know Applescript to show the Apple menu bar items from an arbitrary application (but the many bar should remain for the orginal application).

I tried the following script, using Finder as a kind of dummy app, but it did not work.

tell application "System Events"
    tell process "Finder"
        tell menu bar 1
            click menu bar item "Apple"
        end tell
    end tell
end tell

Can anyone help?

PS-1: I want to know this because Control-F2 to move focus to menu bar often does not work, as reported in the following link: https://apple.stackexchange.com/questions/12723/control-f2-move-focus-to-menu-bar-only-works-occasionally

PS-2: I tried to post an image, but not allowed.

回答1:

process 1 where frontmost is true gets the frontmost process:

tell application "System Events" to tell (process 1 where frontmost is true)
    click menu bar item 1 of menu bar 1
end tell

Clicking menu bar items doesn't work in full screen windows though. And if System Events is not used for a few minutes, it closes automatically, and there's a short delay when it's opened again.



回答2:

Do you want to actually show the menu items, i.e. either in the open menu or in a dialog window, or do you only want to select a menu item from a menu?

If you run the following script, the Apple menu will open. Use ASCII characters 28-31 to navigate the menus.

tell application "System Events"
  -- focus
  key code 120 using control down
  -- navigate
  keystroke (ASCII character 31)
end tell

Universal access must be turned on and menu navigation must be on too. If menu navigation is off, you need to type Control-F1 to turn it on. You can do this by script, but I don't know how to use AppleScript to check its status. The keycode for F1 is 122.



回答3:

Try this one out. It uses a function instead of the complicated tell structure you are currently utilizing.

tell application "Finder" to activate
menu_click({"Finder", "View", "Arrange By", "Size"})