There’s this longish thread about importing attachments from e-mails, removing them from the e-mail, and then adding a new part to the e-mail containing links to the attachments.
While experimenting with JXA in this context I noticed
that it is not possible to get the raw text content of an e-mail in DT (data returns an apparently useless string, source returns nothing, and plainText returns only the mail’s text, no headers).
OTOH, it is possible to create a new e-mail record from a correct (!) e-mail text, setting the source property to it in the call to createRecordWith().
but it is not possible to set the source property of an existing e-mail record to a (valid!) e-mail text. No error is raised; the record is simply not changed at all.
IMO, this is a bit unfortunate. I’m aware that being able to read and write the source property of an e-mail record at will might leave users with apparently broken e-mails – but the same is true for HTML documents, where sourceis readable and writable (according to the documentation). And scripting can cause all kinds of problems if performed without caution…
I know, and that’s what I’m doing. While that is perfectly ok with me with more complex formats like PDF and images, I’d rather see e-mails, which are pure text, treated like markdown etc.
Just a suggestion, of course.
I’ve not wanted to mess with files in the DEVONthink database to date, but it looks like the same info I need to create the .mbox files I’m creating in AppleScript is available in the DEVONthink .eml source file (primarily the first line of the e-mail data, which I’m currently [inefficiently] recreating).
The benefit is I wouldn’t have to do any coordination between Mail and DEVONthink, so it might still be something I also utilise, even if it’s in the roundabout “get data from file at path”, well, path.
Being able to just “ask” DEVONthink for the .eml source as a single command when scripting would be handy.
I’m a long way from finishing my own script, but it’s always useful to have alternative solutions.
process this tempfile with xxd -r -p tempfile targetTempFile
read the targetTempFile
targetTempFile contains the text of the e-mail. But this approach is obviously even more convoluted than reading the file directly using the path property.
That’s most likely an issue of JavaScript for Automation (JXA), DEVONthink just returns the raw data of files and that works in case of AppleScript. Anyway, an easier approach might be so simply create a temporary copy of the existing file for processing.
Agreed. I just posted that alternative here because this data issue was bugging me for ages. JXA and AS probably both receive the raw data when reading the data property. The question is, what can one do with it? In the case of a PDF, for example, I can set the data property of another record to what I get from an existing PDF, IIRC
But whenever I “look” (i.e. print) the data property, I get a string in JXA. That makes perfect sense, but I never knew how to convert that string into something useful. Now I do know.