Search code examples
xcodeapplescriptautomator

Applescript menu bar item toggle if title of attribute is... (or exists)


I need a applescript that clicks a menu item if the title of the item has a specific name. Here the concrete case (Sorry... I'm not allowed to embed pictures yet, so please click):

enter image description here

enter image description here

If the menu item 2 is "Enable calibration" it should be clicked. If the title is "Disable calibration" it should stop (I just put a "false" for testing in the code).

Here my first code but it doesn't work (Syntaxerror):

tell application "System Events" to tell process "SoundID Reference"
    if the title of the attribute "AXMenuItem" is "Disable calibration" then click it
        else
            return false
    end if
end tell

My second try was with the code "exists" but it doesn't work as well (just nothing happens):

tell application "System Events" to tell process "SoundID Reference"
    if menu item "Enable calibration" of menu 1 exists then click it
end tell

So any advices how I could solve this?


Solution

  • Finally I found another solution how to solve this problem with the "try"-command:

    tell application "System Events" to tell process "SoundID Reference"
    tell menu bar item 1 of menu bar 2
        try
            click menu item "Enable calibration" of menu 1
        end try
    end tell
    end tell