17
votes

I'm trying to make an applescript for an application called F.lux that clicks the menu item "Disable for an Hour" as indicated in the screenshot below:

enter image description here

The element path is indicated in the screenshot below:

enter image description here

Here is my code thus far:

tell application "System Events"
    tell process "Flux"
        click (menu bar item 1 of menu bar 2)
        click menu item "Disable for an hour" of menu 1 of menu bar item 1 of        
        menu bar 2
    end tell    
end tell

Everything compiles fine, however I keep getting the error message below when I attempt to run the script:

error "System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process \"Flux\". Invalid index." number -1719 from menu 1 of menu bar item 1 of menu bar 2 of process "Flux"

Can someone pinpoint where I'm going wrong with this?

1
that isn't a standard menu item. - Daniel A. White
@DanielA.White are you suggesting that there's no solution for selecting this particular option via applescript? - Black Milk
I believe that you cannot use GUI scripting to talk to a menu bar extra that doesn't belong to the system (a third-party menu bar extra such as this one). Don't hold me to it, but that's my general impression. You'll probably want to do some further research. - matt
What is the UI Browser Screen Reader that you have a screenshot of? - thetallweeks
@thetallweeks I believe it's UI Browser from pfiddlesoft.com/uibrowser - Black Milk

1 Answers

37
votes

This worked for me, but there is a delay of about 5 seconds after the first click command.

tell application "System Events" to tell process "Flux"
    tell menu bar item 1 of menu bar 2
        click
        click menu item "Disable for an hour" of menu 1
    end tell
end tell

One workaround is to use ignoring application responses and terminate System Events after the click command:

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
        click menu item "Disable for an hour" of menu 1
    end tell
end tell