Skip to content

Instantly share code, notes, and snippets.

@EmilioMoreno
Created August 8, 2017 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmilioMoreno/18bc81d319a049600f4a08dfd4ab7495 to your computer and use it in GitHub Desktop.
Save EmilioMoreno/18bc81d319a049600f4a08dfd4ab7495 to your computer and use it in GitHub Desktop.
Script to import mail and attachments from Airmail into Devonthink (as html) while keeping the references between them.
(*
Applescript to import an email with attachments from Airmail3 into Devonthink
Based on the scripts from Kyle Eggleton: http://eggleton.co.nz/geekposts/2017-02-12-Sending-email-from-AirMail-to-DevonThink/
This script combines the two scrips from Kyle Eggleton into a single one with the following aditions:
It imports the e-mail and all the attachments in one script
The mail is imported as html format (instead of eml). This helps to preserve the original look; importing as eml usually break complex html designs.
The record with the e-mail includes links to the different attachments, with its own name each.
The attachments are created as standalone records modifying the creation time of the record to the current date, instead of document creation date (this helps when sorting by date)
The attachments are created with the reference url of the Devonthink imported mail, so when you look at an attachment, you know which is the record that references it.
This is my first applescript, so do not expect it to be efficient or well done, but it works for me.
*)
global all_documents
global theGroup
global theMainRecord
set all_documents to {}
set all_documents_urls to {}
tell application "Airmail 3"
--Check if we have at least 1 e-mail selected in AM3, exit otherwise.
set x to the count of the selected message
if x is 0 then
display alert "Airmail 3" message "There are no messages selected" as warning
return
end if
--We get the group to store the e-mail and the files.
tell application id "DNtp"
if not (exists current database) then display alert "Airmail 3" message "No database is in use in DevonThink" as warning
set theGroup to display group selector
end tell
set {theSender, theSubject, theSource} to {the sender, the subject, the htmlContent} of the selected message
set theURL to the selectedMessageUrl
set num_of_attachments to the count of mail attachment of the selected message
-- if the message has attachments, add them to DT and store the reference in a list
if num_of_attachments is greater than 0 then
-- Import the attachments into DevonThink. All the attachments are placed also in global list all_documents.
set i to 1
repeat until i = num_of_attachments + 1
set theAttach to filename of mail attachment index i of the selected message
set theFile to POSIX path of theAttach
tell application id "DNtp"
set simple_file to import theFile to theGroup
set the date of simple_file to current date
set end of all_documents to simple_file
set end of all_documents_urls to reference URL of simple_file
end tell
set i to i + 1
end repeat
end if
-- add header information
set theBody to "<b>Subject:</b> " & theSubject & "<br>
<b>From:</b> " & theSender & " <br>
<hr>
" & theSource
repeat with i from 1 to length of all_documents_urls
set theCurrentListItem to item i of all_documents_urls
set theBody to (theBody & "<a href=" & theCurrentListItem & ">" & the name of (item i of all_documents) & "</a><br>")
end repeat
tell application id "DNtp"
set theMainRecord to create record with {name:theSubject, source:theBody, type:"html", URL:theURL} in theGroup
set theRecordUrl to reference URL of theMainRecord
repeat with i from 1 to length of all_documents
set theCurrentListItem to item i of all_documents
set the URL of theCurrentListItem to theRecordUrl
end repeat
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment