Applescript to import mail with attachments into devonthink that would expand archived files My workflow is to tag and put all incoming docs from Mail.app into Devonthink. I do this with Applescript. Script is working fine for me with one exception - I do not know how to make it expand .zip and .rar files in the attachments before moving them to Devonthink. Probably would require Unarchiver or some other scriptable archive utility. I would appreciate your suggestions. My script is below.

property pNoSubjectString : "(no subject)"

tell application "Mail"
    try
        tell application id "com.devon-technologies.thinkpro2"
            if not (exists «class DTcu») then error "No database is in use."
        end tell
        set theSelection to the selection
        set theFolder to (POSIX path of (path to temporary items))
        if the length of theSelection is less than 1 then error "One or more messages must be selected."
        repeat with theMessage in theSelection
            my importMessage(theMessage, theFolder)
        end repeat
    on error error_message number error_number
        if error_number is not -128 then display alert "Mail" message error_message as warning
    end try
end tell

on importMessage(theMessage, theFolder)
    tell application "Mail"
        try
            tell theMessage
                set {theDateReceived, theDateSent, theSender, theSubject, theSource, theReadFlag} to {the date received, the date sent, the sender, subject, the source, the read status}
                tell application "MailTagsHelper"
                    tell theMessage
                        set theTags to the keywords
                    end tell
                end tell
            end tell
            if theSubject is equal to "" then set theSubject to pNoSubjectString
            set theAttachmentCount to count of mail attachments of theMessage
            tell application id "com.devon-technologies.thinkpro2"
                set theGroup to «class DTig»
                if theAttachmentCount is greater than 0 then set theGroup to «event DTpacd08» {name:theSubject, «class DTty»:«constant DtypDTgr»} given «class DTin»:theGroup
                set theRecord to «event DTpacd08» {name:theSubject &amp; ".eml", «class DTty»:«constant Dtyp****», «class DTcr»:theDateSent, «class DTmo»:theDateReceived, URL:theSender, «class conT»:(theSource as string), «class tags»:theTags} given «class DTin»:theGroup
                set «class DTur» of theRecord to (not theReadFlag)
            end tell
            repeat with theAttachment in mail attachments of theMessage
                set theFile to theFolder &amp; (name of theAttachment)
                tell theAttachment to save in theFile
                tell application id "com.devon-technologies.thinkpro2"
                    set theAttachmentRecord to «event DTpacd01» theFile given «class DTto»:theGroup
                    set «class DTur» of theAttachmentRecord to (not theReadFlag)
                    set URL of theAttachmentRecord to theSender
                    set «class tags» of theAttachmentRecord to theTags
                end tell
            end repeat
        on error error_message number error_number
            if error_number is not -128 then display alert "Mail" message error_message as warning
        end try
    end tell
end importMessage
I can see two options:

First is to save the file and simply use the open command like is it shown at the link below. I tried it and it works well enough.

https://stackoverflow.com/questions/13651533/create-an-applescript-or-shell-script-to-unzip-and-rename-files

Alternatively, you can use a do shell script command to use the unzip command line tool to do the same thing like shown here,

https://discussions.apple.com/message/9062795#9062795

But extracting the archive contents is the easy part. Does the archive have a structure? Will it always be the same? Here is where you may run into trouble.

Hope this helps.