Integration of Devonthink Pro with Omnifocus 2 (Rob Trew)

I was using the scripts Rob Trew wrote for integrating Omnifocus 1 with Devonthink Pro (OpenProjFolderInDevn.scptd, OpenProjNOTESInDevn214.scptd). Now with the iPhone/iPad version of DTTG I want to switch again from Evernote to Devonthink for my reference database.

I am currently working with Omnifocus 2 (2.6.1) and the scripts aren’t working with DTPO version 2.9.1

After first solving the database path issue I get an error on "cannot access “set «class FCvm» to “project””. The scripts are here: github.com/RobTrew/tree-tools/t … %20scripts

Unfortunately I cannot solve the problem. I have contacted Rob Trew and he is indicating this is an OF 1 script, and that the OF 2 object model is different.

Is the anybody out there who can help or has the scripts working?

Help is deeply appreciated because I want to use both wonderful products together.

I’m actually looking at these pretty recently and if I come up with anything remotely relevant I’ll surely post here. A huge problem you’ll likely encounter in many older projects is that a lot of them rely on ruby’s AppScript and it is deprecated and relies on further deprecated and abandoned components that are very difficult to get rolling even if you explicitly use filthy vulnerable ancient ruby via rvm.

The best starting point for understanding the ofocus format is this unofficial but defacto official doc: github.com/tomzx/ofocus-format

Another point of interest — https://github.com/psidnell/ofexport2

It’s Java, but works as intended for transmogrifying OmniFocus data into other formats. I use it to generate a feed out of a couple of projects, some MindMaps of others, and Taskpaper-formatted text files for “worklog” documentation, etc.

Is there an updated script which works with current versions of DT/OF?

Bill

This is not a DEVONtech script. You’ll need to ask Rob. You can DM him on Twitter @complexpoint.

The next release will include an improved script for OmniFocus.

I personally haven’t used Omnifocus for a while, and don’t have Omnifocus 2 on my system, so I’m afraid that I haven’t needed or been able to update that for myself.

Good to hear that DEVON has something in their pipeline.

Rob

Can you share with us when and what it will be?

The script will support custom dates, v2.9.12 will be probably available in the first half of June.

Is there somebody out there who can help adjusting the scripts?

Adjusting the scripts for what?

Also, note that tweaking someone’s code is often harder than rolling your own. Just something to consider.

Maybe we can do this in pieces. Here’s a hack of Rob’s script that will create a project in DTPO according to the tree in OF2.

Edit: code improved by unlocked2412 on OmniFocus forum https://discourse.omnigroup.com/t/integrating-omnifocus-2-with-devonthink-pro-rob-trew-scripts/26100/6 to paste DTPO group link in note of OmniFocus project.

