Script: Comfortably developing a Smart Rule Script

Developing a Smart Rule script can be challenging:

  • If we use an embedded script then changes are updated immediately - but it’s necessary to open the Smart Rule script editor for each edit. I almost never edit directly in DEVONthink’s editor but copy paste into Script Debugger which makes the process even more cumbersome.

  • If we use an external script then changes are not updated immediately. DEVONthink caches scripts which makes it necessary to restart the app after each edit.

  • If we’re building a Smart Rule that uses an Execute script action and other actions testing might become very difficult.

Scenario 1

Simple Smart Rules

For simple Smart Rules that only use an Execute script action it’s sufficient to call the handler on the selection. That’s also sufficient if a script doesn’t rely on changes made by other actions.

This way we can use an external editor and changes are updated immediately.

-- Developing a Smart Rule Script - External script that calls the handler

on run
	tell application id "DNtp" to my performSmartRule(selection as list)
end run

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			activate
			repeat with thisRecord in theRecords
				display dialog "Changes are updated immediately"
			end repeat
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performSmartRule


Scenario 2

Advanced Smart Rules

If we’re building a Smart Rule that uses an Execute script action and other actions we might run into a situation where the Smart Rule Script can’t be tested on it’s own because it depends on changes made by other actions.

However, in such a situation there’s a quite comfortable way to develop a Smart Rule script: While developing we can use an embedded script that loads an external script.

This way we can use an external editor, changes are updated immediately and we can test the whole Smart Rule with the changed script.

Setup

1 - External script

In Script Editor.app:

  • Save this script
    Note: It’s not necessary to save it to DEVONthink’s Smart Rule folder.
-- Developing a Smart Rule Script - External script

on developSmartRuleScript(theRecords)
	tell application id "DNtp"
		try
			activate
			repeat with thisRecord in theRecords
				display dialog "Changes are updated immediately"
			end repeat
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end developSmartRuleScript


2 - Embedded script

In DEVONthink:

  • Create a Execute script > Embedded action
  • Paste this script
  • Don’t close the editor

In Finder:

  • Go to the external script file
  • Right click it while pressing ⌥
  • Choose Copy as Pathname

In DEVONthink:

  • Set property theScriptPath to the copied path
-- Developing a Smart Rule Script - Embedded Smart Rule script that loads the external script

property theScriptPath : "/Users/USER/Desktop/Test Smart Rule Script.scpt"

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			set theScript to load script (POSIX file theScriptPath as alias)
			theScript's developSmartRuleScript(theRecords)
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performSmartRule


Usage

  • Develop your Smart Rule Script in the editor of your choice.

  • Once everything is working as desired:

    • Scenario 1:

      • Use the script as an embedded or as an external script.
    • Scenario 2:

      • Copy the code inside the developSmartRuleScript handler
      • Paste it in an embedded or in an external script.
        Note: Make sure to copy only what’s inside the developSmartRuleScript handler and paste it into an actual performSmartRule handler.
13 Likes

This is a great idea.
There are a few tiny obstacles to get over when you’re teaching yourself scripting and one of them is that cacheing issue.
I’m sure anyone who started down this path has run into the situation where you run the script from Script Editor or Script Debugger and it’s all fine and then when you try to turn it into a smart rule something steps working and all the changes you make have no impact and eventually you give up after an hour of fruitless googling.
I knew there was a cacheing issue with scripts but it takes a while for the newly formed domains of information to link up in a way that’s useful so you know WHAT to google to find out HOW to do something. Thanks again.

1 Like

I wish I had reviewed the instructions for Scenario 2 before debugging a Smart Rule-based script over the weekend! I saw DEVONthink’s lovely “Welcome to DEVONthink 3” startup window far too many times…

In case it helps others, I built out a little script to create test files in order to test my Smart Rule-based script.

  1. Modify the below script such that the created file’s properties will cause your script to trigger.
  2. Insert the name of your Smart Rule in line ten, in place of “YOUR SMART RULE NAME” (perform smart rule "YOUR SMART RULE NAME")
my createDemoFiles()

on createDemoFiles()
	tell application id "DNtp"
		set theDestination to get record with uuid "9C204305-52CB-43DB-BC0C-44361880B8B4"
		set dateString to (current date) as text
		set theRecord to create PDF document from "https://fulcra.design/files/_Kudoz-Where-are-you-scaling-placemat.pdf" in theDestination name dateString
		set tags of theRecord to theRecord's tags & ".to-organize" & "News"
		delay 1
		perform smart rule "YOUR SMART RULE NAME"
	end tell
end createDemoFiles

Your explanation “DEVONthink caches scripts which makes it necessary to restart the app after each edit” made my day. I’ve been struggling for hours with a script that I couldn’t get to work but now I realize that DT had my first (incorrect) version in cache all the time.

After restart it worked perfectly.
Many thanks!

2 Likes