Trouble with `performSmartRule`

I have set up a Smart Rule with an External script. Here the script I use:

-- Strip unwanted parts in Document Name

on performSmartRule(theRecords)
    tell application id "DNtp"
        repeat with theRecord in theRecords
            set name of theRecord to do shell script "$HOME/share/bin/strip_title " & quoted form of name of theRecord
        end repeat
    end tell
end performSmartRule

I have put it in the appropriate Libarray folder, so I was able to select it for the Smart rule in the rule UI. So far so good.

When I select my rule in the Sidebar the file(s) in rule condition show up and I right-click on the rule and click “Apply” - then nothing happens.

Ther’s obviously something wrong with my Applescript… Any idea what?

Does a similar script work in the Script Editor.app? A screenshot of the rule might be useful too.

Here you go, Christian:

The rule in the side bar:

The steps of the rule:

Cheers,
Leo

Both the rule and the script look fine. Did you test the script in the Script Editor.app first? Is strip_title working as expected?

Hold the Option key and choose Help > Report Bug.

DO NOT SEND THE EMAIL.

In the draft email, double-click the Console.log and any AppleScript errors from the smart rule should be reported there.

PS: What is your shell script doing?

Yes, on the command line

$HOME/share/bin/strip_title "dt-comp BERT Explained"

returns

BERT Explained

as intended (and used by other tools).

Thanks for the invaluable tip with the Console.log!

In the log I get:

2019-12-12 18:21:11.625 DEVONthink 3[375:1227237] on performSmartRule: {
    NSAppleScriptErrorBriefMessage = "Can\U2019t make quoted form of name of item 1 of {content id 290526 of database id 1} into type Unicode text.";
    NSAppleScriptErrorMessage = "Can\U2019t make quoted form of name of item 1 of {content id 290526 of database id 1} into type Unicode text.";
    NSAppleScriptErrorNumber = "-1700";
    NSAppleScriptErrorRange = "NSRange: {0, 0}";
}

or with another document

2019-12-14 12:52:29.665 DEVONthink 3[18898:1555668] on performSmartRule: {
    NSAppleScriptErrorBriefMessage = "Can\U2019t make quoted form of name of item 1 of {\U00abclass DTcn\U00bb id 290633 of \U00abclass DTkb\U00bb id 1} into type Unicode text.";
    NSAppleScriptErrorMessage = "Can\U2019t make quoted form of name of item 1 of {\U00abclass DTcn\U00bb id 290633 of \U00abclass DTkb\U00bb id 1} into type Unicode text.";
    NSAppleScriptErrorNumber = "-1700";
    NSAppleScriptErrorRange = "NSRange: {0, 0}";
}

Is this strip_title something you wrote?
Is it only stripping dt-comp from the front?

I’m guessing this can be more effectively done without calling an external shell script.

Yes, I wrote it. It does not much more then stripping various markers from the title, but I use it on teh comand line as well and don’t really want to rewrite it in Applescript.

From the error message I take it is a type error. In the performSmartRule function do I have to type-convert the argument of the shell script and/or the output of the same script?(Applescript does this sometimes with the as string qualifier.)

You could try quoted form of (name of theRecord as string).

Just tried this. Doesn’t better it. I will investigate more…

Ok, more details now:

When I do the casting to string the function works when called from Script editor! I.e. when I have the following script:

-- Strip unwanted parts in Document Name

tell application id "DNtp"
    set sel to (selection as list)
    my performSmartRule(sel)
end tell

on performSmartRule(theRecords)
    tell application id "DNtp"
        repeat with theRecord in theRecords
            set name of theRecord to do shell script "$HOME/share/bin/strip_title " & quoted form of (name of theRecord
        end repeat
    end tell
end performSmartRule

and I run it from Script Editor the selected document is changed as expected. However when I link the same script to a Smart rule and I click Apply rule (while the document is selected) nothing happens, but I get the above reported error in console.log.

Do I miss anything or does this maybe point to a bug in the Smart Rules?

The shell invoked by smartRules is likely be a vanilla shell, different from the one in Terminal.app, and lacking anything set up in the latter.

You may need to add an environmental variable like ENV_LC_ALL, and anything else that your script depends on.

The bottom line is that things which work in your Terminal.app environment may need one or two extra settings (or fuller paths etc) in a factory-fresh shell that has just come blinking into the world.

The contents of your dt-comp would be a good place to start. It looks as if you may be using an osascript call in which the App library is not in context, or some Unicode chars (and or a path) can’t be resolved.