AppleScript在菜单项旁边切换复选标记

时间:2013-05-12 05:40:07

标签: macos applescript

这是上一个问题的扩展 Applescript: on clicking Menu Bar item via gui script

在f.lux菜单栏下方突出显示的菜单项上,如果单击它,将会显示一个复选标记,表示已启用“禁用一小时”功能。我要做的是为f.lux编写一个GUI AppleScript,用户可以决定通过在Alfred中输入一个关键字然后输入1或0来切换复选标记,其中1用于启用“禁用一小时” “和0,用于保持不受控制。

这是f.lux

菜单栏的屏幕截图

screen shot of the element inspector providing info on the "Disable for an hour" element and its attributes

但是,我很难确定要为“禁用一小时”菜单项调整哪个属性才能切换复选标记。这是代码,但是在通过applescript编辑器编译时会出现意外的令牌错误。到目前为止,我要做的是针对上面屏幕截图中箭头指示的“菜单项标记字符”属性,但我不确定这是否是切换“禁用一小时”项目的正确方法。有人可以给我建议吗?

on alfred_script(q)
    set myOption to q as integer

ignoring application responses
    tell application "System Events" to tell process "Flux"
        click menu bar item 1 of menu bar 2
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Flux"
    tell menu bar item 1 of menu bar 2
        if myOption is 1 then
            set ✓ to value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1

        else if myOption is 0 then
            set nil to value of attribute "AxMenuItemMarkChar" of menu item "Disable for an hour" of menu 1

        end if

    end tell 
end tell

end alfred_script

1 个答案:

答案 0 :(得分:5)

这似乎有效:

tell application "System Events" to tell process "Flux"
    tell menu bar item 1 of menu bar 2
        set v to (value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1) as string
        if (v = "" and myOption = 1) or (v is not equal to "" and myOption = 0) then
            click menu item "Disable for an hour" of menu 1
        else
            key code 53
        end if
    end tell
end tell