applescript click menu bar option

夙愿已清 提交于 2019-12-11 04:21:09

问题


I want to create an AppleScript to click a menu but I can't what ever the script I using, here is the menu I want :

ELEMENT :
 Role : menu item
 Title: "sign in As..."
 Description :
 Help:
 Application: SytemUIServer

ELEMENT PATCH (starting at leaf element):
 menu Item "Sign in As..." (menu item 12)
  menu (menu 1)
   menu extra (menu bar item 2)
    menu bar (menu bar 1)
      application "SystemUIServer"

So I did a few script, the last one was

ignoring application responses
    tell application "System Events" to tell process "SystemUIServer"
        click menu bar item 2 of menu bar 1
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
    tell menu item 12 of menu 1 of menu bar item 2 of menu bar 1
        click
    end tell
end tell

Also I just realise that the position can change (some time the item I want to click is menu item 12 sometime its 10 etc.)


回答1:


In your question, you didn't specify name of the menu bar item and name of the app which owns the menu bar item. That's the problem.

First, SystemUIServer only run menu bar items/icons native to OS X. To see the icons it runs, do these three lines separately in Script Editor.

1)

tell application "System Events" to tell process "SystemUIServer" ¬
to number of menu bars

2)

tell application "System Events" to tell process "SystemUIServer" ¬
to value of attribute "AXDescription" of menu bar items of menu bar 1

3)

tell application "System Events" to tell process "SystemUIServer" ¬
to title of menu bar items of menu bar 2

The results should look something like:

1) 2

2) {"Wi-Fi, four of four bars, with WiFiName.", "Battery: Charged ", "Clock"}

3) {"Notification Center", missing value}

Third-party apps, plus Spotlight, manage its own menu bar items/icons. Spotlight for example:

tell application "System Events" to tell process "Spotlight" ¬
to title of menu bar items of menu bar 1

This gives you: {"Spotlight"}

If you have Caffeine:

tell application "System Events" to tell process "Caffeine" ¬
to title of menu bar items of menu bar 1

You get: {missing value}, 'cause its programer didn't bother naming the item.

So if this is third-party menu bar item you are trying to script, it's not in SystemUIServer. If you only refer to the menu bar item with position instead of its name, you cannot reliably click it every time.

McUsr inserted this line, in order to save the edit that had to be of minimum 6 characters.



来源:https://stackoverflow.com/questions/29699577/applescript-click-menu-bar-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!