Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript: on clicking Menu Bar item via gui script

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?

like image 863
Black Milk Avatar asked May 11 '13 01:05

Black Milk


1 Answers

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
like image 137
Lri Avatar answered Oct 26 '22 21:10

Lri