I use various email services, including Apple Mail
My intended purpose is email, not storage/archive
For storage/archive, I use Devonthink as my digital filing cabinet
Why we have an “special E-Mail Plugin und special E-Mail Import in devonthink"
Others may find these useful, but I prefer running a script for selected emails
I also use the script to save a copy of all sent email
I like this script and use it often. My workflow, however, would work better for me if the script would not create a new group, but leave the original mail and all its attachment in their original location. My knowledge of AppleScript was too spurious to do it myself - would you or anybody else be able to help me out? Thanks as always.
-- Import attachments of selected emails
tell application id "DNtp"
set theSelection to the selection
set tmpFolder to path to temporary items
set tmpPath to POSIX path of tmpFolder
with timeout of 14400 seconds
repeat with theRecord in theSelection
if type of theRecord is unknown and path of theRecord ends with ".eml" then
set theRTF to convert record theRecord to rich
set theURL to reference URL of theRecord
set newGroup to false
try
if type of theRTF is rtfd then
set thePath to path of theRTF
set theGroup to parent 1 of theRecord
set theName to name of theRecord
set theModificationDate to the modification date of theRecord
set theCreationDate to the creation date of theRecord
set theAdditionDate to the addition date of theRecord
tell text of theRTF
if exists attachment in attribute runs then
tell application "Finder"
set filelist to every file in ((POSIX file thePath) as alias)
repeat with theFile in filelist
set theAttachment to POSIX path of (theFile as string)
if theAttachment does not end with ".rtf" and theAttachment does not end with ".png" then
try
with timeout of 7200 seconds
-- Importing skips files inside the database package,
-- therefore let's move them to a temporary folder first
set theAttachment to move ((POSIX file theAttachment) as alias) to tmpFolder with replacing
set theAttachment to POSIX path of (theAttachment as string)
tell application id "DNtp"
if newGroup is false then
set newGroup to create record with {name:theName, type:group, modification date:theModificationDate, creation date:theCreationDate, addition date:theAdditionDate} in theGroup
end if
set importedFile to import theAttachment to newGroup
set URL of importedFile to theURL
set the modification date of importedFile to theModificationDate
set the creation date of importedFile to theCreationDate
set the addition date of importFile to theAdditionDate
end tell
end timeout
end try
end if
end repeat
end tell
if newGroup is not false then
tell application id "DNtp"
move record theRecord to newGroup
end tell
end if
end if
end tell
end if
on error msg
display dialog msg
end try
end if
One thing I find weird (but I’m not an AppleScript afficionado):
if newGroup is false then
set newGroup to create record with {name:theName, type:group, modification
date:theModificationDate, creation date:theCreationDate, addition
date:theAdditionDate} in theGroup
end if
set importedFile to import theAttachment to newGroup
and you have set newGroup to false at the top of your script. So why ask for it, if you already know that it’s false? OTH, if it weretrue, your code would fail mserably, because set importedFile to import theAttachment to newGroup would import your file to true.
As to your original question: You already know the group where the original record is located (or at least a group) because of
So get rid of all this newGroup stuff and simply set importedFile to import theAttachment to theGroup
That is, if I understood you and the script correctly. Which might not be the case, given the coding style.
<rant>
AppleScript would be a lot easier to read without these useless the in variable names. And without flags (newGroup in this case) re-purposed as something else. Dynamic typing can be a nice thing, but in many cases it is just a PITA. </rant>
Don’t worry, it’s nothing personal - @chrillek is our resident JavaScript guru, and kinda despises AppleScript Good catch, though, @chrillek - I wonder whether the original author intended for the options to be false or /path/path/ rather than false/true.
While i can’t deny that, in this case it was more about the infatuation with putting the in front of nearly every variable name. And I’m aware that it’s not a language requirement or feature but rather a weird habit of many authors. Which doesn’t help with readability, i think.
Original author here. Not sure why I exactly included that, but I think it was just to be able to run the script without that part by setting newGroup. On using the with variables it’s exactly like @cgrunenberg says: it’s indeed ugly, but Applescript is weird / eccentric when it comes to reserved variable names and this is the easiest way to prevent accidentally naming something that is actually a reserved keyword.
That’s a great solution for my long-lasting problem. One question though. Is it possible to modify the script so that the attachment(s) is/are renamed according to the Name of the email? Possibly after set importedFile to import theAttachment to newGroup command I sense that the name of the attachment could be modified accordingly. Since I have no experience with Apple Script, any help is appreciated.
Can I ask that you try to not break the workflow for those who do not use Apple Mail and instead import to DT by downloading messages from webmail?
I do this because if I used Apple Mail then it would be yet another thing to sync to both my desktop and laptop computer and another major database to set up any time I get a new computer.
Plus I have found that Apple Mail tends to have performance issues with very large mailboxes i.e. 50 Gb or larger.
Hi, thank you for your response. I dont want to convert every email to a grouping with the email and pdf attachment extracted. My flow of smart rules determine which emails get placed where in the database then only emails in a certian group would need to have the attachments pulled out of the email and grouped together before being moved.