Scripting the MailImport plugin

Is it possible to access the MailImport (or MailConduit or whatever it is called) plugin from AppleScript?

I would like to automate periodic importing of mailboxes from Mail.app, and like how the plugin avoids re-importing the messages it already knows about.

Ideally I would like to be able to specify both the mailbox and the corresponding DEVONthink group. But maybe I could just temporarily change the preferred import location to the currently selected folder to work around it, if the location is not configurable (like it isn’t when the import is triggered from within Mail.app).

I still need to know how to trigger the import from Mail using the plugin though. I could trigger it via UI scripting to choose the menu item, but then I wouldn’t know when the import is finished, so I can move to the next mailbox. So is there a way to run this synchronously from a script?

BTW, out of curiosity, how does the plugin detect that a message has already been imported? I noticed that messages can’t be imported if they have ever been imported into any other DEVONthink database before, not just the current import location. But if I delete the imported message (and restart DEVONthink), then I can import the same message again.

I don’t have a problem with this behaviour, but would like to understand how it works.

It’s not scriptable.

The import just checks whether there’s already an item whose UUID is identical to the message ID.

Thanks, that is good to know. I’ll try to replicate the same in AppleScript then.

I noticed that when I add the UUID with


create record with {..., uuid:theMessageID}

``` then I have to check for existence with ```

get record with uuid "<" & theMessageID & ">"

``` I assume this is just something you do to prevent user assigned UUIDs from conflicting with auto-generated UUIDs. Or is there something I'm missing?

A UUID is not assigned by the User. It is assigned by the application.

By “user” I meant via “create record” in AppleScript, as shown in the code snippet above. This does set the UUID of the new record to the provided string, but also surrounds it with angle brackets, so “foo” becomes “”.

Since application assigned UUIDs don’t have the angle brackets, they effectively create different namespaces. I’m just not sure why this would be necessary.

Anyways, it is not really important, I’m just glad that the behaviour of the mail plugin can be duplicated in AppleScript.

PS: Another thing I noticed with “create record” is that you cannot set the “filename” independently from the “name”. You have to set the “name” to the desired filename, and then change the name after the record has been created. Again, not an issue once you know about it. :slight_smile:

The create record command ignores specified UUIDs but DEVONthink Pro Office automatically sets the UUID to the message ID. But only if there’s not already such a UUID.

Thank you for correcting my misunderstanding! This makes much more sense than my previous assumption that “create record” would somehow add those angle brackets to the “uuid” I was feeding it. :slight_smile: