如果属性标题为...(或存在),则 Applescript 菜单栏项目切换

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

我需要一个 applescript,如果菜单项的标题具有特定名称,它可以单击该菜单项。 具体案例如下(不好意思,目前还不能嵌入图片,请点击):

如果菜单项 2 是“启用校准”,则应单击它。如果标题是“禁用校准”,它应该停止(我只是在代码中放了一个“false”用于测试)。

这是我的第一个代码,但它不起作用(语法错误):

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

我的第二次尝试是使用代码“存在”,但效果不佳(什么也没发生):

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

那么有什么建议可以解决这个问题吗?

最后我找到了另一个解决方案如何使用“try”命令解决这个问题:

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