View Single Post
To experiment with placing document-independent tags (from a list in a text file) you could start with a simple draft like the one below.
  • Allows placing of document-independent tags in a Tags column in OO3 documents
  • List of available tags is maintained in an external text file (TextEdit file).
  • One or more tags can be applied at a time.
  • Tags are attached to all rows selected in OO3.
  • The script will create the Tags column (and the tag-list text file, in the same folder as the script) if they are not initially present.

The script displays a simple menu, listing:
  1. The option to edit the tags file in TextEdit
  2. The list of available tags (multiple tags may be selected).

Any tags selected will be placed (avoiding duplication) in the Tags field of any rows selected in the front OO3 document.

Code:
property pVersion : "0.3"
property pTitle : "Place Tags in OO3" & space & "Ver. " & pVersion
property pTagFile : "TagSet.txt"
property pTagColumn : "Tags"
property pEditCommand : "[Edit Tag File]"

-- 	Copyright © 2010, Robin Trew
--  All rights reserved.
-- 	
-- 	Redistribution and use in source and binary forms, with or without modification, 
-- 	are permitted provided that the following conditions are met:
-- 	
-- 		- Redistributions of source code must retain the above copyright notice, 
-- 		  this list of conditions and the following disclaimer.
-- 		- Redistributions in binary form must reproduce the above copyright notice, 
-- 		  this list of conditions and the following disclaimer in the documentation 
-- 		  and/or other materials provided with the distribution.
-- 		
-- 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
-- 	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
-- 	THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
-- 	IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
-- 	ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-- 	 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-- 	 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
-- 	 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
-- 	 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-- Allows placing of document-independent tags in a Tags column in OO3 documents
-- List of tags is maintained in an external text file (TextEdit file). One tag per line.
-- The script will create the Tags column (and the tags file) if they are not initially present.

-- The script displays a simple menu, listing:
-- 1. The option to edit the tags file in TextEdit
-- 2. The list of available tags (multiple tags may be selected).

-- Any tags selected will be placed in the Tags field of any rows selected in the front OO3 document.

on run
	tell application id "com.omnigroup.OmniOutlinerPro3"
		-- ensure that at least one row is selected
		try
			set oDoc to front document
		on error
			return
		end try
		tell oDoc
			set lstRows to selected rows
			if (count of lstRows) < 1 then
				display dialog "Select one or more rows, and try again." buttons {"OK"} with title pTitle
				return
			end if
			
			-- choose tag(s)
			set lstChosen to my ChooseTags()
			if (count of lstChosen) < 1 then return
			
			-- ensure that there is a tag column in the front document
			try
				set oCol to (first column where (name is pTagColumn) and (type is rich text))
			on error
				set oCol to make new column at end of columns with properties {name:pTagColumn, type:rich text}
			end try
			set idCol to id of oCol
		end tell
		
		-- add tag(s) to selected rows (avoiding any duplication of tags in a given row)
		repeat with oRow in lstRows
			set refText to (a reference to text of cell id idCol of oRow)
			set blnFirst to length of (refText as string) is 0
			repeat with oTag in lstChosen
				if blnFirst or (refText does not contain oTag) then
					set strData to oTag
					if blnFirst then
						set blnFirst to false
					else
						set strData to space & strData
					end if
					make new word at end of refText with data strData
				end if
			end repeat
		end repeat
	end tell
end run

on ChooseTags()
	set lstTags to GetTags()
	if (count of lstTags) < 1 then
		set strPath to GetPath()
		display dialog "No tags found in:" & return & return & strPath buttons {"OK"}
		tell application id "com.apple.TextEdit"
			open strPath as POSIX file
			activate
		end tell
		{}
	else
		set lstOptions to {pEditCommand} & lstTags
		set varChoice to choose from list lstOptions default items item 1 of lstTags with title my pTitle ¬
			with prompt "(Cmd-click for multiple selections)" & return & return & "Choose Tag(s):" with multiple selections allowed
		if varChoice is not false then
			if varChoice contains pEditCommand then
				set strPath to GetPath()
				tell application id "com.apple.TextEdit"
					open strPath as POSIX file
					activate
				end tell
				{}
			else
				varChoice
			end if
		else
			{}
		end if
	end if
end ChooseTags

on GetPath()
	set text item delimiters to ":"
	set strPath to (items 1 thru -2 of (text items of (path to me as string)) & {pTagFile}) as string
	set text item delimiters to space
	POSIX path of strPath
end GetPath

on GetTags()
	set strPath to GetPath()
	set lstTags to paragraphs of readFile(strPath)
end GetTags

on readFile(unixPath)
	set foo to (open for access (POSIX file unixPath))
	set txt to (read foo for (get eof foo))
	close access foo
	return txt
end readFile

Last edited by RobTrew; 2010-09-27 at 06:23 AM.. Reason: ver 0.3 checks that any tag column is of the right type