Need help with AppleScript to run a smart rule via Keyboard Maestro

I’m trying to use Keyboard Maestroboard Maestro to trigger a smart rule in DEVONthink when I connect to a specific network. Note that the performSmartRule piece works when running it in DEVONthink. But I’m having trouble when trying to trigger it with Keyboard Maestro. Here’s the script I’m using:

tell application id "DNtp"
    set theRule to get rule with name "Move to Network"
    if theRule is not missing value then
        set theRecords to every record whose (kind ≠ "Group") and (kind ≠ "Database") and (kind ≠ "Trash") of (root of theRule)
        display dialog "Found Smart Rule and records"
        set theResult to performSmartRule(theRecords)
    else
        display dialog "Smart Rule not found"
    end if
end tell

on performSmartRule(theRecords)
    tell application id "DNtp"
        set movedFiles to {}
        set theNetworkFolder to get record with uuid "08CF2152-0D61-4E6E-A7D7-DD41AC540011"
        repeat with theRecord in theRecords
            set thePath to path of theRecord
            if thePath is not missing value then
                move record theRecord to theNetworkFolder
                set end of movedFiles to thePath
            end if
        end repeat
        if movedFiles is not {} then
            display dialog "Moved files to " & (name of theNetworkFolder) & return & (movedFiles as text)
        else
            display dialog "No files found to move"
        end if
        return movedFiles
    end tell
end performSmartRule

However, I’m encountering an error (see below) when I try to run the script. I’ve tried making some modifications to the script, but I haven’t been able to get it to work. Can anyone provide some advice on how to modify the script to get it to work?

CleanShot 2023-05-08 at 17.28.02

FYI, I’m using Keyboard Maestro to trigger the script because it offers the "wireless network trigger” feature. The ultimate goal is that I want the script to run in DEVONthink as soon as I am connected to my office network.

Thanks in advance for your help!

I think you are trying to define a Smart Rule in KM. You should instead define the Smart rule (along with the associated script if needed) in DT3. Then just use KM to trigger the Smart rule.

If your smart rules have been set up with the relevant criteria to filter the records and criteria upon which they will act then all you need in KM is the brief Appplescript command to DNtp perform smart rule.

Alternatively - if you want to define the script in KM then you don’t need a smart rule at all- just put the whole script in KM but do not use the SmartRule syntax.

2 Likes

The code doesn’t even run in Script editor, with exactly the same error message. Which refers to the line
set theRule to get rule with name "Move to Network"
And looking at DT’s scripting dictionary, I do not see any Element, Property or other object named rule. Nor is there a method get rule. Did you perhaps ask ChatGPT to write this code for you? Or did you find this syntax somewhere else? Or am I missing something obvious here?

As to

Sure thing, since the rest of the code is never run from a smart rule in DT. Only the performsmartrule handler is, but the error occurs outside of it.

Also, I doubt that path can ever be “missing value”, given that it refers to the location of a record on disk. If path were missing, there’d be no record, I guess.

Apart from that: @rkaplan is, I think, spot on with his suggestions. In addition, I’d not add dialogs to automations if not absolutely required. Having a dialog pop up for a task supposedly running in the background and requiring no interaction is kind of counter-productive. And in your case, you’re possibly creating a very, very, very long list of filenames which might even be so long as to push the “OK” button out of the screen. I’ve seen that happen. If you really need the file names (for what?), you could write them to an external log file. Or simply have a look in the network group to see what’s there.

Microsoft was famous for that: Having some admin task run in the middle of the night, popping up a dialog and waiting for confirmation to continue – which never came because the admins were in bed…

1 Like