You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript to move email attachments

i am new to mac and have no clue how to make a script


i want to have all my email attachments from one sender go to a folder on my network drive and automaticly print all pdf's any help would be greatly appreciated

MacBook Air, iOS 7.1

Posted on Mar 25, 2014 9:01 PM

Reply
17 replies

Mar 26, 2014 7:16 AM in response to markgbb

The first part of your request is straigthforward enough. Here's an applescript that will move the attachments. Read the instructions carefully so you understand how it works:


Automatically save Mail attachments using AppleScript | AppleScript ...


The second part of your request is more complicated as it depends on a number of things we don't know:


i. is the network folder on a mac or a pc machine?


ii. if it's on a mac, do you have admin permissions?


iii. what is your default pdf software?

Mar 26, 2014 5:46 PM in response to softwater

cant get the script to work tried to set it up on my local drive


on perform_mail_action(ruleData)



-- The folder to save the attachments in (must already exist)

set attachmentsFolder to ((path to home folder as text) & "users:mark:dwg") astext



-- Save in a sub-folder based on the name of the rule in Mail

set subFolder to name of |Rule| of ruleData as text

tell application "Finder"

if not (existsfoldersubFolder of folderattachmentsFolder) then


makenewfolderatattachmentsFolderwith properties {name:subFolder}

end if

end tell



-- Get incoming messages that match the rule

tell application "Mail"

set selectedMessages to |SelectedMessages| of ruleData

repeat with theMessage in selectedMessages



-- Get the date the message was sent

set {year:y, month:m, day:d, hours:h, minutes:min} to theMessage'sdate sent

set timeStamp to ("" & y & "-" & my pad(m as integer) & "-" & my pad(d) & "-" & my pad(h) & "-" & my pad(min))



-- Save the attachment

repeat with theAttachment in theMessage'smail attachments

set originalName to name of theAttachment

set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & " " & originalName

try


savetheAttachmentinsavePath

end try

end repeat

end repeat

end tell


end perform_mail_action


-- Adds leading zeros to date components

on pad(n)

return text -2 thru -1 of ("00" & n)

end pad




i set mail rules to

from = to the sender

run apple script


im sure i am missing something but dont know what


the work flow i am after is attachments from only one sender saved to "dwg" folder and pdf's saved to "dwg/po" then automaticly print the pdf i would prefer to have the attachments saved to my network drive the path for the drive is "afp://AirportBase._afpovertcp._tcp.local/Data/dwg" i am using preview to print the pdf files


thanks for the help

Apr 8, 2014 1:20 AM in response to markgbb

You need to do three things; install a script, set up a mail rule, and set up folder actions.




Part 1: Install Script


Copy and paste the script below into an AppleScript editor window:


using terms from application "Mail"

on perform mail action with messagestheMessages

set attachmentsFolder to "Volumes:<name of volume:<name of folder>:" as rich text

--remove the angle brackets, but keep the colons, including the last one


tell application "Mail"

set selectedMessages to theMessages

try

repeat with theMessage in selectedMessages


repeat with theAttachment in theMessage'smail attachments

set originalName to name of theAttachment

set savePath to attachmentsFolder & originalName



savetheAttachmentinfilesavePath

end repeat

end repeat

end try


end tell


end perform mail action with messages

end using terms from


Save this script with a name like 'CopyAttachments' in


~/Library/Application Scripts/com.apple.mail



(note:

~/Library means your user library. You can find it by triple-clicking the path above, then control-click on the highlited texgt and choose Services > Reveal in Finder

)




Part 2: Set up Mail Rule


Open Mail.app. Click on your Inbox in the sidebar. Click ‘Mail > Preferences... > Rules > Add Rule'


Under ‘Description’ give the rule a name (e.g., ‘Copy attachments’)


Set ‘If ANY of the following conditions are met:


to


‘From contains’


and the email address of the person whose attachments you want to target.


(note:

You can add more than one person’s email if you wish, but you do so by hitting the ‘+’ key and adding a new condition, not by adding more than one address in the text field. Each text field must contain only one condition (i.e., email address or keyword).

)


Next, set ‘Perform the following actions:’


