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

 
Smart Contexts via AppleScript Thread Tools Search this Thread Display Modes
Here is a quick/ugly applescript I threw together that allows one to:

1. select multiple contexts
2. set all the filters
3. focus on a folder (or show all)
4. set or clear the search field

Just put in the values you want and save it with a meaningful name.

Some notes:

1. Applescript support for the sort fiter seems to be broken on Omni's end, so leave it blank for now.
2. Context selection is based on name-matching, so non-unique context names will only select the first instance.
3. Same for focusing on a folder
4. When onmi allows focusing on a project, more code will be required
5. There are a few calls to System Events, which is too bad, but what can you do.
6. I don't really know applescript well, so I'm sure there are parts that could have been accomplished better.

Code:
----------------------------------------------------------------------------
--Set smart parameters below. Empty strings leave existing setting.

--context(s)
property contextNames : {""} --a list of context names you want selected.  
-- *** if context names are not unique, only first instance will be selected ***

--filter settings
property contextGroupValue : "active-contexts" --choose from {"all-contexts","active-contexts"} 
property groupValue : "" --choose from {"none","context","due","start","competed","added","modified"}
property sortValue : "" --choose from {"none","name","due","start","complete","add","modified"}
-- *** sorting seems to be broken on Omni's end - leave it blank for now ***
property taskStateValue : "" --choose from {"all","next","available","incomplete","flagged","complete"}
property taskDurationValue : "" --choose from {"any","5m","15m","30m","60m","long","unestimated"}

--folder/project to focus on.  Use "All" to show all.
property focusFolderName : ""

--search Term.  Use "Clear" to force-clear the search term.
property searchTermValue : ""

----------------------------------------------------------------------------


global theWindow, theSidebar, theContent, foundFolder

tell application "OmniFocus"
	activate
	-- set some variables
	set theDocument to document 1
	set theWindow to document window 1 of theDocument
	set theSidebar to sidebar of theWindow
	set theContent to content of theWindow
	
	set selected view mode identifier of theWindow to "context"
	
	--make sure all contexts are expanded and deselected
	tell application "System Events"
		activate
		perform action "OOMakeFirstResponder" of outline "Sidebar" of scroll area 1 of splitter group 1 of window 1 of application process "OmniFocus"
	end tell
	activate
	tell application "System Events"
		perform action "AXPress" of menu item "Expand All" of menu "View" of menu bar item "View" of menu bar 1 of application process "OmniFocus"
		perform action "AXPress" of menu item "Deselect All" of menu "Edit" of menu bar item "Edit" of menu bar 1 of application process "OmniFocus"
	end tell
	--set selected contexts
	my selectContexts(theSidebar)
	
	--set filters
	if contextGroupValue is not "" then
		set selected smart group identifier of theSidebar to contextGroupValue
	end if
	if groupValue is not "" then
		set selected grouping identifier of theContent to groupValue
	end if
	if sortValue is not "" then
		set selected sorting identifier of theContent to sortValue
	end if
	if taskStateValue is not "" then
		set selected task state filter identifier of theContent to taskStateValue
	end if
	if taskDurationValue is not "" then
		set selected task duration filter identifier of theContent to taskDurationValue
	end if
	
	--set focus folder
	if focusFolderName is "All" then
		tell application "System Events"
			perform action "AXPress" of menu item "Show All Projects" of menu "View" of menu bar item "View" of menu bar 1 of application process "OmniFocus"
		end tell
	else if focusFolderName is not "" then
		if my selectFocus(theDocument) then
			set focus folder of theWindow to foundFolder
		end if
	end if
	
	--set search term
	if searchTermValue is "Clear" then
		set search term of theWindow to ""
	else if searchTermValue is not "" then
		set search term of theWindow to searchTermValue
	end if
	
end tell

on selectContexts(treeList)
	tell application "OmniFocus"
		repeat with thisTree in trees of treeList
			if contextNames contains name of thisTree then
				select tree (name of thisTree) of treeList with extending
			end if
			my selectContexts(thisTree) --recurse
		end repeat
	end tell
