View Single Post
An alternative version which asks for one or more initial letters, and then displays only tags which start with those letters.

The initial characters can be given straight to LaunchBar (preceded by a tap on the space bar, once the script has been selected).

If the script is not launched from LaunchBar, it will put up a text dialog and request the starting characters.

[This version probably only helpful if the tag list is very large]

Code:
property pVersion : "0.4"
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 handle_string(strChars)
	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
		return
	else
		set lstTags to FilterList(lstTags, strChars)
		
		-- DISPLAY THE MENU
		set lstOptions to {pEditCommand} & lstTags
		if length of lstTags > 0 then
			set strDefault to item 1 of lstTags
		else
			set strDefault to pEditCommand
		end if
		set varChoice to choose from list lstOptions default items strDefault with title my pTitle ¬
			with prompt "(Cmd-click for multiple selections)" & return & return & "Choose tag(s) starting with " & quote & strChars & quote 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
				set lstChosen to {}
			else
				set lstChosen to varChoice
			end if
		else
			set lstChosen to {}
		end if
		
		if lstChosen is not {} then AddTags(lstChosen)
	end if
end handle_string

on run
	set var to display dialog "Tag(s) starting with:" default answer ""
	set strChars to text returned of var
	handle_string(strChars)
end run

on AddTags(lstChosen)
	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
			
			-- 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 AddTags


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

on FilterList(lstTags, strChars)
	if strChars is "" then return lstTags
	set lst to {}
	repeat with oTag in lstTags
		if oTag begins with strChars then set end of lst to oTag as string
	end repeat
	lst
end FilterList

Last edited by RobTrew; 2010-09-28 at 10:52 AM.. Reason: Mention that string parameters can be fed to Launchbar