Script to create new outline with selection, keeping context

Curt Clifton curt.clifton at mac.com
Sun Jan 22 12:06:02 PST 2006


After an off-list conversation with Anders Baden Nielsen, I developed  
the attached script.  It's a version of one I posted earlier for  
making a new outline from the current selection.  The previous script  
copied selected rows and their children to a new outline.  This  
version copies selected rows and their parents, but omits unselected  
children.  For example:

On Jan 21, 2006, at 2:32 PM, Anders Baden Nielsen wrote:

> Yes, exactly. The parents are preserved (Parent 1 and 3). But,  
> also : suppose Child 1,2 and 6 did have their own children, none of  
> those would be copied to the new outline. Like this:
>
> - Parent 1
>     - Child 1 *
>     - Child 2 *
> 	- Subchild 1
> - Parent 2
>     - Child 3
> 	- Subchild 2
>     - Child 4
> 	- Subchild 3
> - Parent 3
>     - Child 5
>     - Child 6 *
> 	- Subchild 4
>
> becomes
>
> - Parent 1
>     - Child 1 *
>     - Child 2 *
>
> - Parent 3
>     - Child 6 *
>
> So all the subchildren are removed.

You can think of the previous script as preserving details of the  
selection, while this version preserves context.

Share and enjoy,

Curt


(*
	Creates a new outline from the selected rows of the front document,  
ignoring
	the selected rows' children but keeping their parents for context.   
Assumes that the
	topic and note are in columns 1 and 2 as in the default OO template.
	by Curtis Clifton
	Copyright © 2006, Some Rights Reserved
*)

property mark : "keep"
-- generates a random column name for use in marking rows that  
shouldn't appear in final document
set markerColumnName to do shell script "jot -c 16 a z | rs -T -g0"

tell application "OmniOutliner Professional"
	-- Uses document name in tell block so references are absolute
	set docName to name of front document
	tell (document docName)
		-- Skips operation if no rows selected
		if (count of selected rows) is 0 then
			beep
			return
		end if
		
		set theOriginalSelectedRows to selected rows
		-- Creates a column for marking rows that should remain in the  
created outline
		make new column at after second column with properties  
{name:markerColumnName}
		select theOriginalSelectedRows
		
		-- adds all the parent rows recursively so that all the context is  
in the selection
		set oldSelectionSize to 0
		repeat until (oldSelectionSize is (count of selected rows))
			set oldSelectionSize to (count of selected rows)
			-- if the following expands the selection, then the loop will  
repeat			
			select parent of selected rows with extending
		end repeat
		
		set value of cell markerColumnName of selected rows to mark
		
		-- only need to copy the top-most rows, they'll bring their kids along
		select (expel descendants (get selected rows))
		set rowsToCopy to a reference to selected rows
		
		set theName to "Selected Rows from " & name
		make new document at before it with properties {name:theName}
		-- fixme: Make columns of new document match the source document
	end tell
	
	tell first document
		make new column at after second column with properties  
{name:markerColumnName}
	end tell
	duplicate rowsToCopy to after last row of first document
	tell first document
		-- Deletes the extra children and siblings that we didn't really  
want to copy
		delete (every row whose value of cell markerColumnName is not mark)
		delete column markerColumnName
	end tell
	
	-- Restores the state of the original document	
	tell (document docName)
		delete column markerColumnName
		select (theOriginalSelectedRows)
	end tell
end tell



More information about the OmniOutliner-Users mailing list