When I’ve imported an email with PDF attachments from Outlook to DTPO by the script Add Message(s) To DevonThink, the attachments are displayed in DTPO as shwon below:
How can I import the attached PDF files into the same group of DTPO at one click? At the moment I have to drag and drop each file for itself. This is annoying if - as in the example - the email contains a dozen files.
-- 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
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
try
if type of theRTF is rtfd then
set thePath to path of theRTF
set theGroup to parent 1 of theRecord
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" then
-- 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" to import theAttachment to theGroup
end if
end repeat
end tell
end if
on error msg
display dialog msg
end try
delete record theRTF
end if
end repeat
end tell
Indeed this script works great. @cgrunenberg is there also a way to link the extracted file in some way to the original message? (I thought maybe a hack would be to include the devonthink URL of the original message in the URL field?)
This slightly modified version of the script will include the DEVONthink UUID of the original mail as the URL for each attachment:
-- 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
repeat with theRecord in theSelection
set theID to uuid of theRecord
set theID to texts 2 thru ((length of theID) - 1) of theID
set theID to "x-devonthink-item://%3C" & theID & "%3E"
if type of theRecord is unknown and path of theRecord ends with ".eml" then
set theRTF to convert record theRecord to rich
try
if type of theRTF is rtfd then
set thePath to path of theRTF
set theGroup to parent 1 of theRecord
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" then
-- 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"
set theAttachmentRecord to import theAttachment to theGroup
set URL of theAttachmentRecord to theID
end tell
end if
end repeat
end tell
end if
on error msg
display dialog msg
end try
delete record theRTF
end if
end repeat
end tell
Please test it on some test files before putting it to proper use. I have tested it on one e-mail with 2 attachments, and it works as expected here. Does it do what you were asking for?
(for anybody passing by: this simple task required a workaround: the UUID is returned in the format <UUID>; in the case of an e-mail that means the UUID is returned in the format <UUID@domain>. When the URL of a record is set as x-devonthink-item://<UUID@domain> it is automatically converted to mailto:UUID@domain. The solution is to remove the < and > from the returned UUID, replacing them with %3C and %3E respectively. When the URL is set to x-devonthink-item://%3CUUID%3E it is recognised as a link to an item in DT.)
Thanks a lot @Blanc! I’ve also taken a look and added a timeout. Also you can directly get the URL without getting the UUID. I’ve got to test it some more because it’s generating a lot of duplicates it seems too, not sure if that’s logic or could/should be prevented.
-- 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
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
try
if type of theRTF is rtfd then
set thePath to path of theRTF
set theGroup to parent 1 of theRecord
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" 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)
theAttachment
tell application id "DNtp"
set importedFile to import theAttachment to theGroup
set URL of importedFile to theURL
end tell
end timeout
end try
end if
end repeat
end tell
end if
on error msg
display dialog msg
end try
delete record theRTF
end if
end repeat
end tell