Can't log name of record via Smart Rule Script

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.

Yes. I am sure. You might look for examples here in the forum.

But your example throws an error here, too. Even if I get rid of the const main ... main() construct that’s not needed here. Could be one of the weirdnesses of JXA, because the script runs just fine in the Script Editor.

Sorry, I didn’t mean it in a harsh way. Thank you for testing it ! I will look at the examples.

Yes, the script runs fine in Script Editor.

I just tried with this template script:

function performSmartRule(recs) {

  const app = Application("DEVONthink 3");
};

and I get:

08:53:09: ~/Library/Application Scripts/com.devon-technologies.think3/Smart Rules/test.scpt on performSmartRule (A identifier can’t go after this identifier.)

With this script:

function performSmartRule(records){
	return 'Hello World'
};

I get:

08:57:29: ~/Library/Application Scripts/com.devon-technologies.think3/Smart Rules/po.scpt on performSmartRule (Error -1708)

Perhaps I am missing something ?

I’m really puzzled. I tried the same examples as you, and even an empty function throws error -1708.
Just to make sure, I ran my JS script mentioned in the other thread and linked to above – no problems at all.
Maybe @cgrunenberg or @BLUEFROG can shed some light on this?

For DEVONthink it makes no difference whether a script is actually AppleScript or JavaScript. But the internal script editor doesn’t support JavaScript due to limitations of Cocoa, therefore I would actually discourage to mix different script languages (e.g. in case of smart rules & reminders).

But this example is pure JavaScript, isn’t it? And it’s also an external script (or at least I tried this with an external script, so DT3’s script editor want involved.

I also tested it as an external script and found those errors.

DEVONthink doesn’t even know whether a script uses JavaScript or AppleScript, it always receives the same events (or at least should). Whatever is going on seems to be an issue of JXA which I wouldn’t recommend actually due to its poor state and uncertain future.