end selectContexts

on selectFocus(FolderList)
	tell application "OmniFocus"
		repeat with thisFolder in folders of FolderList
			if focusFolderName is name of thisFolder then
				set foundFolder to thisFolder as reference
				return true
			else if my selectFocus(thisFolder) then --recurse
				return true
			end if
		end repeat
	end tell
	return false
end selectFocus
 
And here is one to put everything back to default:

Code:
-- set parameters
set contextGroupValue to "active-contexts"
set groupValue to "none" as text
set sortValue to "none" as text
set taskStateValue to "incomplete" as text
set taskDurationValue to "any" as text
set searchTermValue to "" as text
set focusFolderValue to null

tell application "OmniFocus"
	activate
	set theWindow to document window 1 of document 1
	set theSidebar to sidebar of theWindow
	set theContent to content of theWindow
	set selected view mode identifier of theWindow to "context"
	
	--make sure all contexts are expanded and deselected
	tell application "System Events"
		activate
		perform action "OOMakeFirstResponder" of outline "Sidebar" of scroll area 1 of splitter group 1 of window 1 of application process "OmniFocus"
	end tell
	activate
	tell application "System Events"
		perform action "AXPress" of menu item "Expand All" of menu "View" of menu bar item "View" of menu bar 1 of application process "OmniFocus"
		perform action "AXPress" of menu item "Deselect All" of menu "Edit" of menu bar item "Edit" of menu bar 1 of application process "OmniFocus"
	end tell
	
	--Set the filters
	set selected smart group identifier of theSidebar to contextGroupValue
	set selected grouping identifier of theContent to groupValue
	(*uncomment this when the bug is fixed
	set selected sorting identifier of theContent to sortValue
	*)
	set selected task state filter identifier of theContent to taskStateValue
	set selected task duration filter identifier of theContent to taskDurationValue
	
	--set focus folder and search term
	tell application "System Events"
		perform action "AXPress" of menu item "Show All Projects" of menu "View" of menu bar item "View" of menu bar 1 of application process "OmniFocus"
	end tell
	
	--set focus folder of theWindow to focusFolderValue
	set search term of theWindow to searchTermValue
	
end tell
 
brilliant! works great!
 
works for me too. thanks for sharing!
 
Almost useless now that OmniObjectMeter is turned back on. I think it is just the slow response time that is causing the script to bail sometimes. But I'll have to wait and see.
 
I've customized a couple of versions of this script and I find it terrifically helpful (until the kinks in the Perspectives feature get worked out).

I've noticed that the script works flawlessly when invoked from the scripts menu, or from Quicksilver, but I get an error when trying to start it from an icon added to OmniFocus's toolbar ("AppleScript reported the following error:
System Events got an error: NSReceiverEvaluationScriptError: 4")...
 
Script had multiple errors, and totally fubared my settings in Revision 95718. Thank goodness for database restore!

I'm looking to restore default settings for text styles in Actions. I was playing around with changing the text styles before I learned about Sequential vs. Parallel Projects, and haven't yet discovered a way of restoring the default text-style settings.

Any advice?
 
As you discovered, applying a 7 month old script written for an alpha version of a product probably wasn't a great idea.

What you want is probably Format --> Clear Style, which will remove the custom styling of what ever sort of action you have selected.

Hope that helps!
__________________
Cheers,

Curt
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export from the command line or AppleScript for specific contexts/projects? xuinkrbin. OmniFocus Extras 1 2010-09-30 03:21 PM
Making sub-contexts in AppleScript? nate OmniFocus Extras 3 2009-02-12 09:17 PM
Recursing nested contexts in AppleScript dmcg OmniFocus Extras 2 2009-01-06 11:45 PM
Smart Contexts sriggins OmniFocus 1 for Mac 5 2008-06-21 04:32 PM
Feature Request: "Smart" contexts frosquin OmniFocus 1 for Mac 11 2007-05-29 10:09 AM


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


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