Thread: View By Level?
View Single Post
Here is a version which can optionally be used with Launchbar.

You can pass a number to it by selecting the script in Launchbar and tapping the space bar to get an entry field.
  • Simple integers will be read as an absolute level of expansion
  • Numbers prefixed by + or - will be interpreted as relative adjustments
  • A simple + will be read as +1 (expand another level)
  • - on its own will be read as -1 (collapse another level)

(This version - unlike the one in the 'Useful Scripts' download - avoids collapsing any hidden levels at deeper levels of nesting, in case reopening them all manually slows thing down during the expansion of particular subtrees).

Code:
property pTitle : "Expand OO3 doc to level N"
property pVer : "0.03"

-- WORKS WITH LAUNCHBAR,
-- give an absolute expansion level, or an adjustment like +1 -2 etc 
-- (a simple + or - is interepreted as +1 or -1)
on handle_string(strLevel)
	tell application id "OOut"
		set lstDocs to documents
		if length of lstDocs < 1 then return
		my ShowLevel(strLevel, item 1 of lstDocs)
	end tell
end handle_string

-- OR WITHOUT LAUNCHBAR ...
on run
	tell application id "OOut"
		set lstDocs to documents
		if length of lstDocs < 1 then return
		set oDoc to item 1 of lstDocs
		
		set lngMax to count of level styles of oDoc
		activate
		set varLevel to (display dialog "Level to expand to (1-" & lngMax & ")" & return & return & ¬
			"(or amount to adjust expansion by: +1 -2 etc)." default answer lngMax buttons {"Cancel", "OK"} ¬
			cancel button "Cancel" default button "OK" with title pTitle & "  ver. " & pVer)
		set varLevel to text returned of varLevel
		my ShowLevel(varLevel, oDoc)
	end tell
end run

-- What is the level of the highest node which is currently collapsed ?
on GetLevel(oDoc, lngMax)
	tell application id "OOut"
		tell oDoc
			set lngMin to lngMax
			set lstLevels to level of rows where expanded = false and has subtopics = true
			if length of lstLevels = 0 then return lngMax
			repeat with oLevel in lstLevels
				if oLevel < lngMin then set lngMin to contents of oLevel
			end repeat
			return lngMin
		end tell
	end tell
end GetLevel

-- Set an absolute expansion level, or adjust the expansion level by a delta
on ShowLevel(strLevel, oDoc)
	tell application id "OOut"
		set blnPlus to strLevel contains "+"
		set blnMinus to strLevel contains "-"
		set blnDelta to (blnPlus or blnMinus)
		try
			set lngValue to strLevel as integer
		on error
			if not blnDelta then
				activate
				display dialog strLevel & " could not be interpreted as a level or adjustment" buttons {"OK"} default button "OK" with title pTitle & "  ver. " & pVer
				return
			else
				if blnMinus then
					set lngValue to -1
				else
					set lngValue to 1
				end if
			end if
		end try
		
		tell oDoc
			set lngMax to count of level styles
			lngValue
			if blnDelta then
				set lngLevel to (my GetLevel(oDoc, lngMax)) + lngValue
			else
				set lngLevel to lngValue
			end if
			if lngLevel > lngMax then set lngLevel to lngMax
			if lngLevel > 0 then
				set expanded of rows where level < lngLevel to true
				set expanded of rows where level = lngLevel to false
			else
				set expanded of rows to false
			end if
		end tell
	end tell
end ShowLevel

Last edited by RobTrew; 2012-10-22 at 07:34 AM..