Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have been trying to write an Applescript to help me saving attachments, but realized quickly that I do not have the knowledge to do such a difficult script. Here is some background.

We receive daily sales reports, and flight manifests. The format in the subject should be: dd.mm.yyyy/"two letter airline code".DSR.Weight.

I.e. 30.01.2013/VS.DSR.1110KG

In the case of the manifest, in the same format, substitute DSR with MNFST.

I.e. 30.01.2013/VS.MNFST.1110KG.

There are however countless variations to the subject, and we are all human, and sometimes , is used, or more / etc. Our staff are great and work extremely hard, and the last thing they need me to do is be on their case about subject formats.

Therefore I was trying to get an Applescript to look at the two things that are always in the subject. The airlines two letter code, and the word DSR or MNFST/MFST/Manifest

We have the following two letter codes, sometimes in lower, or uppercase or a mix.

CV VS SAA TK DHV LH

Now the DSR can be in the subject in a number of ways, its not always seperated, ie. 30.01.2013/VSDSR1110KG

Same applies for MNFST/MFST/Manifest.

Ideally, I need a script to work in two parts:

DSR:

  1. Check the subject for the flight code and DSR
  2. Save the .xls attachment to /DSR/Airline two letter code
  3. If File is more recent (A correction might be sent later that day/early the next morning.), add 1, if another 2 etc.

I.e. 30.01.2013/VS.DSR.1110KG.xls: Macintosh HD/Users/(My username)/Dropbox/DSR/VS I.e. 30.01.2013/CV.DSR.1110KG.xls: Macintosh HD/Users/(My username)/Dropbox/DSR/CV

Manifest (Sometimes a .xls/.pdf/.zip file. It varies.)

  1. Check the subject for the flight code and (Manifest or MNFST or MFST)
  2. Save the .xls/.pdf/.zip attachment to Manifests/Airline two letter airline code
  3. If the file is more recent (I.e. a file conflict with the same name. A correction might be sent later that day/early the next morning.), add 1, if another 2 etc.

I.e. For VS: Macintosh HD/Users/(My username)/Dropbox/Manifests/VS

From the 3 hours I spent attempting this and getting nowhere fast, I thought someone might find this an interesting challenge, and it would be greatly appreciated!

Meanwhile, I am going to be reading up on Applescript in my free time, very powerful tool and time saver!

share|improve this question
3  
Please attach your AppleScript if you're asking us to review your AppleScript (what you've written so far). Else your question reads as asking for someone to write one. Perhaps rephrase your question to seek software or tools that does what you want? –  Global nomad Feb 13 '13 at 20:57
    
What you need is a piece of regex. This might be helpful: stackoverflow.com/questions/997828/… –  Mohsen Feb 13 '13 at 21:17
    
Note that it might be easier to provide a script to create the mail so the format is correct –  Mark Feb 13 '13 at 21:24
    
I recommend that you either post what have you tried or hire one of the many talented developers in the careers section –  adayzdone Feb 13 '13 at 22:21

1 Answer 1

up vote 4 down vote accepted

First create a rule that looks for ANY of the Manifest in all of the variations or DSR

Like So:

enter image description here

Then save this script (assuming you are using 10.8 Mountain Lion. ) In the folder ~/Library/Application Scripts/com.apple.mail/

    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule

        set codeList to {"CV", "VS", "SAA", "TK", "DHV", "LH"}
        set subFolder to ""
        set theDate to do shell script "date +%Y_%m_%d_%H%M%S"
        --set ruleName to name of theRule





        -- The folder to save the attachments in (must already exist)
        tell application "Finder" to set attachmentsFolder to ((path to home folder as text) & "Dropbox:DSR") as text

        tell application "Mail"

            repeat with eachMessage in theMessages
                set theSubject to subject of eachMessage
                repeat with i from 1 to number of items in codeList
                    set this_item to item i of codeList
                    if theSubject contains this_item then

                        set subFolder to this_item

                    end if
                end repeat
                if (count of (eachMessage's mail attachments)) > 0 then


                    try

                        tell application "Finder"

                            if not (exists folder subFolder of folder attachmentsFolder) then
                                make new folder at attachmentsFolder with properties {name:subFolder}
                            end if
                        end tell
                        -- Save the attachment
                        repeat with theAttachment in eachMessage's mail attachments

                            set originalName to name of theAttachment
                            set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName

                            tell application "Finder"

                                if (exists file originalName of folder subFolder of folder attachmentsFolder) then
                                    set savePath to attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName
                                end if
                            end tell
                            try
                                save theAttachment in file (savePath)



                            end try
                        end repeat


                    end try

                end if

            end repeat

        end tell
    end perform mail action with messages
end using terms from

And point the Run Applescript at it.

If you are using an earlier Mac OS. Then you should be able to place the script where ever you want.

Thats it.

My tests all have worked. But it is late here and I should be in bed. So let me know if any issues and I will try and sort. :-)

The one thing to note is I have added a time stamp instead of a number for same named files. It is more reliable.

share|improve this answer
    
Hey markhunte! You have probably just saved me an hour of time/day with those lines of code! 15 Days of my life per year! Many thanks. It works almost perfectly, –  user41766 Feb 14 '13 at 13:38
    
It pulls all DSRs and Manifests and puts them in DSR/Airline two letter code. Which is fine, and actually I prefer it this way as the manifests and DSR come through next to each other. Your script from a previous thread relating to mail was my base, which I can see is in this one too. I was close (and forgot to comment the sub folder dialog in your original, Had to put a tea mug on my return key for 80,000 message prompts!). Part of learning! This script also helped me alot in understanding Applescirpt. P.S. Bookmarked your blog! –  user41766 Feb 14 '13 at 13:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.