to


‘Run AppleScript’


Click the ‘No Script Selected’ button and choose ‘CopyAttachments’


Click ‘OK” and in the following dialog click ‘Apply’.




3. Create & Set up a Folder Action


Open ‘Automator.app’. From the open panel choose ‘Folder Action’.


In the large, empty panel at the top you’ll see


Folder Action receives files and folders added to Choose folder


Click the ‘Choose folder’ menu, choose ‘Other’ and select the folder you want the attachments to be saved in.


In the filter/search bar on the left of the Automator window, type ‘print images’. Drag the ‘Print Images’ selection from the results list into the middle of the empty workflow and release.


You can set some options here if you like (‘scale to fit’ might be useful).

You can choose either ‘Default Printer’ or click to select your actual printer. If your actual printer is the default, it won’t make any difference.


Press ‘command-S’ on your keyboard to save. Supply a name (e.g. Print PDFs) and hit ‘OK’. You do not choose a save location.


Quit Automator.


Open a Finder window and navigate to the folder where the attachments are going to be saved.


Hold down the ‘control’ key on your keyboard and click the attachments folder. From the contextual menu, go to ‘Services > Folder Actions Setup…' and click to open the dialog box.



Navigate down the list of scripts till you see the name of the Automator action you saved above and select it. Click ‘Attach’.


In the parent dialog box, check the box at the top that says ‘Enable Folder Actions’ and ensure that in the list on the left the attachments folder is listed and checked. Check that on the right, the ‘Print PDFs.workflow’ is checked.


If all is in order, close the dialog box. The procedure is complete and the workflow is installed.


It'd be wise to test the script as soon as possible. If it fails to work, double check that you've entered the correct path in the AppleScript as that's the most likely point of failure.

May 3, 2014 12:03 PM in response to Phil Stokes

I am not getting any attachments saved, and I have a feeling it is due to errors in the line setting the destination folder.


Phil Stokes wrote:


set attachmentsFolder to "Volumes:<name of volume:<name of folder>:" as rich text

--remove the angle brackets, but keep the colons, including the last one





Being extremely specific, if I have a volume called "Macintosh HD" and I want to put the files in a folder called "Attachments" that exists in my home folder and my username is Gordon, what should this "set" line look like?

Oct 27, 2014 8:39 AM in response to Phil Stokes

Phil,


I used your Applescript but modified it just a bit (attached below is the code). What I am wanting to do in addition to move the attachment is to also rename the attachment with the subject line of the email. I have managed to do this with one exception, iIf the email message has more than 1 attachment, the rule as I have it written, will only process the last attachment in the email. Actually, what I suspect is happening is the rule/applescript is working but it overwrites the first attachment with the second one. Any help on how to have the applescript accommodate multiple attachments and rename them by appending _1, _2, etc to subsequent attachments?


Thanks


using terms from application "Mail"

on perform mail action with messages theMessages

set attachmentsFolder to "Volumes:Macintosh HD:Users:tboothe:Desktop:WorkOrders:" as rich text

tell application "Mail"

set selectedMessages to theMessages

try

repeat with theMessage in selectedMessages

repeat with theAttachment in theMessage's mail attachments

