Removing duplicates with Smart Rule?

I spent some time this evening—based on your snippet—getting this to run for Smart Groups. I also made some minor improvements. If anyone is interested, here it is:

function performsmartrule(records) {
    var app = Application("DEVONthink");
    app.includeStandardAdditions = true;

    records.forEach(r => {
        if (r.numberOfDuplicates() > 0) {
            const duplicateRecords = r.duplicates();
            const targetGroups = new Set();

            // Populate the targetGroups set
            duplicateRecords.forEach(duplicate => {
                targetGroups.add(duplicate.locationGroup());
            });

            // Create only one replicate in each group
            targetGroups.forEach(group => {
                app.replicate({ record: r, to: group });
            });

            // Delete duplicates
            duplicateRecords.forEach(duplicate => {
                app.delete({ record: duplicate });
            });
        }
    });
}

It seems to work.

You are right by saying this is trivial, however, it still throws an error: on performSmartRule (Error: Error: Can't get object.). Any suggestion? I’ll investigate this later if I find time…