Skip navigation

Save attachments Apple Script for Mail in Lion

4285 Views 15 Replies Latest reply: Jan 29, 2013 5:54 AM by 2point5 RSS
1 2 Previous Next
jo flavour Calculating status...
Currently Being Moderated
Aug 5, 2011 5:40 AM

Hello,

 

The script below has been superb for us as we need attachments that come in from specific people to be saved automatically to a folder. We then have a series of Folder Actions that take care of things thereafter.

 

However since upgrading to Lion this script doens't seem to work. I know a few others are having problems and wondered if this is a bug with Lion or if the changes in mail mean that this script needs re-worked. Any help would be great!

 

using terms from application "Mail" -----bij mail rule aanzetten

          on perform mail action with messages theMessages for rule theRule -----bij mail rule aanzetten

 

 

  --set theOutputFolder to (here's where you tell it the main folder to save in)

                    tell application "Finder"

                              set theOutputFolder to folder "MACHD:Users:Pinstripe:Desktop:Incoming"

                    end tell

 

                    tell application "Mail"

  --comment the following line when running this script as a Mail.app rule function - uncomment it for testing

  --set theMessages to selection --------bij testen aanzetten

 

                              repeat with theMessage in theMessages

 

  --does the message have attachments? if not, then skip it

                                        if (every mail attachment of theMessage) ≠ {} then

 

  --save the attachment to the subfolder

 

                                                  set theAttachments to (every mail attachment of theMessage)

                                                  repeat with theAttachment in theAttachments

                                                            set thePath to (theOutputFolder as string) & (name of theAttachment)

 

  --if same named file already exists in subfolder, skip it   

                                                            if not (exists (alias thePath of application "Finder")) then

  save theAttachment in thePath

                                                            end if

                                                  end repeat

                                                  set read status of theMessage to true

                                        end if

 

                              end repeat

                    end tell

          end perform mail action with messages

end using terms from


iMac, Mac OS X (10.7)
  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Aug 5, 2011 12:06 PM (in response to jo flavour)
    However since upgrading to Lion this script doens't seem to work

     

    uhhh, ok...?

     

    How about telling us which bit doesn't work. Since there are multiple steps in your workflow that would be helpful.

     

    For example you say your script saves the attachments to a folder and the Folder Action then kicks in. It would help to know whether the files are getting saved to the folder or not (which actually indicates the script IS working, and that it's the Folder Action that's amiss). Also any error messages (including messages in the system log) could help.

  • hubionmac Level 2 Level 2 (435 points)
    Currently Being Moderated
    Aug 6, 2011 12:51 AM (in response to jo flavour)

    well, after a short test-run as a normal script it turned out to be the "save theAttachment in thePath" line that throws out an error... several other people encountered this "bug" in Lion's mail.

    Here they made a crazy workaround via GUI-Skripting, I could also imagine another one with a special ruby gem that is able to ready Mail's emlx-files but I would really say... wait for Apple to fix this bug... as usual the first version of the os is buggy and you should wait at least for the first update on a real productive system (sorry for this smart *** stuff, but this is what it's like)

  • bjw Level 4 Level 4 (1,325 points)
    Currently Being Moderated
    Aug 30, 2011 10:51 AM (in response to hubionmac)

    I'm having the same problem.  Script runs fine in Snow Leopard.  But in Lion this line doesn't run:

     

    save EachFile in PathToSaveAttachment & (SavedFileName)

     


  • highkm Calculating status...
    Currently Being Moderated
    Aug 30, 2011 2:34 PM (in response to bjw)

    Wow, I had the same problem. From my company's point of view this is major as it breaks many production applications. We didn't expect this from Apple. Looks like we'll have to roll back to SL.

  • MagicUK Calculating status...
    Currently Being Moderated
    Nov 20, 2011 10:49 AM (in response to jo flavour)

    Does anyone know if there is any movement on this. I am running 10.7.2 but the problem remains.

  • bjw Level 4 Level 4 (1,325 points)
    Currently Being Moderated
    Nov 20, 2011 11:10 AM (in response to MagicUK)

    My script works now.  I'm still using the line I referenced in an earlier post and it works now.

     

    Distantly related:  I have an Autormator workflow that attaches a file to a mail message and send it.  That one works up until it's supposed to send.  I had to add AppleScript to send the keystrokes to send it.

     

    My point in mentioning this is that Apple has been really flaky over the years with AppleScript and Automator as they related to handling mail messages and attachments.  

     

    If you rely on this stuff you can't upgrade without testing or having a willingness to adapt.

     

    Here's my entire script...

     

     

    using terms from application "Mail"

              on perform mail action with messages MatchingMessages

      --********Change the next line to reflect a folder location you want to save incoming images to  ****

                        set PathToSaveAttachment to "Macintosh HD:Users:murphy:Documents:processes:rotation:in:"

                        tell application "Mail"

                                  repeat with CurrentMsg in MatchingMessages

                                            set AttachedFiles to mail attachments of CurrentMsg

                                            repeat with EachFile in AttachedFiles

                                                      set SavedFileName to "inbound.jpg"

      save EachFile in PathToSaveAttachment & (SavedFileName)

                                            end repeat

                                  end repeat

                        end tell

     

      --Perform Rotation

                        tell application "Image Events"

      --*******The next line should point to the folder you referenced above  *****

                                  set theFile to "Macintosh HD:Users:murphy:Documents:processes:rotation:in:inbound.jpg"

                                  set thisImage to open theFile

      rotate thisImage to angle 90

      save thisImage

                        end tell

     

      --Strip exif and move file to outbox

     

      --  ** Important Notes:

      -- The do shell script section below runs three Terminal commands, separated by semicolons.

      -- This first uses the jhead command to wipe out the exif rotation information.

      -- The second moves the file to another folder.

      -- The third kicks off an Automator workflow to mail the file back to yourself.

      -- See the post for more information on the workflow.

      -- jhead needs to be downloaded and placed in the folder you'll call it from.

      -- In this case jhead was placed in a folder called /Users/murphy/lab/in

      -- jhead also needs to be made executable.  See the post for more details.

      -- the move isn't really necessary.  You could alter the script and the automator workflow to skip it.

      -- it was helpful to have the move for troubleshooting while working out the script.

     

                        do shell script "/Users/murphy/Documents/processes/rotation/jhead -norot Users/murphy/Documents/processes/rotation/in/inbound.jpg; mv /Users/murphy/Documents/processes/rotation/in/inbound.jpg /Users/murphy/Documents/processes/rotation/out/outbound.jpg; automator /Users/murphy/Documents/processes/rotation/mail_exif_corrected_photos.workflow"

     

                        tell application "System Events"

                                  tell application "Mail" to activate

                                  delay 5

      keystroke "d" using {command down, shift down}

                        end tell

     

              end perform mail action with messages

     

    end using terms from

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Nov 20, 2011 11:14 AM (in response to MagicUK)

    Jo flavour's script also seems to work without any modification under Mac OS X 10.7.2.

  • veshman9 Level 1 Level 1 (5 points)
    Currently Being Moderated
    Jul 18, 2012 9:00 AM (in response to jo flavour)

    the original script from Jo Flavour works with 10.7.4 as well. It's a great script. Easy to modify (bascially just the folder path) and worked perfectly the first time I tried it.

  • w1nner Calculating status...
    Currently Being Moderated
    Nov 23, 2012 7:42 AM (in response to jo flavour)

    I need you help. My mac send me error in Console - 23.11.12 17:32:34,054 sandboxd[2255]: ([2071]) Mail(2071) deny file-write-create /Users/rostislav/Desktop/Incoming/xxx.xxx

  • hubionmac Level 2 Level 2 (435 points)
    Currently Being Moderated
    Nov 27, 2012 9:09 PM (in response to w1nner)

    Well, this has something to do with 10.8. security features... Apple Mail is not allowed to save attachments in another place than the downloads...

    see also this thread or take a look here

    The trick is to save the file in the downloads and move it somewhere else with another command -> Security Feature bypassed ;-)

  • 2point5 Level 3 Level 3 (955 points)
    Currently Being Moderated
    Jan 29, 2013 4:39 AM (in response to jo flavour)

    I'll wade into this and put my two cents in for what it is worth.

     

    I've been using the Mail save attachment script, pretty much the same as that which "jo flavour" describes, for a long time and it worked flawlessley until I upgraded to Mountain Lion.  At the time I didn't make the connection that something had changed in ML.  I wasn't getting any error messages... the script just did nothing or so it seemed.

     

    I have a rule in Mail that says "If the message is from a person who is a member of such and such group AND the attachment is an XLS sheet then then run the script and save the attachment to the Desktop and then play a sound to alert me"

     

    This was the orginal script.

     

    (* save attachments of selected messages

    AK IT Carlow April 2006

    will crash if file already exits

    *)

    using terms from application "Mail"

              on perform mail action with messages theMsges

                             set theAttachmentPath to "Macintosh HD:Users:username:Desktop:"

                             tell application "Mail"

                                            repeat with ThisMessage in theMsges

                                                           set Attached to mail attachments of ThisMessage

                                                           set FileIndex to 0

                                                           repeat with ThisAttach in Attached

                           save ThisAttach in theAttachmentPath & (name of ThisAttach)

                                                           end repeat

                                            end repeat

                             end tell

              end perform mail action with messages

    end using terms from

     

    using terms from application "Mail"

              on run

                             tell application "Mail" to set sel to selection

                             tell me to perform mail action with messages (sel)

              end run

    end using terms from


    Well that was all spot on before ML came along.

     

    On doing a bit of research I found another script that included an instruction to tell Finder to set the path rather than the statement above which implies to tell Finder to set the path.

     

    So I edited the script as follows:

     

    using terms from application "Mail"

         on perform mail action with messages theMsges

               tell application "Finder"

                    set theAttachmentPath to "Macintosh HD:Users:username:Desktop:"

              end tell

              tell application "Mail"

                   repeat with ThisMessage in theMsges

                        set Attached to mail attachments of ThisMessage

                        set FileIndex to 0

                        repeat with ThisAttach in Attached

                             save ThisAttach in theAttachmentPath & (name of ThisAttach)

                        end repeat

                   end repeat

              end tell

         end perform mail action with messages

    end using terms from.

     

    using terms from application "Mail"

      on run

            tell application "Mail" to set sel to selection

            tell me to perform mail action with messages (sel)

      end run

    end using terms from

     

    It now works flawlesly again.  I don't know why it has made the difference although the only thing I can think of is that there is a bit more rigidity in coding now in that you have to be more explicit with anything that is passed from one application to another.

     

    Hope that helps someone.

     

    Chrispy

  • MagicUK Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 29, 2013 5:23 AM (in response to 2point5)

    Mmm tried this and still no banana.

     

    Oh and before anyone suggests I change "username" I did that ;-)

  • 2point5 Level 3 Level 3 (955 points)
    Currently Being Moderated
    Jan 29, 2013 5:29 AM (in response to MagicUK)

    I am assuming that you are running the script from  ~/Library/Application Scripts/com.apple.mail/

     

    Chrispy

  • MagicUK Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 29, 2013 5:34 AM (in response to 2point5)

    Yeah. MailRules won't let you select the script unless its in there.

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.