21

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?

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Black Milk
  • 515
  • 2
  • 8
  • 17
  • that isn't a standard menu item. – Daniel A. White May 11 '13 at 01:27
  • @DanielA.White are you suggesting that there's no solution for selecting this particular option via applescript? – Black Milk May 11 '13 at 01:32
  • 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 May 11 '13 at 01:43
  • 2
    What is the UI Browser Screen Reader that you have a screenshot of? – thetallweeks Sep 16 '15 at 02:06
  • @thetallweeks I believe it's UI Browser from http://pfiddlesoft.com/uibrowser/ – Black Milk Sep 16 '15 at 15:47

1 Answers1

40

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
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Lri
  • 26,768
  • 8
  • 84
  • 82
  • 2
    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. – Black Milk May 12 '13 at 00:35
  • btw, is there a way to hide the gui actions as the script is being run? – Black Milk May 13 '13 at 02:52
  • @BlackMilk No. You’re scripting a GUI, so you cannot hide it. It needs to be seen to be acted upon. – user137369 May 20 '15 at 13:04
  • The second code did not work for "Disable for an hour" replaced by "About f.lux". –  Oct 17 '18 at 01:55
  • How to escape the accented character of the name. For example `Fenêtre` instead of `Flux` ? –  Oct 17 '18 at 02:34
  • 1
    @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.) – clemsam lang Nov 07 '18 at 15:06
  • 3
    If you're having trouble with the second script, add a `delay 0.1` before the `killall System\\ Events` – Austin Hanson Jul 30 '20 at 14:21
  • @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. – scorgn Nov 07 '20 at 07:09
  • Does anyone know why the ignoring bit works? Is the Systems Events data cached by the script somewhere perhaps? – user336828 Aug 21 '21 at 02:47