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 Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Toggle State of Context via AppleScript? Thread Tools Search this Thread Display Modes
Does a simple way exist via AppleScript to toggle the state of an entire context Active, On Hold, and Dropped? It seems this is not a property of contexts that can be altered via AppleScript. I wonder if that means the only way to accomplish it is via a loop through actions to toggle their state?

The application is to set up a background app to toggle on/off such contexts as Errands at particular times.

--
JJW
 
Yes, here's a sample script that will toggle the availability of the Errands context each time it is run:

Code:
tell application "OmniFocus"
	tell front document
		set idHome to id of first item of (complete "Errands" as context)
		set homeContext to first item of (every context where id is idHome)
		tell homeContext
			set allows next action to not allows next action
		end tell
	end tell
end tell
Looking at the variable names, you might well deduce that I originally was toggling the state of a different context :-)
 
And to cycle a whole clump of selected contexts between three states:

Code:
-- Toggle selected contexts
tell application id "OFOC"
	tell front document window of front document
		-- GET A REFERENCE TO THE SELECTED CONTEXTS
		repeat with oPanel in {sidebar, content}
			set refContexts to (a reference to (selected trees of oPanel where class of its value is context))
			set lngContexts to count of refContexts
			if lngContexts > 0 then exit repeat
		end repeat
		if lngContexts < 1 then return
		
		-- GET THE STATE OF THE FIRST SELECTED CONTEXT
		tell (value of first item of refContexts) to ¬
			set {blnNotOnHold, blnHidden} to {allows next action, effectively hidden}
		
		-- CYCLE THE STATE AROUND A TRIANGLE
		-- Active --> on hold --> dropped --> active
		if blnNotOnHold then
			set {blnNotOnHold, blnHidden} to {false, false}
		else if blnHidden then
			set {blnNotOnHold, blnHidden} to {true, false}
		else -- on hold
			set {blnNotOnHold, blnHidden} to {false, true}
		end if
		tell value of refContexts to ¬
			set {allows next action, hidden} to {blnNotOnHold, blnHidden}
	end tell
end tell
--

Last edited by RobTrew; 2011-08-25 at 02:09 PM..
 
Thanks. Unfortunately, the context I am trying to reach is a sub-context. Substituting "@ : Errands" is not working. How do I get to a sub-context level?

--
JJW
 
If you are working with nested contexts, one word should do it:

every context --> every flattened context
 
In truly churlish fashion, I'll point out that you won't get the results you want if you are toggling a group of contexts which are not all in the same availability state :-)

If you had two sets of contexts which were mutually exclusive (set A is available when set B is unavailable, and vice versa), Rob's improved version would set them all to be available or unavailable. Here's an improvement on the improvement:

Code:
-- Toggle selected contexts
tell application id "OFOC"
	tell front document window of front document
		-- GET A REFERENCE TO THE SELECTED CONTEXTS
		repeat with oPanel in {sidebar, content}
			set refContexts to (a reference to (selected trees of oPanel where class of its value is context))
			set lngContexts to count of refContexts
			if lngContexts > 0 then exit repeat
		end repeat
		if lngContexts < 1 then return
		
		repeat with curContext in refContexts
			set blnOnHold to not allows next action of value of curContext
			set allows next action of value of curContext to blnOnHold			
		end repeat
	end tell
end tell
 
Ah, the endless fun opportunities that OmniFocus offers for tinkering rather than working :-)
 
Quote:
Originally Posted by RobTrew View Post
If you are working with nested contexts, one word should do it:

every context --> every flattened context
Oh happy day! Thank you.

--
JJW
 
Well, a difference in management style perhaps :-)

I like to dragoon my children into some kind of unison ...

(but I'm demonstrably just as willing to be distracted :-)
 
Here's yet another variation! This one prompts for a context name substring to match, in response to another thread where this sort of is being discussed. The new state for each context is independently determined, though to make full use of that, you might want to change the logic to have it just go between two states (active/dropped or active/on hold being the likely candidates) instead of active/on hold/dropped as is done here. Left as an exercise for the reader :-)

Code:
tell application "OmniFocus"
	-- prompt user for context substring to match
	set Matchstr to text returned of (display dialog ¬
		"Context substring to match" default answer ¬
		"" buttons {"OK", "Cancel"} ¬
		default button 1)
	
	tell front document
		set refContexts to (every flattened context where name contains Matchstr)
		set lngContexts to count of refContexts
		if lngContexts < 1 then -- oops, didn't find any matches!
			display alert "No matching contexts found for " & Matchstr
			return
		end if
		
		repeat with curContext in refContexts
			tell curContext to ¬
				set {blnNotOnHold, blnHidden} to {allows next action, effectively hidden} of curContext
			
			-- CYCLE THE STATE AROUND A TRIANGLE
			-- Active --> on hold --> dropped --> active
			if blnNotOnHold then
				set {blnNotOnHold, blnHidden} to {false, false}
			else if blnHidden then
				set {blnNotOnHold, blnHidden} to {true, false}
			else -- on hold
				set {blnNotOnHold, blnHidden} to {false, true}
			end if
			
			tell curContext to ¬
				set {allows next action, hidden} to {blnNotOnHold, blnHidden}
		end repeat
		
	end tell
end tell
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Omnifocus/applescript issue when setting context greeno OmniFocus 1 for Mac 3 2012-07-10 03:32 AM
Need basic applescript for changing context tah OmniFocus Extras 8 2009-07-30 12:51 PM
Applescript Snippet Request: Get Tasks for Context kenclark OmniFocus Extras 2 2009-02-12 02:46 AM
AppleScript to add tasks in a particular context to iCal for notifications dmcg OmniFocus Extras 0 2009-01-08 03:20 PM
Applescript example: listing by context RobTrew OmniFocus Extras 0 2007-08-21 01:31 AM


All times are GMT -8. The time now is 02:22 AM.


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