且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

applescript 单击菜单栏选项

更新时间:2023-01-05 20:08:56

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.