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’sSmart 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 thedevelopSmartRuleScript
handler and paste it into an actualperformSmartRule
handler.
- Copy the code inside the
-