content: -- COPY OMNIGRAFFLE SELECTION(S) (WITH SUBTREES) AS TAB-INDENTED TEXT
  
  property pTitle : "Copy OmniGraffle subtree as indented text"
  property pVer : "0.01"
  property pAuthor : "Robin Trew"
  
  property piHoriz : 1
  property piVert : 2
  property plstSeen : {} -- Keep track of which nodes have been read, to avoid cyclical duplication
  
  on run
  	tell application id "OGfl"
  		if (count of documents) < 1 then return
  		if (count of windows) < 1 then return
  		
  		tell front window
  			tell its canvas
  				tell its layout info
  					set enDirn to direction
  					if enDirn = top to bottom or enDirn = bottom to top then
  						set iAxis to piHoriz
  					else
  						set iAxis to piVert
  					end if
  				end tell
  			end tell
  			
  			set lstSeln to selection
  			set lstRoot to {}
  			set plstSeen to {}
  			repeat with oSeln in lstSeln
  				if class of oSeln is shape then
  					set end of plstSeen to id of oSeln
  					set end of lstRoot to {oSeln, my GetChildren(oSeln, iAxis)}
  				end if
  			end repeat
  		end tell
  	end tell
  	
  	set strTree to WriteAsTabIndented(lstRoot, "")
  	set the clipboard to strTree
  end run
  
  -- GET THE CHILDREN OF THE NODE IN THEIR LEFT RIGHT (OR TOP DOWN) SEQUENCE
  -- (The OG Applescript library doesn't expose outline structure,
  --	 so we have to assume that Automatic Layout has been run, so that we can read the sibling sequence spatially)
  on GetChildren(oParent, iAxis)
  	tell application id "OGfl"
  		-- Create a textual list of fields ID + horiz or vert position
  		set {lstOrigin, lstID} to {origin, id} of (destination of outgoing lines of oParent)
  		
  		set strRows to ""
  		set lngID to length of lstID
  		repeat with i from 1 to lngID
  			set strRows to strRows & (item iAxis of (item i of lstOrigin)) & tab & item i of lstID & linefeed
  		end repeat
  		
  		if strRows ≠ "" then
  			set lstID to paragraphs of (do shell script "echo " & quoted form of (texts 1 thru -2 of strRows) & " | sort -n | cut -f 2")
  			
  			set lstChiln to {}
  			tell canvas of oParent
  				repeat with strID in lstID
  					set lngID to strID as integer
  					if not (plstSeen contains lngID) then
  						
  						set end of plstSeen to lngID
  						set oChild to shape id lngID
  						set end of lstChiln to {oChild, my GetChildren(oChild, iAxis)}
  					end if
  				end repeat
  			end tell
  			return lstChiln
  		else
  			return {}
  		end if
  	end tell
  end GetChildren
  
  on WriteAsTabIndented(lstTree, strIndent)
  	set str to ""
  	tell application id "OGfl"
  		repeat with oNode in lstTree
  			set {oGraphic, lstChiln} to oNode
  			set str to str & strIndent & "- " & text of oGraphic & linefeed
  			if length of lstChiln > 0 then ¬
  				set str to str & my WriteAsTabIndented(lstChiln, strIndent & tab)
  		end repeat
  	end tell
  	return str
  end WriteAsTabIndented
  
  