Check for duplicate/already imported item/mail before importing

As Apple removed the support of Mail-Plugins and importing via the sidebar without the plugin won’t currently preserve the mailbox structure, I tried to use a modified version of the the “Add mailbox(es) to DEVONthink.scpt” Script, which allows me to archive a whole account.

I could’nt really find anything useful here or in the DT3s Script Editor Library about how to check for an already imported mail.
The non-plugin mail-import in the sidebar also uses scripting as per documentation and supports that already imported mails are not imported again.

So generally speaking:
How do i check if importing would produce a duplicate, is there something like ‘create record only if not duplicate’.
How does the scripts from the import-sidebar handle this?
All this without importing first, check then if there are duplicates and delete again.

I’m aware of ‘exists record with file’ (or ‘with path’), but afaik that’s the path/filename in the database, which won’t help here.

Thank you very much.

There is a smart rule “Filter Duplicates”, which is able to remove duplicates after they have been imported. You can run the smart rule within your script using perform smart rule, or run it manually after importing.

Is there a specific reason for filtering duplicates before importing?

Yes,

first: i don’t want to import thousands of mails again to then delete them.
second: the normal duplicate checking isn’t always a safe thing especially with mails (as only the mails body and subject is used and not the attachments).
And importing via the sidebar won’t import already existing ones unless otherwise configured. So there seems to be a way

Normally when DT imports emails it makes the UUID of the record to be not a regular random unique string but a message id property of importing email. That’s why you cannot import already existing email. So, your script should check it before creating a record in DT.