Smart Rule and scripting

As a knowledge worker, I’ve used TheBrain, Mindmanager, DEVONthink Tinderbox, and now Obsidian. All have great features and paradigms, but only DEVONthink stays at the center of my workflows. Every week I find new ways to boost my productivity with DEVONthink.

I’m somewhat of a newbie when it comes to AppleScript, but I monitor this forum every day and I’m inspired to try more ways to use Smart Rules with scripting.

I want to build a Smart Rule for my task management. I routinely use a “Todo” label that I apply to DEVONthink documents that I want to act upon, and I then move them into Omnifocus for tracking and followup.

I modified a script found on this forum and I can’t seem to get it to work. What I’m trying to do is this: when I apply the “todo” label to a document, send the document to Omnifocus’ Inbox and add a link back to the DEVONthink document in the Omnifocus note. Then, change the DEVONthink table to “None” and apply a tag indicating I’ve processed it.

Here’s the AppleScript that I attach to my Smart Rule:
on performSmartRule(selection)

set theSummary to ("From Devonthink: " & name of selection) as string

set theURL to (reference URL of selection) as string

set label of selection to 0

set tags of selection to (parents of selection) & “sentToOmnifocus”

display alert “DEVONthink” message (selection) & " words."

– Add new to do to OmniFocus

try

tell application “OmniFocus”

tell default document to set newTask to make new inbox task with properties {name:theSummary, note:theURL}

end tell

end try

end performSmartRule

But–nothing happens!

What am I missing?

Thanks for any advice from forum members.

The basic format is

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
		end repeat
	end tell
end performSmartRule

(btw: if you post your script enclosed by ``` on a leading and a trailing line then it will appear as above).

Your script has two problems:

  • it omits the tell application id "DNtp" block
  • it uses a list to reference single records
    • that is, the smart rule sends a list of records (conventionally called theRecords and called selection in your rule) from which you then need to select a record, which you have omitted to do.
    • if the smart rule only ever sends a single record, then your script might work; I’ve not got time to try; but it would certainly not work as soon as more than one record was passed to the script.

I’m also not sure what you are trying to do with this line: set tags of selection to (parents of selection) & “sentToOmnifocus”, although that may be because I am bleary eyed.

When developing scripts, it is worth leaving out the try block or adding an on error section to that block; both ways make debugging easier (currently your script can fail without feedback).

Thanks Blanc! I fixed it up as you recommended and it’s working great. Much appreciated.

1 Like

I’m not an AppleScript afficionado, but this line looks fishy to me. tags is a list of strings, you’re setting it to a single string. This might work (it would certainly fail in JavaScript, though), but if it does, it will effectively overwrite any tags set on the record before. I suppose that you’d rather first get the current tags list, append your “send to Omnifocus” thingy to it and then set the tags to this new list.

The AppleScript line does work, though I absolutely understand what you’re saying. My goal was to process the document in the SmartGroup, send it to OF as a new task, and add a new tag to the end of the existing DT document tags to show that it had been processed by the script and “send to Omnifocus.”

My current dilemma is triggering the script; I want it to launch whenever I change the label on the document to “Todo,” but I’m not achieving that. Instead, I have to launch the Smart Group item on demand.

This sounds useful @ptc97504.
Would you be willing to post the working version of the script? I’d like to give it a go

on performSmartRule(theRecords)
	
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theSummary to ("From Devonthink: " & name of theRecord) as string
			set theURL to (reference URL of theRecord) as string
			set label of theRecord to 0
			set tags of theRecord to (parents of theRecord) & "sentToOmnifocus"
			tell application "OmniFocus"
				tell default document to set newTask to make new inbox task with properties {name:theSummary, note:theURL}
			end tell
		end repeat
	end tell
	
end performSmartRule

Attached as AppleScript to an OnDemand Smart Rule

Great, thanks!

Typically you would use set tags of theRecord to (tags of theRecord) & “sentToOmnifocus” to do that. I can’t get my head round parents of theRecord in this case :see_no_evil: (but yes, a search of the forum turns up a post from 2019 explaining…)

One of the smart rule triggers is “on label” - does that not work?

I went back to troubleshoot after your comment and I found my mistake: “On Labelling” does work as an action in the Smart Rule, but I had the search criteria wrong. I was searching on “label=red” and what I needed to search on was “Kind = any document.” Problem solved!

1 Like

Out of curiosity: Does the & operator append an element to a list? I’ve only encountered it as string concatenation so far.

Yes, the “&” operand in AppleScript appends to the tag list successfully.

Operator recycling. Reminds me of my youth when we had to learn how to overload the plus operator for matrix addition. Which is kind of ok in a strongly typed language, but in a scripting idiom like AS? A very loud call for desaster.

(Not implying that TheOtherLanguage™ is any better: there they use “+” for addition and string concatenation triggering all kinds of interesting casting errors).

I don’t even know what that means :see_no_evil: But I am trying to figure out how to fry eggs in a steel pan.

Note: in AppleScript, the result of a concatenation will be of the same class as the leftmost item. You can use this when creating text from items that may be text or lists: simply start the concatenation with an empty string.

1 Like