Group Script to Add Documents to OF Assistance

I’ve downloaded a folder action script that automatically creates tasks from the documents in the folder (with the document as an embedded attachment) in OmniFocus. The link to the article and script is here.

What I would like to do with this is modify it to add the task with the document link back DEVONthink (x-devonthink-item://…) and attach the script to a DEVONthink group to run when the group is selected, and new documents have been added to the group.

I suspect that the biggest challenge would be to not add all documents that have already been added as a task in OmniFocus, and naturally the code needs to add a link to the document rather than embed the document as an attachment. Any thoughts?

Original code:

on adding folder items to this_folder after receiving added_items
		repeat with i from 1 to number of items in added_items
			set this_item to item i of added_items
			tell application "Finder" to set file_name to (name of this_item)
			tell application "OmniFocus"
				tell quick entry
					set NewTask to make new inbox task with properties {name:file_name}
					tell the note of NewTask
						make new file attachment with properties {file name:this_item, embedded:true}
					end tell
				end tell
			end tell
		end repeat
	end try
end adding folder items to

I believe this might do what you’re looking to do:

(* Trigger Script to Add Group Contents to OmniFocus.scpt
This script is added to a group in DEVONthink (in the Info Panel for that
group).  When the group is selected, the script executes.  The script examines
the items in the group to see if they have the token "_OF" in their name.  If
yes, then skip that item, if no, then the item has not been added to OF as link
to an OF task.  Open the OF Quick Entry window, populated with tasks whose name is
the record name in DT, and which have an URL attached that link back to the DT
record.  Do what you do in the Quick Entry window and save it. *)

on triggered(theRecord)
		tell application id "com.devon-technologies.thinkpro2"
			set theSelection to the children of theRecord
			repeat with thisItem in theSelection
				if the name of thisItem does not contain "_OF" then
					set thisURL to reference URL of thisItem
					set thisName to name of thisItem
					tell application "OmniFocus"
						tell quick entry
							set newTask to make new inbox task with properties {name:thisName, note:thisURL}
						end tell
					end tell
					set thisName to thisName & "_OF"
					set the name of thisItem to thisName
				end if
			end repeat
		end tell
	end try
end triggered

As noted in the script, the suffix “_OF” is used to indicate an OF item has been created for the DT record. Use any token, prefix, or suffix desired. Merely modify those code sections.

The script is not smart enough to know that you actually finished Quick Entry.

Most excellent korm! Exactly what I was looking for-thanks.

What repercussions might I encounter when this happens? I’ve not noticed anything unusual thus far.

The DT record has the suffix, looking as though there is an OF item, when there isn’t because Quick Entry was canceled.

I can work with that limitation, no problem. Thanks again korm.

If the master of OF scripting, Robin Trew (here, @Houthakker), stops by this thread he might have a suggestion for checking in OF to see if a given DT reference url is attached to one or more OF tasks

Korm is teasing me :wink:

Not sure if this is what you are after, but the simplest way to check whether there are any OF tasks with notes containing Devonthink UUIDs would be to look in the cache, casting the notexmldata field to text (from ‘blob’).

A first draft might look something like:

property pstrPrefix : "x-devonthink-item://"

-- Example (returns an integer of value zero or more)

on DevUUIDCountInOF(strDevnUUID)
	set strCmd to "sqlite3 ~/Library/Caches/com.omnigroup.OmniFocus/OmniFocusDatabase2 " & ¬
		quoted form of ("select count(*) from task where cast(notexmldata as text) like \"%" & pstrPrefix & strDevnUUID & "%\"")
	(do shell script strCmd) as integer
end DevUUIDCountInOF

(Strictly one should probably search for a fuller pattern in the Note XML, like:

<value key="link">x-devonthink-item://8A772553-DE20-495C-BD7D-A8E72403EC27

but you would have to take care with escaping nested quote characters).