property pblnJustFolder : true
property pblnUseSyncAsRoot : false
-- Robin Trew 
-- ver .204 June 19 2011
--	Aug 22 2011
-- Ver .208 Still defaults to the DT Database named in pstrDTDB (below)
--			but can use different DT databases for different OF folders
--			(will use the first existing DT database whose POSIX path is found in the note of an enclosing OmniFocus folder
--			[a script for editing OF folder notes can be found at 
--			http://forums.omnigroup.com/showthread.php?t=21942]
-- ver .211 Sep 12 2011
--			Adds option, above - top of script, to make the Sync group the root of all new folders
-- Disclaimer
-- This is just a rough draft of something which I have sketched for my own personal use, 
-- and which is provided purely as an illustration of possible approaches to coding.
-- You are free to adapt and reuse any part of it, without any warranties, implied
-- or explicit, as to its behaviour or suitability for use
-- IF property pblnJustFolder : **false**  (see top of script)
-- CREATES/OPENS A PROJECT NOTES FILE (STORED IN DEVONthink 2) FOR THE SELECTED OMNIFOCUS PROJECT.
-- 1.	The project notes file is an OmniOutliner 5 document stored in DEVONthink 2 folder
-- 2.	The Devonthink record contains a hyperlink back to the OmniFocus project
-- 2.	The script ensures that a DT hyperlink to the document in DevonThink is placed in the note field of the project
-- 3.	In the absence of a link to existing notes in DevonThink, 
--		the script will seek or create the OO5 file in a DevonThink folder which matches 
--		the folder path of the project in OmniFocus 
--		(and add the hyperlinks from project to notes, and from folder and notes to project)
-- OR - IF property pblnJustFolder : **true** (see top of script)
-- JUST CREATES/OPENS A PROJECT MATERIALS FOLDER (IN DEVONthink 2) 
-- FOR THE SELECTED OMNIFOCUS PROJECT.
-- 1.	The script ensures that a DT hyperlink to the document in DevonThink is placed in 
--		the note field of the project
-- 2.	In the absence of a link to an existing folder in DevonThink, 
--		the script will add the hyperlinks from project to folder, and from folder to project)
-- Acknowledgements
-- Inspired by Jim Harrison's excellent scripts, which use the Finder rather than DEVONthink 2
-- http://jhh.med.virginia.edu/main/OmniFocusScripts
-- The icon attached to this file is from the Float collection by Corey Marion
-- http://iconfactory.com/freeware/preview/flot
-- GLOBAL CONSTANTS
-- Initially assumes that project folders will be maintained in 
-- a database named [UserName]/Documents/Omnifocus Notes.
-- Edit the name and path below to change the location and/or name 
--of the main Projects folder that will contain the individual project folders
property pstrDTDB : "OmniFocus Notes" -- name of default Devonthink Database
property pstrDTsuffix : ".dtBase2"
property pstrDocsPath : (path to documents folder as string) -- path to ~/Documents
property pstrSync : "/Sync"
property pstrOO5Template : "Default"
property pstrOO5Suffix : ".ooutline"
property pstrOFPrefix : "omnifocus:///task/"
property pstrXMLPrefix : "<value key=\"link\">"
property pstrRunDelim : "</lit></run>"
property pstrDTPrefix : "x-devonthink-item://"
property pstrDBPath : "$HOME/Library/Containers/com.omnigroup.OmniFocus2/Data/Library/Caches/com.omnigroup.OmniFocus2/OmniFocusDatabase2"
--property pstrDBPath : "/Users/detach/Library/Containers/com.omnigroup.OmniFocus2.MacAppStore/Data/Library/Caches/com.omnigroup.OmniFocus2.MacAppStore/OmniFocusDatabase2"
property pstrFolderLinkTitle : "[Devonthink folder for this project]"
property pstrNoteLinkTitle : "[Devonthink ooutline notes]"
property plngURLchars : 36
on run
	-- IS A PROJECT (OR ONE OF ITS TASKS) SELECTED IN OMNIFOCUS ?
	set {oProject, strProjName, strProjID} to GetSeldProject()
	if oProject is missing value then return
	-- IS THERE A RELEVANT LINK IN THE NOTE FIELD OF THE PROJECT ?
	set {strFolderURL, stroo5URL} to DTLinksInNote(oProject)
	-- AND IF SO, DOES IT LEAD ANYWHERE ?
	if pblnJustFolder then
		if strFolderURL ≠ "" then if FollowDTLink(strFolderURL) then return
	else
		if stroo5URL ≠ "" then if FollowDTLink(stroo5URL) then return
	end if
	-- IN THE ABSENCE OF A LIVE DT LINK, CREATE OR FIND A MATCHING FOLDER PATH IN DT
	tell application id "DNtp"
		set oDTFolder to my GetParallelFolder(oProject)
		if pblnJustFolder then
			set strDTLink to reference URL of oDTFolder
		else
			-- CREATE OR FIND A NOTE FILE FOR THIS PROJECT
			set strNoteName to "• " & strProjName & " notes" & pstrOO5Suffix
			set recNotes to my GetNotes(oDTFolder, strNoteName, strProjID)
			set strDTLink to reference URL of recNotes
		end if
	end tell
	-- DT REFERENCE URL TO OF PROJECT NOTE: unlocked2412
	tell application "OmniFocus"
		tell front document
			set note of oProject to strDTLink
		end tell
	end tell
	-- PLACE AN RTF-FORMATTED DT LINK TO THE NEW OR PRE-EXISTING NOTES IN THE NOTES FIELD OF THE PROJECT
	if pblnJustFolder then
		set strLinkTitle to pstrFolderLinkTitle
	else
		set strLinkTitle to pstrNoteLinkTitle
	end if
	(*
	my PasteToNote(oProject, strLinkTitle, strDTLink)
*)
	-- AND FOLLOW THE LINK TO THE FOLDER OR OO5 DOCUMENT IN DT2
	FollowDTLink(strDTLink)
	tell application id "DNtp" to activate
end run
-- Return the first project selected in the Omnifocus GUI
on GetSeldProject()
	tell application "OmniFocus"
		tell front document
			-- GET THE FIRST SELECTED PROJECT (CONTENT OR SIDEBAR)
			if (count of document windows) < 1 then return {missing value, "", ""}
			tell front document window
				set oProject to missing value
				repeat with oPanel in {content, sidebar}
					set lstSelns to (value of selected trees of oPanel where (class of value = task) or (class of value = project))
					if (count of lstSelns) > 0 then
						set oProject to first item of lstSelns
						exit repeat
					end if
				end repeat
				if oProject is missing value then return {missing value, "", ""}
				if class of oProject = task then set oProject to containing project of oProject
				tell oProject to return {it, name, id}
			end tell
		end tell
	end tell
end GetSeldProject
on DTLinksInNote(oProject)
	tell application "OmniFocus"
		-- DOES ITS NOTE CONTAIN A DEVONTHINK URL ?
		set strQuery to "select CAST(notexmldata as text) from task where persistentIdentifier = \"" & (id of oProject) & "\""
		set strNoteXML to do shell script "sqlite3 " & pstrDBPath & space & quoted form of strQuery
		set strLink to pstrXMLPrefix & pstrDTPrefix
		set blnOpened to false
		if strNoteXML contains strLink then
			set {strDlm, my text item delimiters} to {my text item delimiters, pstrRunDelim}
			set lstRuns to text items of strNoteXML
			set my text item delimiters to "<lit>"
			set {blnFolder, blnNote} to {false, false}
			set {strFolderURL, strNotesURL} to {"", ""}
			set strStart to text 1 thru 2 of pstrNoteLinkTitle
			repeat with oRun in lstRuns
				set lstSections to text items of oRun
				if length of lstSections > 1 then
					set strLabel to item -1 of lstSections
					set strPreamble to item -2 of lstSections
					if strLabel begins with strStart then
						if not blnFolder then
							if strLabel contains "folder" then
								set strFolderURL to my ParseLink(strPreamble)
								if strFolderURL ≠ "" then set blnFolder to true
							end if
						end if
						if not blnNote then
							if strLabel contains "notes" then
								set strNotesURL to my ParseLink(strPreamble)
								if strNotesURL ≠ "" then set blnNotes to true
							end if
						end if
					end if
					if blnFolder and blnNote then exit repeat
				end if
			end repeat
			set my text item delimiters to strDlm
			return {strFolderURL, strNotesURL}
		else
			if note of oProject = "" then set note of oProject to space -- (seems to prepare note for easier opening later)
			return {"", ""}
		end if
	end tell
end DTLinksInNote
on ParseLink(strXML)
	set {strDlm, my text item delimiters} to {my text item delimiters, pstrXMLPrefix & pstrDTPrefix}
	set lstParts to text items of strXML
	set strURL to ""
	if length of lstParts > 1 then
		set my text item delimiters to "</value>"
		set strURL to first text item of item 2 of lstParts
		if length of strURL = plngURLchars then
			set strURL to pstrDTPrefix & strURL
		else
			set strURL to ""
		end if
	end if
	set my text item delimiters to strDlm
	return strURL
end ParseLink
on FollowDTLink(strURL)
	if strURL ≠ "" then
		set blnOpened to (do shell script "open " & quoted form of strURL) = ""
		if blnOpened then
			tell application id "DNtp" to activate
			return true
		else
			return false
		end if
	else
		return false
	end if
end FollowDTLink
on GetParallelFolder(oProject)
	set {strPath, strFolderDB, strProject} to GetProjPath(oProject)
	tell application id "DNtp"
		-- CHOOSE THE TARGET DATABASE
		-- EITHER FROM A PATH IN THE ENCLOSING FOLDER, OR FROM THE DEFAULT
		if strFolderDB ≠ "" then
			set strDb to strFolderDB
		else
			set strDb to (POSIX path of pstrDocsPath) & pstrDTDB & pstrDTsuffix
		end if
		set oDb to open database strDb
		if oDb is missing value then
			set oAnswer to display dialog "Create new Devonthink database at \"" & strDb & "\" ?" buttons {"Cancel", "OK"} default button 1
			if the button returned of oAnswer is "Cancel" then return
			set oDb to create database strDb
		end if
		-- DEPENDING ON GLOBAL SETTING, OPTIONALLY CREATE NEW FOLDER WITHIN SYNC GROUP
		if pblnUseSyncAsRoot then
			set oLocn to create location pstrSync & strPath in oDb
		else
			set oLocn to create location strPath in oDb
		end if
		set URL of oLocn to pstrOFPrefix & (id of oProject)
		return oLocn
	end tell
end GetParallelFolder
on GetProjPath(oProject)
	tell application "OmniFocus"
		set strProject to name of oProject
		set strPath to strProject
		set oContainer to container of oProject
		set blnFolderFound to false
		set strFolderDB to ""
		set cClass to the class of oContainer
		repeat while cClass is not document
			if not blnFolderFound then
				if cClass is folder then
					set strFolderDB to note of oContainer
					if ((strFolderDB ends with pstrDTsuffix) and my FileExists(strFolderDB)) then set blnFolderFound to true
				end if
			end if
			set strPath to name of oContainer & "/" & strPath
			set oContainer to container of oContainer
			set cClass to the class of oContainer
		end repeat
		return {"/" & strPath, strFolderDB, strProject}
	end tell
end GetProjPath
on GetNotes(oDTFolder, strNoteFile, strProjectID)
	tell application id "DNtp"
		set lstNoteRecs to children of oDTFolder where name = strNoteFile
		if length of lstNoteRecs > 0 then
			return first item of lstNoteRecs
		else
			-- IMPORT A FRESH TEMPLATE FILE FROM THE SAME FOLDER AS THIS SCRIPT
			tell application id "MACS" to set oScriptFolder to container of file (path to me)
			set strScriptFolder to POSIX path of (oScriptFolder as alias)
			set strTemplate to strScriptFolder & pstrOO5Template & pstrOO5Suffix
			if my BundleExists(strTemplate) or my FileExists(strTemplate) then
				set oRec to import strTemplate to oDTFolder
			else
				-- OR FROM INSIDE THE SCRIPT BUNDLE
				set strTemplate to POSIX path of (path to me) & pstrOO5Template & pstrOO5Suffix
				try
					set oRec to import strTemplate to oDTFolder
				on error
					display alert pstrOOTemplate & " not found in this script bundle"
					return missing value
				end try
			end if
			tell oRec
				if it is missing value then return it
				set its name to strNoteFile
				set its URL to pstrOFPrefix & strProjectID
			end tell
			return oRec
		end if
	end tell
end GetNotes
on FileExists(strPath)
	(do shell script ("test -e " & quoted form of strPath & "; echo $?")) = "0"
end FileExists
on BundleExists(strPath)
	(do shell script ("test -d " & quoted form of strPath & "; echo $?")) = "0"
end BundleExists