use scripting additions
use framework "Foundation"
on performSmartRule(theRecords)
tell application "DEVONthink 3"
set strName to name of (get item 1 of theRecords)
log message strName
end tell
end performSmartRule
as part of a Smart Rule “execute script” action (Embedded).
I get this report in Console.log:
2020-03-06 13:45:57.456 DEVONthink 3[1844:102875] on performSmartRule: {
NSAppleScriptErrorAppName = "DEVONthink 3";
NSAppleScriptErrorBriefMessage = "Can\U2019t get name of content id 1250 of database id 2.";
NSAppleScriptErrorMessage = "Can\U2019t get name of content id 1250 of database id 2.";
NSAppleScriptErrorNumber = "-1728";
NSAppleScriptErrorRange = "NSRange: {134, 4}";
}
use scripting additions
use framework “Foundation”
Why are you using these?
Don’t add these items unless they’re necessary, i.e., you’re doing ASOC operations (though Development may weigh in on this as well).
Why aren’t you using the basic core snippet used by embedded scripts?
You should be using tell application id "DNtp" when calling DEVONthink.
I need to convert an AppleScript Record to a JSON String.
For example, if I have an AS record in a variable named theData, I would use roughly this code to accomplish it. In order to use NSJSONSerialization, it is necessary Foundation framework.
Code:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
-- classes, constants, and enums used
property NSJSONWritingPrettyPrinted : a reference to 1
property NSJSONSerialization : a reference to current application's NSJSONSerialization
-- An AppleScript data structure to convert to JSON
set theData to {|menu|:¬
{|id|:"file", value:"File", popup:¬
{menuitem:{¬
{value:"New", onclick:"CreateNewDoc()"}, ¬
{value:"Open", onclick:"OpenDoc()"}, ¬
{value:"Close", onclick:"CloseDoc()"}}}}}
set theJSONData to NSJSONSerialization's dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted |error|:(missing value)
I’m trying to pass that information to parse it inside a JXA script.
Inside it, I’m going to parse it by means of JSON.parse method and take advantage of JS Libraries that I’ve built (as I understand, Smart Rules do not support JXA).
I have run into the exact same problem, where an external script that relies on AppleScriptObjC runs fine standalone but fails with a similar error when run in a Smart Rule:
3:59:56 PM: ~/Library/Application Scripts/com.devon-technologies.think3/Smart Rules/Debug Smart Rule.scpt on performSmartRule (Can’t get «class DTmo» of «class DTcn» id 36305 of «class DTkb» id 4.)
I simplified my repro case to more or less the same as yours:
-- Commenting this out makes the script below succeed both standalone and from a Smart Rule.
use framework "Foundation"
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
-- This fails if run via Smart Rule but works if run directly from Script Debugger..
set theDate to theRecord's modification date
end repeat
end tell
end performSmartRule
tell application id "DNtp"
set theRecords to the selection as list
my performSmartRule(theRecords)
end tell
I’m not sure why @BLUEFROG said that JavaScript isn’t supported in smart rules. In my experience, you can use it for all scripting tasks in DT3.
And JS might make producing JSON just a tad easier
I suppose @BLUEFROG was referring to embedded scripts in smart rules. Those really must be written in AppleScript. External scripts however can be in JavaScript. So may be you give that, a try
I poked at this some more. I was able to get my script working (so far) by moving all of the code that required the use of AppleScriptObjC into a Script Library (e.g., to ~/Library/Script Libraries/MyScript.scpt) and then using it in my Smart Rule script like so:
use script "MyScript"
tell script "MyScript"
doSomethingUsingAppleScriptObjC()
end tell
The key is that the script DEVONthink compiles for use in the Smart Rule does not contain any AppleScriptObjC references itself, only the script in the Script Library.