View Single Post
Thanks for these great scripts! I propose a “streamlined” version with an added feature: for each project, a link is created in the Finder that points at the corresponding project in OF.

This modified version of the "Project Folder" script does not ask any questions (by default) and automatically creates an OF link and an OO document for the selected project.

Note that my Projects folder in the Finder resides under a Reference folder in the Documents folder: you may want to change the value of the variable projectsDir to suit your needs. Other customizations are possible: take a look at the properties at the beginning of the scripts.

Code:
--Project Folder
--Written by Jim Harrison, Dec 2008 (jhh.med.virginia.edu).
--Modified by Nicola Vitacolonna, Sep 2009
--May be used, edited and redistributed without restriction.
--Opens folders at a specified location that have the same name as an OmniFocus project that contains the selection
--Edit the name and path below to correspond to the location of the main Projects folder that will contain the individual project folders
set projectsFolderName to "Projects" -- name for projects folder (change this as you like)
set projectsDir to (path to documents folder as text) & "Reference" & ":" -- Path to projects folder (change this as you like)
set askForConfirmation to false -- Set to true if you want the script to ask for confirmation before creating new folders
set linkPrefix to "• [OF] " -- change this as you like
set ooPrefix to "• " -- change this as you like
set ooSuffix to " notes" -- change this as you like
--Edit the name and file extension below if you wish to use another application to create and edit the project notes files
set ooExtension to ".oo3"
set linksInAllFolders to false -- Set to true if you want an OF link in every folder (not only for projects)
set createOODocument to true -- Set to false if you do not want an OO document to be created for each project
--------------------------------------------------------------------------------------------------------------------------

tell front window of application "OmniFocus" -- get the name of the project containing the current selection
	try
		set theTrees to the selected trees of content
		if the (count of theTrees) is less than 1 then
			set theTrees to the selected trees of sidebar
		end if
		if the (count of theTrees) is less than 1 then
			display dialog "To open a project folder, click on or in a project, task or note before running this script"
			return
		end if
		set theSelection to value of item 1 of theTrees
		if the class of theSelection is folder then
			set thisProjList to {the name of theSelection}
			set theGroup to the container of theSelection
			set theLinkList to {"omnifocus:///folder/" & the id of theSelection}
			set isProjectFolder to false
		else
			set thisProjList to {the name of the containing project of theSelection}
			set theGroup to the container of the containing project of theSelection
			set theLinkList to {"omnifocus:///task/" & the id of the containing project of theSelection}
			set isProjectFolder to true
		end if
		repeat while the class of theGroup is not document
			set thisProjList to {the name of theGroup} & thisProjList
			set theLinkList to {"omnifocus:///folder/" & the id of theGroup} & theLinkList
			set theGroup to the container of theGroup
		end repeat
	on error
		display dialog "Click on or in a project, task or note before running this script." buttons {"OK"} default button 1
		return
	end try
end tell

set oldDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set thisProjPath to thisProjList as text
set AppleScript's text item delimiters to oldDelimiter

tell application "Finder"
	activate
	try
		open folder (projectsDir & projectsFolderName & ":" & thisProjPath & ":")
	on error
		try
			if not (folder (projectsDir) exists) then
				display dialog "Please create the folder " & projectsDir & " and try again."
				return
			end if
			if askForConfirmation then
				set answer to display dialog "Some folders do not exist yet. Should I create them?" buttons {"Cancel", "OK"} default button 1
				if the button returned of answer is "Cancel" then return
			end if
			
			set theCurrentPath to projectsDir & projectsFolderName & ":"
			if not (folder (theCurrentPath) exists) then
				make new folder at projectsDir with properties {name:projectsFolderName}
			end if
			
			set theParentPath to theCurrentPath
			repeat with i from 1 to (count thisProjList) - 1
				set theFolder to item i of thisProjList
				-- Invariant: at this point theParentPath exists and it is equal to theCurrentPath
				set theCurrentPath to theCurrentPath & theFolder & ":"
				if not (folder (theCurrentPath) exists) then
					make new folder at alias (theParentPath) with properties {name:theFolder}
					if linksInAllFolders then
						make new internet location file to item i of theLinkList at alias (theCurrentPath) with properties {name:linkPrefix & theFolder}
					end if
				end if
				set theParentPath to theCurrentPath
			end repeat
			-- Last folder
			set theFolder to the last item of thisProjList
			set theLink to the last item of theLinkList
			set theCurrentPath to theCurrentPath & theFolder & ":"
			if not (folder (theCurrentPath) exists) then
				make new folder at alias (theParentPath) with properties {name:theFolder}
				if isProjectFolder or linksInAllFolders then
					make new internet location file to theLink at alias (theCurrentPath) with properties {name:linkPrefix & theFolder}
				end if
				if isProjectFolder and createOODocument then
					set projNotesName to ooPrefix & theFolder & ooSuffix & ooExtension
					tell application "OmniOutliner"
						set notesDoc to make new document with properties {name:projNotesName}
						save notesDoc in theCurrentPath & projNotesName
						close notesDoc
					end tell
				end if
			end if
			open folder (theCurrentPath)
		on error
			return
		end try
	end try
