View Single Post
Quote:
Originally Posted by fudster View Post
--should also check to make sure they're projects and not tasks
The simplest approach is probably to filter the selected trees with a where clause. Something, for example, like:

Code:
tell application "OmniFocus"
	tell front document
		-- GET SELECTED PROJECTS
		tell front document window
			repeat with oPanel in {content, sidebar}
				set refProjects to (a reference to (selected trees of oPanel where class of value is project))
				if (count of refProjects) > 1 then exit repeat
			end repeat
		end tell
		if (count of refProjects) < 2 then 
			display dialog "more than two projects must be selected in order to set a dependency" buttons {"OK"} cancel button "OK"
		
		--   CHOOSE ACTIVE PROJECT
		set lstNames to name of refProjects
		set varResponse to {choose from list (lstNames) with prompt "Choose the project that should remain active (all others will be placed on hold):"}
		if varResponse = false then return
		set strActive to varResponse as string
		set oActive to first flattened project where name = (strActive)
		set status of oActive to active
		
		-- SET OTHERS ON HOLD AND MAKE LINKS
		set lstOnHold to value of (selected trees of oPanel where (class of value is project) and (name ≠ strActive))
		repeat with oProj in lstOnHold
			set refClip to (a reference to the clipboard)
			tell oProj
				set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & "omnifocus:///task/" & id & "\">" & name & "</a></font><p>")
				do shell script "echo " & strHTML & "  | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
				tell oActive to set oTask to (make new task with properties {name:"activate project: " & name of oProj})
				my PasteToNote(oTask)
				
				set status to on hold
				set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & "omnifocus:///task/" & id of oActive & "\">" & name of oActive & "</a></font><p>")
				do shell script "echo " & strHTML & "  | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
				
				set oTask to (make new task at beginning with properties 
					{name:"this project on hold pending completion of: " & name of oActive})
				my PasteToNote(oTask)
			end tell
		end repeat
	end tell
end tell

on PasteToNote(oTask)
	tell application id "com.omnigroup.OmniFocus"
		tell content of front document window of front document to select {oTask}
		activate
	end tell
	tell application id "com.apple.systemevents"
		keystroke "'" using {command down} -- Edit note
		keystroke "v" using {command down} -- Paste
	end tell
end PasteToNote

Last edited by RobTrew; 2011-02-12 at 03:09 AM.. Reason: Amended code to allow for more legible links between projects