set subjectText to (get theMessage's subject) as rich text

set originalName to ((every word of subjectText) as rich text)

set savePath to attachmentsFolder & originalName

save theAttachment in file savePath

end repeat

end repeat

end try

end tell

end perform mail action with messages

end uend using terms from


T


T

Oct 27, 2014 9:02 AM in response to Tommy Boothe

I haven't tested this, but try this modified version, which in theory should increment the name:


using terms from application "Mail"

on perform mail action with messagestheMessages

set attachmentsFolder to "Volumes:Macintosh HD:Users:tboothe:Desktop:WorkOrders:" as rich text

tell application "Mail"

set selectedMessages to theMessages

try

repeat with theMessage in selectedMessages

set counter to 1 -- added

repeat with theAttachment in theMessage'smail attachments

set subjectText to (get theMessage's subject) as rich text

set originalName to ((every word of subjectText) as rich text)

set savePath to attachmentsFolder & originalName & (counter as rich text) --modified

set counter to counter + 1 -- added


savetheAttachmentinfilesavePath

end repeat

end repeat

end try

end tell

end perform mail action with messages

end using terms from

Oct 27, 2014 11:43 AM in response to Phil Stokes

Thanks Phil. Is there any way to change the numerical suffix using something other than “+1”? The problem I have is that I have attachments related to construction sites, let’s say Elm 123. If I receive 2 attachments labeled image001.jpg and image002.jpg related to this job, the script is renaming them Elm 123 and Elm 124. I need them to remain Elm 123 because that references the specific job so I need the script to change the names to Elm 123 and Elm 123_1.


Tommy

Oct 27, 2014 8:05 PM in response to Tommy Boothe

Change this line:


set savePath to attachmentsFolder & originalName & (counter as rich text) --modified


to


set savePath to attachmentsFolder & originalName & "_" & (counter as rich text) --modified


This will save the first attachment (even if there's only 1) to (for example) "....elm 123_1" and the second and subsequent ones to "....elm 123_2", "...elm 123_3" and so on.


Will that do, or must the first attachment really have no numerical identifier at all (that's doable, but it adds a bit more complexity, and early reports suggest there's a bug in Yosemite that'll make that difficult, too)?

Jan 5, 2015 2:09 PM in response to Phil Stokes

Phil,


I am completely new to Applescript, but I have tried your script to automate saving attachments from email. The script is working quite nicely, however not entirely the way I need it to work. Basically. I have a dedicated mailbox for all incoming documents (either scanned, sent be email or sent by fax). Now I want to schedule the mailbox to be "read out" weekly and go through all unread incoming email and save their attachments. I noticed that using your Applescript, nothing really happens with attachments of email that have been sent while my Mac was switched off. (I get quite come documents each day and I also have a workflow in place to OCR files so that I can search them afterwards) So therefore I want to schedule reading out this mailbox once a week on a Sunday night and to automatically execute the Applescript. Of course I need to be sure that I have all attachments saved. In your script it seems to me that the script only works for the messages that are selected? Any idea how to modify the script so that it works for all unread messages? Ideally after saving the attachment, the status should be set to read or moved to the archive although so that they won't be saved again in the next round.


I combined your script with a script I found elsewhere to come up with this:


using terms from application "Mail"

on perform mail action with messagestheMessages



-- see the example at the end of my blog post (link in the header) for how to format this line:

set attachmentsFolder to "Macintosh HD:Users:Wouter:inbox:" as rich text


tell application "Mail"


activate

tell (messages of mailbox "INBOX" of account "Google" whose read status is false)

set unread_messages to it

set unread_count to count

end tell


display dialog "Total unread messages: " & unread_countbuttons {"OK"} default button 1


try


repeat with each_message in unread_messages


set originalName to name of theAttachment


set savePath to attachmentsFolder & originalName


savetheAttachmentinfilesavePath


end repeat

end try


display dialog "Total unsaved attachments: " & unread_countbuttons {"OK"} default button 1

end tell


end perform mail action with messages

end using terms from

Unfortunately, I does not seem to work ! I added to display dialogs to make sure that I am actually getting to the end of the script. Any idea or suggestion to improve the script would be very welcome!

Mar 6, 2015 1:12 AM in response to Phil Stokes

Phil, I found your post on how to save and print email attachments since I was trying to do the same thing as Markgbb, however I have one question. I would like the apple script to rename the saved attachment to a combination of a specific name (in this case "journal order") followed by a space and then the email address of the sending email account. I have already setup the functionality as you specified above and it works well, I just don't know enough about applescript to also have it change the name the way I want. Any help on modifying the script you listed above would be great. Thank you - Rick

Aug 10, 2015 12:18 PM in response to Phil Stokes

Phil,


Thanks again for the help on this. The problem I have with the rule/Applescript is that I often receive multiple emails that should be processed by the rule and only 1 of them is affected. My mail is set to check for messages ever 5 minutes. So I may get 10 messages that all come in at the same time and the run/Applescript is only run on one of them. Any ideas as to why and how to correct this?

applescript to move email attachments

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.