end tell
The modified version of the "Project Notes" can be used to open an existing notes document for a project or to create a new one, provided that the project folder already exists (otherwise a warning is displayed). This script also fixes the bug mentioned in previous posts (the new OO document is not the same object as the one saved on disk).

Code:
--Project Notes
--Written by Jim Harrison, Dec 2008 (jhh.med.virginia.edu).
--Modified by Nicola Vitacolonna, Sep 2009
--May be used, edited and redistributed without restriction.
--Opens folders at a specified location that have the same name as an OmniFocus project that contains the selection
--Edit the name and path below to correspond to the location of the main Projects folder that will contain the individual project folders
set projectsFolderName to "Projects" -- name for projects folder (change this as you like)
set projectsDir to (path to documents folder as text) & "Reference" & ":" -- Path to projects folder (change this as you like)
set askForConfirmation to false -- Set to true if you want the script to ask for confirmation before creating new documents
set ooPrefix to "• " -- change this as you like
set ooSuffix to " notes" -- change this as you like
--Edit the name and file extension below if you wish to use another application to create and edit the project notes files
set ooExtension to ".oo3"
--------------------------------------------------------------------------------------------------------------------------

tell front window of application "OmniFocus" -- get the name of the project containing the current selection
	try
		set theTrees to the selected trees of content
		if the (count of theTrees) is less than 1 then
			set theTrees to the selected trees of sidebar
		end if
		if the (count of theTrees) is less than 1 then
			display dialog "To open a project folder, click on or in a project, task or note before running this script"
			return
		end if
		set theSelection to value of item 1 of theTrees
		if the class of theSelection is folder then
			set thisProjList to {the name of theSelection}
			set theGroup to the container of theSelection
		else
			set thisProjList to {the name of the containing project of theSelection}
			set theGroup to the container of the containing project of theSelection
		end if
		repeat while the class of theGroup is not document
			set thisProjList to {the name of theGroup} & thisProjList
			set theGroup to the container of theGroup
		end repeat
	on error
		display dialog "Click on or in a project, task or note before running this script." buttons {"OK"} default button 1
		return
	end try
end tell

set oldDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set thisProjPath to thisProjList as text
set AppleScript's text item delimiters to oldDelimiter

set projNotesName to ooPrefix & last item of thisProjList & ooSuffix & ooExtension
set theCurrentPath to projectsDir & projectsFolderName & ":" & thisProjPath & ":"
tell application "Finder"
	activate
	try
		open alias (theCurrentPath & projNotesName)
	on error
		try
			open folder (theCurrentPath)
		on error
			display dialog "Please create the project folder first, then try again." buttons {"OK"}
			tell application "OmniFocus" to activate
			return
		end try
		-- If the folder exists, create an OO document in it
		try
			if askForConfirmation then
				set answer to display dialog "There are no notes in " & theCurrentPath & ". Should I create them?" buttons {"Cancel", "OK"} default button 1
				if the button returned of answer is "Cancel" then return
			end if
			tell application "OmniOutliner"
				activate
				set notesDoc to make new document with properties {name:projNotesName}
				save notesDoc in theCurrentPath & projNotesName
				-- BUG: the saved document does not seem to be the same object as the new document
				-- We overcome this problem by closing and re-opening the document
				-- See also http://forums.omnigroup.com/showthread.php?t=8356
				close notesDoc
				open theCurrentPath & projNotesName
			end tell
		on error
			tell application "OmniFocus" to activate
			return
		end try
	end try
end tell