1 2 3 Previous Next 31 Replies Latest reply: Sep 4, 2013 4:47 PM by Pierre L.
Ron60 Level 1 Level 1 (0 points)

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)
  • 1. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    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

  • 2. Re: Applescript needed to move Mail attachment to folder
    Ron60 Level 1 Level 1 (0 points)

    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

     


  • 3. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    Ok. Sort of getting it now.

     

    Do you mind doing a screen shot of a sample email with the attachment?

     

    Just open the email and do Command-Shift-4-spacebar and click the window.

     

    Then upload it so I can see. Thanks.

  • 4. Re: Applescript needed to move Mail attachment to folder
    Ron60 Level 1 Level 1 (0 points)

    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.

    Screen Shotcopy2.png

     

    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"

  • 5. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    Ok good, almost there! One last question, which is very important to know, in the example you gave above, what is the actual name of the jpg that is attached to the email?

     

    Once I have this I will work out a script for you. Bare with me, I am not a pro but I know the basics.

  • 6. Re: Applescript needed to move Mail attachment to folder
    Ron60 Level 1 Level 1 (0 points)

    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

  • 7. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    Ok great. Just trying to establish that the jpg name was different than what you had named in the message body.

     

    Let me get back to you.

     

    Phil

  • 8. Re: Applescript needed to move Mail attachment to folder
    Pierre L. Level 4 Level 4 (3,860 points)

    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 messages theMessages for rule theRule

     

            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 count paragraphs of theContent

     

                    repeat with k from 1 to N

                        set thisParagraph to paragraph k 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 paragraph k 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 exists mail attachment 1 of theMessage then

                        set thisAttachment to mail attachment 1 of theMessage

                        my getFile(theBasePath, theFolderName, name of thisAttachment)

                        save thisAttachment in result

                        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 file theBasePath

        tell application "Finder"

            if not (exists folder theFolderName of folder theBaseFolder) then

                make new folder in folder theBaseFolder with properties {name:theFolderName}

            end if

            repeat until exists folder theFolderName of folder theBaseFolder

                -- just wait a little

            end repeat

            set theFilePath to POSIX path of (folder theFolderName of folder theBaseFolder as text) & theFileName

        end tell

        return POSIX file theFilePath -- the attachment file

    end getFile

     

    Message was edited by: Pierre L.

  • 9. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    Good job!!!

  • 10. Re: Applescript needed to move Mail attachment to folder
    Pierre L. Level 4 Level 4 (3,860 points)

    Oops…

     

    Please just replace

            my getFile(theBasePath, theFolderName, name of thisAttachment)

    with

            my getFile(theBasePath, theFolderName, theFileName)

    in the previous script.

  • 11. Re: Applescript needed to move Mail attachment to folder
    Pierre L. Level 4 Level 4 (3,860 points)

    pjdube wrote:

     

    Good job!!!

     

    Have you tried the script? Does it work as expected?

  • 12. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    Yes it worked! I just tried it!

     

    Ron is going to be very pleased!!!

     

    Thanks for your help on this.

     

    By the way, where did you learn applescripting?

  • 13. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    Here I am showing you the result, after I sent an email to myself with the Subject: Xfer Image:

     

    Screen Shot 2013-04-23 at 5.34.14 PM.JPG

  • 14. Re: Applescript needed to move Mail attachment to folder
    pjdube Level 3 Level 3 (830 points)

    I just tried it with the same itemName but with a different itemNumber and it detected that the folder existed and filed the image in that folder.

     

    Good job man!

1 2 3 Previous Next