Smart Rule fails only when triggered as a smart rule: (Can’t get name of «class DTcn» id 1296597 of «class DTkb» id 2.)

Update: I may have “solved” the problem.

A bit of lucky searching led me to a StackOverflow thread that referenced this “not hosted by Apple, Inc.” documentation page on use statements:

My guess is that DEVONthink is wrapping our Smart Rule scripts somehow, so AppleScript gets angry at the use of use.

That Stack Overflow answer had a solution:

To fix this we create a “script library”, which is just another AppleScript file. Let’s say we call this script library chewbacca.scpt . You need to place the script in a specific location on your Mac. (You have a few options for this location). AppleScript only looks in those locations when trying to import script libraries.

To summarize:

  1. Write the functions (“handlers”, in AppleScript parlance) using the use statements—i.e., using the frameworks you need—in a single script file.
  2. Save the script file to a Script Library folder (i.e., ~/Library/Script Libraries/) with a useful name (e.g., Read Annotations.scpt). There, you’ve just created a Script Library of your own!
  3. In the DEVONthink Smart Rule, you can then reference that Script Library like this: tell script "Read Annotations", and then invoke your function.

This works! :tada:

It would be nice if DEVONthink didn’t need this workaround, but my guess is that it’s an unsolvably deep technical limitation.

The sad consequence is that it makes sharing this script a little harder, as I will now have to tell folks to install a script library, which is probably a deterrent to using the script. Alas—better make it good so that it is worth the trouble.

Oh, one more thing, given the tomatoes being thrown at AppleScript. Check out this Stack Overflow user’s commentary from a related thread:

Unfortunately, you’re assuming AppleScript’s library system was designed by a competent engineer. This is not so: like almost everything the current AppleScript team spews out, it’s the software version of a clown car: totally unpredictable, dangerously unsafe, and the moment you think you’ve finally figured out correctly how it works, all the doors fly off and dozens of lunatic clowns leap out and pelt you with unspeakable substances.

2 Likes