Bulk Save Gmail Attachments with Mail.app and Automator February 16th, 2018

This is my first post in quite a while! I wanted to share a little tool I put together to address a problem I’m sure many of us have faced. My Gmail account was nearing its quota, so I wanted to download all the photos and movies attached to the older emails before I went and deleted all the older stuff to make space.

There is no way to do this natively in Gmail, and the only other solutions I could find for bulk saving Gmail attachments were browser add-ons and Office macros that I didn’t feel comfortable running against my Google account. So, I developed a workflow using Mail.app, Automator, and a bit of AppleScript (see below). Yes, this is a heavily Mac-centric solution, but you use the tools that are available to you, right?

  1. Configure Mail.app to sync with your Gmail account
  2. Start Automator and select New Workflow
  3. Drag the “Get Selected Mail Items” action from the Library over to the workflow area. Make sure “messages” is selected in the drop down menu next to “Get selected”.
  4. Drag the “Run AppleScript action from the Library to the workflow area. Paste the following code into the text box of the script action:
    on run {input, parameters}
        set attachmentsFolder to ((path to desktop folder) & "Gmail Attachments") as text
        tell application "Finder"
            if not (exists folder attachmentsFolder) then
                display dialog "Folder doesn't exist: " & attachmentsFolder
            end if
        end tell
        tell application "Mail"
            repeat with eachMessage in input
                set {year:y, month:m, day:d, time:t} to date received of eachMessage
                set hr to t div hours
                set min to t mod hours div minutes
                set sec to t mod minutes
                set datestr to (y * 10000 + m * 100 + d) as string
                set timestr to (hr * 10000 + min * 100 + sec) as string
                repeat with eachAttachment in eachMessage's mail attachments
                    set originalName to name of eachAttachment
                    set savePath to attachmentsFolder & ":" & datestr & "-" & timestr & "-" & originalName
                    save eachAttachment in file savePath   
                end repeat
            end repeat
        end tell
    end run

    Note: this code assumes you have a writable folder called ~/Desktop/Gmail Attachments. You can change that path if you so desire.

  5. In Mail.app, highlight the messages you want to extract the attachments from. You can, for example, search for messages older than a particular date, or those that have JPEG attachments, etc.
  6. Back in Automator, click “Run” in the top right corner or hit Cmd+R. This will start the workflow which will go through each attachment from each highlighted message, and save it with a time-stamped filename based on the date and time the message was received.

Hopefully this is helpful. Leave me a comment if you have any feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>