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

 
Toggling between collapse all and expand all Thread Tools Search this Thread Display Modes
A draft applescript to toggle between collapsing and expanding all project tasks and sub-tasks.

Mainly illustrative - it works if run from the script menu, but its practical utility is limited by the fact that, (at least as of OF build 98016) static variables like blnNewState below lose their state between runs if the script is run from the OF toolbar.

Thus, if used from the toolbar, the script provides only a Collapse All function, whereas if run from the script menu, it alternately functions as Collapse All or Expand All, switching every time it is run.

Code:
tell application "OmniFocus"
	global blnNewState
	
	try
		blnNewState
		set blnNewState to not blnNewState
	on error
		set blnNewState to false
	end try
	
	tell content of window 1
		
		set oTrees to trees
		
		repeat with oTree in oTrees
			my Expand(oTree, blnNewState)
		end repeat
	end tell
end tell


on Expand(oTree, blnNewState)
	using terms from application "OmniFocus"
		set oSubTrees to trees of oTree
		
		if (count of oSubTrees) > 0 then
			set expanded of oTree to blnNewState
			repeat with oSubTree in oSubTrees
				Expand(oSubTree, blnNewState)
			end repeat
		end if
	end using terms from
end Expand

Last edited by RobTrew; 2008-02-27 at 02:30 AM..
 
Here is an updated toggle script for expanding and collapsing the outlines in the contents panel and / or the sidebar panel.

