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

Applescript needed to move Mail attachment to folder

Hi,


I am in need of a script to add to a Mail Rule that will take an attached image (JPEG) and place it in a folder with the name indicated in the body "message" section of the email, and also renaming that file.


Example:



Rule will use "Subject" - "contains" - "Xfer Image"


The email will contain something like this in the body "message" section of the email.


itemName - Rose_Flower_001.

itemNumber - Fl-652



I would like the mailed JPEG image Attachment copied over to a folder called (itemName) which would be place within the following path:


(Folder with the itemName) ( file renamed to itemNumber)


"/Volumes/Ron/Dropbox/Images/Rose_Flower_001/Fl-621.jpg


Then after it's done this I would like to have the that email moved from the inbox to a sub-folder called "Image Folder Created"


Any and all help or suggestions would be greatly appriciated.



Hopefully this can be done....

I really need this for my start-up business and it would save me a lot of time making folders and managing emails with attachments.



Thanks in advance....


Ron

iMac, Mac OS X (10.7.3)

Posted on Apr 22, 2013 5:52 PM

Reply
31 replies

Apr 22, 2013 7:41 PM in response to Ron60

Hey Ron,


I can help you out with this... but I get confused when reading what you want to do exactly.


I understand the mail rule part.


Can you explain better the steps after mail rule gets activated?


for instance I am not sure what you mean by:


The email will contain something like this in the body "message" section of the email.


itemName - Rose_Flower_001.

itemNumber - Fl-652

Are the jpg attachments the same name or completely different?


Thanks


Phil

Apr 22, 2013 7:59 PM in response to pjdube

Thanks for helping Phil!



Every email with an image attachement will have the name of the file and and name of the folder included in the message section of the email. itemName for the folder, and itemNumber for the file name within that folder.


In this 1st example it would be the following


itemName - Rose_Flower_001.

itemNumber - Fl-652


Then 2nd email the itemName and itemNumber will have a different names to use for the Folder and File name.


like this


itemName - Lillies_002

itemNumber - Li-023



So the 1st email would have a folder (using the itemName) Rose_Flower_001 with a file (itemNumber) Fl-652.jpg within that folder.



The 2nd email would have a folder(using the itemname) Lillies_002 with a file (itemNumber) Li-023.jpg within.


And so on.......


All emails with attachement will have the same subject name of "Xfer Image"


I hope this make it clearer.....


Ron


Apr 22, 2013 9:28 PM in response to pjdube

Okay here is a image of a cake....


I need a folder called "Birthday Cake_001

And the inclosed image needs to be renamed to BC-32.jpg and stored inside the new folder.

User uploaded file


The next email the image attachment, ImageName and ImageNumber will be completely different the only thing that will be the same on all the emails will be the


Subject name


And the field names of:


itemName -

itemNumber -


Once completed..... Then this email needs to go from my inbox to a subfolder "Image Folder Created"

Apr 22, 2013 9:49 PM in response to pjdube

The neme of the image attachment should not be part of the script because I will be having different photographers having there camera setup differently for images naming.


One might be 8392740837032790XY.jpg

and some one else may have an image called Cake.jpg


Currently the only thing that all camera images have in common will be the format of .jpg.


If this is an issue I could have the photographers call all future file the same name. Like

Image1.jpg

image2.jpg

image3.jpg


I hope this doesn't make things too difficult.


Ron

Apr 23, 2013 1:33 PM in response to Ron60

If you are not using Mountain Lion, the following script might do most of what you are asking for. Nothing to lose by trying it. (Under Mountain Lion, the script works fine when messages are selected and the "Apply Rules" menu comand is performed, but it doesn't work at all on incoming emails.)


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule


set theBasePath to "/Users/ron/Dropbox/Images" -- POSIX path


tell application "Mail"

repeat with theMessage in theMessages

set theContent to content of theMessage

set N to countparagraphs of theContent


repeat with k from 1 to N

set thisParagraph to paragraphk of theContent

if thisParagraph begins with "itemName" then

set theFolderName to rich text 12 through -1 of thisParagraph

exit repeat

end if

end repeat


repeat with k from 1 to N

set thisParagraph to paragraphk of theContent

if thisParagraph begins with "itemNumber" then

set theFileName to rich text 14 through -1 of thisParagraph & ".jpg"

exit repeat

end if

end repeat


if existsmail attachment 1 of theMessage then

set thisAttachment to mail attachment 1 of theMessage

my getFile(theBasePath, theFolderName, name of thisAttachment)

savethisAttachmentinresult

set read status of theMessage to true

end if

end repeat

end tell


end perform mail action with messages

end using terms from


on getFile(theBasePath, theFolderName, theFileName)

set theBaseFolder to POSIX filetheBasePath

tell application "Finder"

if not (existsfoldertheFolderName of foldertheBaseFolder) then

makenewfolder in foldertheBaseFolderwith properties {name:theFolderName}

end if

repeat until existsfoldertheFolderName of foldertheBaseFolder

-- just wait a little

end repeat

set theFilePath to POSIX path of (foldertheFolderName of foldertheBaseFolder as text) & theFileName

end tell

return POSIX filetheFilePath-- the attachment file

end getFile


Message was edited by: Pierre L.

Applescript needed to move Mail attachment to folder

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