Applescript: on clicking Menu Bar item via gui script
Asked Answered
F

1

22

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?

Fourlegged answered 11/5, 2013 at 1:20 Comment(5)
that isn't a standard menu item.Aggi
@DanielA.White are you suggesting that there's no solution for selecting this particular option via applescript?Fourlegged
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.Borrow
What is the UI Browser Screen Reader that you have a screenshot of?Intuitive
@Intuitive I believe it's UI Browser from pfiddlesoft.com/uibrowserFourlegged
B
42

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
Beckiebeckley answered 11/5, 2013 at 13:22 Comment(9)
this worked perfectly. I like your website btw, seems like a good place to get a feel for applescript. I'd like to +1, but I need more rep.Fourlegged
btw, is there a way to hide the gui actions as the script is being run?Fourlegged
@BlackMilk No. You’re scripting a GUI, so you cannot hide it. It needs to be seen to be acted upon.Overshine
The second code did not work for "Disable for an hour" replaced by "About f.lux".Yardstick
How to escape the accented character of the name. For example Fenêtre instead of Flux ?Yardstick
@BlackMilk You CAN hide gui actions by calling an (even running) app via do shell script "open -g applicationNamePath" in advance, next telling System Events what to do. GUI must be "visible" to System – NOT to a user. So, if an app is in background, it will stay there ("readable"), if it opens freshly it will open behind the frontmost app. Of course any menu clicks will be visible, though in milliseconds. (I do some occasional "System Preferences setting" this way, really fast.)Proposal
If you're having trouble with the second script, add a delay 0.1 before the killall System\\ EventsReduced
@AustinHanson Thanks for the comment, I wasn't able to get it working until I did that. Actually, the delay 0.1 after the do shell script didn't seem necessary at all with it before.Beta
Does anyone know why the ignoring bit works? Is the Systems Events data cached by the script somewhere perhaps?Jennee

© 2022 - 2024 — McMap. All rights reserved.