This version should work as well from the OF toolbar as from a script menu or any other launch context. (It bypasses the OF Toolbar's unfortunate failure to save the state of global variable between runs, by saving and retrieving its own script object).

You can adjust its behaviour in two ways:
  1. Editing the boolean values of the two properties at the top will allow you to decide which panel(s) are toggled
  2. If both are toggled, you can decide whether they expand in synch or alternation by temporarily uncommenting one line (see note in script), running the script once, and then commenting that line out again.

Note that the very first time you launch it, it may appear to do nothing (the initial script object values may coincide with the current expansion states). Just run it again, and you should find that it toggles expansion with every run.

Useful ? Well, perhaps ... (I happen to use it) but mainly a simple illustration of how to conserve state between runs (despite the quirks of the OF toolbar) and write a toggling script ...

Code:
-- TOGGLE THE EXPANSION STATE OF TREES IN CONTENT AND/OR SIDEBAR PANELS

property pblnToggleContent : true
property pblnToggleSidebar : true

property pstrObjFile : "OFExpandStates"
property pstrSuffix : ".scpt"
property pstrScriptPath : (path to temporary items folder as string) & ¬
	pstrObjFile & pstrSuffix

-- SCRIPT OBJECT FOR HOLDING DATA BETWEEN RUNS
-- (PATH AS ABOVE)
script sExpandStates
	property pblnContentExpanded : true
	property pblnSidebarExpanded : true
end script

on run
	-- RETRIEVE ANY STORED EXPANSION FLAGS FROM THE LAST USE
	try
		set sExpandStates to load script file pstrScriptPath
	on error
		set pblnContentExpanded to false
		set pblnSidebarExpanded to false
	end try
	
	tell application id "com.omnigroup.omnifocus"
		set oWin to front document window of default document
		
		-- OPTIONALLY TOGGLE CONTENT EXPANSION
		if pblnToggleContent then
			set blnExpanded to not (pblnContentExpanded of my sExpandStates)
			-- UNCOMMENT THE FOLLOWING LINE AND RUN ONCE
			-- TO RESET SYNCH BETWEEN SIDEBAR AND CONTENT EXPANSION
			-- THEN COMMENT AGAIN:
			-- set blnExpanded to true 
			set refTrees to a reference to trees of content of oWin
			set expanded of refTrees to blnExpanded
			set (pblnContentExpanded of my sExpandStates) to blnExpanded
		end if
		
		-- OPTIONALLY TOGGLE SIDEBAR EXPANSION
		if pblnToggleSidebar then
			set blnExpanded to not (pblnSidebarExpanded of my sExpandStates)
			set oSidebar to sidebar of oWin
			set refLibrary to a reference to tree 2 of oSidebar
			set refTrees to a reference to (trees of refLibrary where class of value is folder)
			set expanded of refTrees to blnExpanded
			set (pblnSidebarExpanded of my sExpandStates) to blnExpanded
		end if
	end tell
	store script my sExpandStates in file pstrScriptPath replacing yes
end run

Last edited by RobTrew; 2010-06-15 at 03:58 AM..
 
Quote:
Originally Posted by RobTrew View Post
Mainly illustrative - it works if run from the script menu, but its practical utility is limited by the fact that, (at least as of OF build 98016) static variables like blnNewState below lose their state between runs if the script is run from the OF toolbar.
This has been fixed for the next sneaky peek build (which should be posted within half an hour).
 
Quote:
Originally Posted by Ken Case View Post
This has been fixed for the next sneaky peek build (which should be posted within half an hour).
Excellent !

Thank you Ken, I really appreciate your putting time into this, an enormously welcome change ...

(Can we hope that your work on this will propagate into the toolbar applescript-launching code of OmniOutliner 3 and Omniplan ?)
 
Quote:
Originally Posted by Ken Case View Post
This has been fixed for the next sneaky peek build (which should be posted within half an hour).
From Ver 1.8 sneaky peek 77.56.0.133908 onwards the following simplified and marginally faster script will work from the OF toolbar, as well as from the OF script menu.

Code:
-- TOGGLE THE EXPANSION STATE OF TREES IN CONTENT AND/OR SIDEBAR PANELS
-- Works if launched from Scripts Menu, a script editor
-- or from the OF Toolbar in Ver 1.8 sneaky peek 77.56.0.133908 onwards 

property pblnToggleContent : true
property pblnToggleSidebar : true

property pstrContext : "context"

global gblnContentExpanded
global gblnSidebarExpanded

on run
	tell application id "OFOC"
		set oWin to front document window of default document
		
		-- OPTIONALLY TOGGLE CONTENT EXPANSION
		if pblnToggleContent then
			try
				set blnExpanded to not (gblnContentExpanded)
			on error
				set blnExpanded to false
			end try
			set refTrees to a reference to trees of content of oWin
			set expanded of refTrees to blnExpanded
			set gblnContentExpanded to blnExpanded
		end if
		
		-- OPTIONALLY TOGGLE SIDEBAR EXPANSION
		if pblnToggleSidebar then
			try
				set blnExpanded to not gblnSidebarExpanded
			on error
				set blnExpanded to false
			end try
			
			set oSidebar to sidebar of oWin
			set refLibrary to a reference to tree 2 of oSidebar
			if selected view mode identifier of oWin is not pstrContext then
				set refFolders to a reference to (trees of refLibrary where class of value is folder)
				set expanded of refFolders to blnExpanded
			else
				set refContexts to a reference to (trees of refLibrary where (class of value is context))
				-- There is an assymetry here (arguably a minor bug ?)
				-- Childless context trees can have their expanded property set to FALSE by script
				-- but not to TRUE ...
				if blnExpanded then
					set lstTrees to contents of refContexts
					repeat with oTree in lstTrees
						try
							set expanded of oTree to true
						end try
					end repeat
				else
					set expanded of refContexts to false
				end if
			end if
			
			set gblnSidebarExpanded to blnExpanded
		end if
	end tell
end run

Last edited by RobTrew; 2012-02-15 at 06:09 AM..
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Expand/Collapse All Joseph OmniGraffle General 2 2012-06-21 05:37 PM
OF 1.2 Expand/Collapse all atreinke OmniFocus for iPad 2 2010-11-13 05:06 AM
Expand a little, collapse a little ... RobTrew OmniOutliner 3 for Mac 0 2010-11-04 04:01 AM
Collapse or expand one section only? rogbar OmniOutliner 3 for Mac 1 2009-01-28 03:06 PM
Expand/collapse notes? marieboyer OmniFocus 1 for Mac 2 2008-08-09 09:29 AM


All times are GMT -8. The time now is 10:30 PM.


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