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?

5
  • that isn't a standard menu item. Commented May 11, 2013 at 1:27
  • @DanielA.White are you suggesting that there's no solution for selecting this particular option via applescript? Commented May 11, 2013 at 1: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. Commented May 11, 2013 at 1:43
  • 2
    What is the UI Browser Screen Reader that you have a screenshot of? Commented Sep 16, 2015 at 2:06
  • @thetallweeks I believe it's UI Browser from pfiddlesoft.com/uibrowser Commented Sep 16, 2015 at 15:47

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

9 Comments

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.
btw, is there a way to hide the gui actions as the script is being run?
@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.)
If you're having trouble with the second script, add a delay 0.1 before the killall System\\ Events
Does anyone know why the ignoring bit works? Is the Systems Events data cached by the script somewhere perhaps?
|
1

I had to do a similar kind of scripting, and this is what worked for me, even though it didn't respect the hierarchy (which is actually <AXApplication: “FortiTray”> <AXMenuItem: “Connect”>)

tell application "System Events" to tell process "FortiTray"
    repeat
        try
            click menu item "Connect to XYZ" of menu 1 of menu bar 1
        end try
        delay 2 -- = Time to wait until next check in seconds
    end repeat
end tell

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.