A couple of AppleScripts...

Gregory Ramsperger omnimail at jitjat.com
Fri Jan 31 17:00:42 PST 2003


Hello-

I am new to the list but noticed, when I was searching the archive, 
that somebody had asked for any useful scripts. I have two that I think 
someone may find useful. I have posted them at 
http://www.jitjat.com/OmniOutlinerScripts.sit with an example file 
included.

The first script simply opens one of your OmniOutliner script folders 
(allowing you to choose which if you have more than one).

The second sums a column of numbers and places the total at the end. 
The trick to it is that it only includes items that are checked. The 
archive I mentioned above includes a sample file for  you to run this 
one against.

-Gregory


Here is the text for the "Open Scripts Folder" (you may want to use the 
one in the SIT file may be a better bet since it won't be wrapped):

tell application "Finder"
	activate
	
	set dirList to {}
	
	set userPath to (home as text) & "Library:Application 
Support:OmniOutliner:Scripts:"
	try
		folder userPath
		set the end of dirList to "User"
	end try
	
	set localPath to (startup disk as text) & "Library:Application 
Support:OmniOutliner:Scripts:"
	try
		folder localPath
		set the end of dirList to "Local"
	end try
	
	set networkPath to (startup disk as text) & 
"Network:Library:Application Support:OmniOutliner:Scripts:"
	try
		folder networkPath
		set the end of dirList to "Network"
	end try
	
	set chosen to ""
	if (count of dirList) is 0 then
		display dialog "No Scripts folders exist." buttons {"OK"} default 
button 1
	else if (count of dirList) is 1 then
		set chosen to item 1 of dirList
	else
		set chosen to choose from list dirList with prompt "Which scripts 
folder would you like to open?"
		if chosen is false then return "user canceled"
	end if
	
	if chosen as text is "User" then
		open folder userPath
	else if chosen as text is "Local" then
		open folder localPath
	else if chosen as text is "Network" then
		open folder networkPath
	end if
end tell




And here is the text of the totals script:


property totalColIndex : 3
property subTotalColor : {32767, 32767, 32767}

tell application "OmniOutliner"
	activate
	global theDoc
	set theDoc to document of window 1
	
	
	set colTitlesList to {}
	repeat with col in every column of theDoc
		set the end of colTitlesList to the title of col
	end repeat
	
	set displayedTitlesList to {}
	
	-- make a list of all number-valued columns based on the first row
	-- since the first cell is the notes cell, start at 2
	repeat with i from 2 to count of colTitlesList
		try
			-- use addition to test that the value of the column is a number
			set temp to (text of cell i of the first child of theDoc) + 1
			-- if there was not an error making the cell text into a number
			-- add it to the list to display.
			-- Note: empty cells recieve a 0 value and show up as numbers.
			set the end of displayedTitlesList to item i of colTitlesList
		end try
	end repeat
	
	-- prompt the user for the column to total. If there is just one, 
verify
	if (count of displayedTitlesList) is greater than 1 then
		set choice to choose from list displayedTitlesList with prompt 
"Select the column to total"
		if choice is false then return "user canceled"
	else
		set choice to item 1 of displayedTitlesList
		set temp to display dialog "Total column: " & choice buttons 
{"Cancel", "Total"} default button 2
		if button returned of temp is "Cancel" then return "user canceled"
	end if
	
	-- get the index of the selected column
	set i to 0
	repeat with i from 1 to count of colTitlesList
		if (item i of colTitlesList) as text is (choice as text) then
			set totalColIndex to i
			exit repeat
		end if
	end repeat
	
	try
		-- sum the items
		set theTotal to my sumChildren(theDoc)
		
		--if it does not exist, create a "Totals" row
		if text of cell 2 of last row of theDoc is not "Totals" then
			make new row at end of rows of theDoc
			set totalRow to the last row of theDoc
			set the state of totalRow to none
			set the text of cell 2 of totalRow to "Totals"
			bold the text of cell 2 of totalRow
		end if
		
		-- set the value of the total at the bottom of the column totaled
		set the text of cell totalColIndex of the last row of theDoc to 
(theTotal as text)
		bold the text of cell totalColIndex of the last row of theDoc
		italicize the text of cell totalColIndex of the last row of theDoc
	on error
		my alert("Unable to total column: " & (item totalColIndex of 
colTitlesList))
	end try
end tell

(*
	Get the sum of all items that are children of the given element.
	This only adds items which have a "checked" state.
  *)
on sumChildren(theElement)
	local theSum
	local theChildren
	local theChild
	local thisValue
	tell application "OmniOutliner"
		set theSum to 0
		set theChildren to every child of theElement
		repeat with theChild in theChildren
			-- don't count children that are not checked
			if (the state of theChild) is in {indeterminate, checked} then
				-- if the element has children, sum them and set the  value of in 
the col
				if (count of every child of theChild) is greater than 0 then
					set thisValue to my sumChildren(theChild)
					set the text of cell totalColIndex of theChild to "" & thisValue
					italicize the text of cell totalColIndex of theChild
					bold the text of cell totalColIndex of theChild
					set color of the text of cell totalColIndex of theChild to 
subTotalColor
					set theSum to theSum + thisValue
				else
					set theSum to theSum + (the text of cell totalColIndex of theChild)
				end if
			end if
		end repeat
		return theSum
	end tell
end sumChildren

on alert(str)
	tell application "OmniOutliner"
		display dialog str buttons {"OK"} default button 1
	end tell
end alert




More information about the OmniOutliner-Users mailing list