System events can't get process Omnifocus

hi all,

Relatively new to UI scripting with AppleScript, but not to scripting/coding in general, and I’d really appreciate any assistance with this wall I keep hitting.

I have a couple of “dashboard”-type applescripts that pop up a bunch of OF windows to different perspectives and tile them in a way that I like. One wrinkle I’ve been trying to iron out is that I want the sidebar settings on these windows to be consistent. I’m trying to do this by having System Events click the Hide Toolbar menu item, but System Events seems not to be able to address the Omnifocus process. Below is a test script that shows the problem with the simple example of opening a new window. Every one of these blocks works, except the Omnifocus one! That one throws this error: “System Events got an error: Can’t get process “Omnifocus”.” number -1728 from process “Omnifocus”.

tell application “System Events” to tell process “Finder”
click menu item “New Finder Window” of menu 1 of menu bar item “File” of menu bar 1
end tell

tell application “System Events” to tell process “Safari”
click menu item “New Window” of menu 1 of menu bar item “File” of menu bar 1
end tell

tell application “System Events” to tell process “Chrome”
click menu item “New Window” of menu 1 of menu bar item “File” of menu bar 1
end tell

tell application “System Events” to tell process “Omnifocus”
click menu item “New Window” of menu 1 of menu bar item “File” of menu bar 1
end tell

I’m on Mojave and I know there are permissions weirdnesses with automation. I have Script Editor and Omnifocus added to the Security and Privacy > Accessibility pane. I have also done similar things successfully in the past by saving the script as an app and then adding the app to Accessibility pane, but I don’t understand why that would be necessary for Omnifocus when it clearly isn’t necessary for e.g. Chrome.

Thanks for any assistance!

G

Seems like the app name is probably case-sensitive. Try this:

tell application "System Events" to tell process "OmniFocus"
    click menu item "New Window" of menu 1 of menu bar item "File" of menu bar 1
end tell

Wow. Thank you - I had not noticed this and it was indeed the problem with my “New Window” test code.

However, even with this straightened out, I still am not able to address the View menu. Do you see any problem with the below?

tell application “System Events” to tell process “OmniFocus”

#this command works	
click menu item "New Window" of menu 1 of menu bar item "File" of menu bar 1

#this doesn't work and just reproduces the command to the log
click menu item "Show Sidebar" of menu 1 of menu bar item "View" of menu bar 1

end tell

The latter only works if the window is active, so try this? Should work if the Sidebar is hidden when you invoke it.

tell application "System Events" to tell process "OmniFocus"
    set frontmost to true
    click menu item "Show Sidebar" of menu 1 of menu bar item "View" of menu bar 1
end tell

Hope that helps!

Yes! Clearly I need to read more about UI scripting. I never would have guessed that targeting the View menu would require set frontmost when targeting the File menu doesn’t. Thank you so much.

1 Like