Can't log name of record via Smart Rule Script

Hello, everyone.

Just trying to execute this script…

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}";
}

Does anyone know what’s happening ?

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.

Hello, @BLUEFROG. Thanks for answering !

I am doing a very basic test as I would need to use Foundation framework in order to do operations with JSON Data.

Thanks, I will take that into account.

You’re welcome.

What are you up to with JSON data? :slight_smile:

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)

And what are you doing with the JSON data - creating a web interface or en Electron app or somesuch?

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).

Correct. Only AppleScript is supported in smart rules.

Is there a way to use Foundation framework?

Did you ever get a resolution for this?

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

Could not make it work. Thanks for the follow-up.

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 :wink:

1 Like

Yes @chrillek , this is also my preferred language for scripting DT.

That’s why I would like to use JS scripts in Smart Rules. I absolutely agree :slight_smile:

1 Like

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

Indeed. The embedded script editor only supports AppleScript.

Are you referring to Smart Rules ?

Yes

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.

Maybe this same technique will work for you.

2 Likes

Are you sure that’s possible ? I’m trying to run this script but getting an error.

function performSmartRule(recs) {
    // main :: IO ()
    const main = () => {
        const app = Application('DEVONthink 3')
        app.logMessage('Hello')
    };

    // MAIN --------------------------------------------------------------
    return main()
};

That’s really good, @clang ! Thank you. Will try that.