The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus 1 for Mac
FAQ Members List Calendar Search Today's Posts Mark Forums Read

applescript for common OF tasks Thread Tools Search this Thread Display Modes

I was wondering if there is a way to create a simple apple script for some common tasks that I do in omnifocus.

I would like an applescript that did the following:

- "Clean Up"
- Expanded everything
- Sorted everything projects by Name

In short, can the menu bars items be scripted? Does anyone have samples?

Here's a "clean up" script:
tell application "OmniFocus"
	compact default document
end tell
I believe we support UI scripting via the method discussed on this page, but my brief experimentation failed to yield a working example. This is hopefully a failure of the scripter, and not the app. :-)

Instead, here's one that uses a different approach: it triggers the "Expand all" command by simulating the keyboard shortcut:

tell application "System Events"
	tell process "OmniFocus"
		keystroke "9" using {command down, control down}
	end tell
end tell
The Alphabet-sort of the project list would be a bit trickier, as it's not susceptible to the fake-keyboard-events approach; there's no keyboard shortcut for the "sort by" options in the Edit menu.

Lemme chew on that one for a bit. Possibly including asking one of the engineers about it next time I bump into them in the lunch line. ;-)
Okay, after a bit of experementation, this appears to work, with the caveat that you need to select something in the sidebar manually before running it. Hope this helps!

activate application "OmniFocus"
tell application "System Events"
	tell process "OmniFocus"
		--select all
		keystroke "a" using {command down}
		-- expand all
		tell menu item 1 of menu of menu item 15 of menu 4 of menu bar 1
		end tell
	end tell
end tell
Figuring out the menu numbers can, of course, take a bit of time.
Another approach is to generalise a bit and simply write something like:

GetMenuItem("OFOC", {"Edit", "Sort", "By Name"})

(full code, with reusable functions, below)

tell application id "sevs"
	set mnuSort to my GetMenuItem("OFOC", {"Edit", "Sort", "By Name"})
	tell application id "OFOC" to activate
	click mnuSort
end tell

-- E.G. set mnuZoomFit to GetMenuItem("OGfl", {"View", "Zoom", "Zoom to Selection"})
on GetMenuItem(strAppCode, lstMenu)
	set lngChain to length of lstMenu
	if lngChain < 2 then return missing value
	tell application id "sevs"
		set lstApps to application processes where its creator type = strAppCode
		if length of lstApps < 1 then return missing value
		tell first item of lstApps
			set strMenu to item 1 of lstMenu
			set oMenu to menu strMenu of menu bar item strMenu of menu bar 1
			repeat with i from 2 to (lngChain - 1)
				set strMenu to item i of lstMenu
				set oMenu to menu strMenu of menu item strMenu of oMenu
			end repeat
			return menu item (item -1 of lstMenu) of oMenu
		end tell
	end tell
end GetMenuItem

on GUIEnabled()
	tell application id "sevs"
		if UI elements enabled then
			return true
			display dialog "This script depends on enabling access for assistive devices in system preferences" buttons "OK" default button "OK" with title pTitle & "   " & pVer
			tell application id "sprf"
				set current pane to pane id ""
			end tell
			return false
		end if
	end tell
end GUIEnabled
On expansion, the keyboard command works well if you only want to expand whichever panel (sidebar or content) is selected.

You can get a bit more flexibility, if you need it, and combine expand and collapse in a single toggle, with a customized expansion script.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Applescript to get all tasks (when focused) mkko OmniFocus Extras 1 2011-07-11 12:44 PM
Connections with a common branch invictus26 OmniGraffle General 0 2011-04-08 11:34 AM
Avoiding duplication of actions common to multiple projects omnibob OmniFocus 1 for Mac 0 2008-10-02 08:42 AM
Selecting new tasks via Applescript scb OmniFocus Extras 3 2007-09-06 10:30 PM

All times are GMT -8. The time now is 08:41 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.