Black Milk Black Milk - 28 days ago 10
AppleScript Question

AppleScript toggling checkmarks beside menu items

This is an extension of a previous question
Applescript: on clicking Menu Bar item via gui script

On the highlighted menu item below for the f.lux menu bar, if you click it, there will be a checkmark indicating that the "Disable for an hour" feature has been enabled. What I'm trying to do is write a GUI AppleScript for f.lux where the user can decide to toggle the check mark by typing a keyword in Alfred followed by a 1 or a 0 where 1 serves to enable the "Disable for an hour" and 0, serves to keep it unchecked.

This is a screen shot of the menu bar for f.lux

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

I am however having a hard time figuring out what attribute to adjust for the "Disable for an hour" menu item in order to toggle the checkmark. Here is the code, but I get an unexpected token error when compiling it via applescript editor. So far what I'm trying to do is target the "menu item mark character" attribute indicated by the arrow in screenshot above, but I'm not sure if this is the right approach to toggle the "Disable for an hour" item. Can someone please give me advice?

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

Answer Source

This seems to work:

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