Skip navigation

Applescript to save mail attachments failing in Mountain Lion

16743 Views 52 Replies Latest reply: Feb 3, 2014 6:09 AM by LogicProLetsGo RSS
  • pfly Calculating status...

    did a repair permissions.  found a few small things, but not mail or applescript related. 

     

    I read this article.  It explains a little why I can manually run the script, but it isn't automating.  But, I think I've got everything in the right place... script is stored in the com.apple.mail folder of my user's library. 

     

    http://www.macworld.com/article/1165641/how_increased_mac_security_measures_will _impact_applescript.html

  • biggerfish Calculating status...

    I have a similar problem, and can't figure out how to implement the solution into my own script. Here's the basis of what I'm using, and it fails:

     

    (*

              Just trying to get the attachments to save to somewhere.

    *)

     

    using terms from application "Mail"

      on perform mail action with messages selectedMsgs

      tell application "Mail"

                                  repeat with counter from 1 to (count of selectedMsgs)

      set msg to item counter of selectedMsgs

                                            set theAttachments to (count of mail attachments of msg)

                                            say "There are " & theAttachments & " attachments."

      repeat with attach_counter from 1 to theAttachments

      set thefilename to SaveAttachments(name of mail attachment attach_counter of msg)

      --set theid to id of mail attachment attach_counter of msg--I don't think  you need this.

      save mail attachment attach_counter of msg in thefilename --The full path is returned from the handler, and the file is saved.

      end repeat

      end repeat

      end tell

      end perform mail action with messages

    end using terms from

    -------------

    on SaveAttachments(x) --This creates the path to the documents folder to save the attachment, then adds the name of the file.

              set dp to (path to documents folder as Unicode text) & x

      return dp

    end SaveAttachments

     

    -- If run as an ordinary script, instead of directly from the Scripts

    -- menu, it will call the default handler instead.

    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

  • pfly Level 1 Level 1 (0 points)

    I ended up using automator to do what I wanted.  Downside is I have to initate the action.  Upside is it works.

  • biggerfish Level 1 Level 1 (5 points)

    pfly, I can deal with that. Can you be more specific?

  • pfly Level 1 Level 1 (0 points)

    So what I needed to do was collect attachments sent to a specific inbox, save the attachment and combine the attachements (pdf) into one large pdf.  Here's the automator process which I saved as a service.

     

    service receives no input in mail

     

    get selected mail mailboxes

         get selected mailboxes

     

    get attachments from mail messages

     

    combine PDF pages

         combine by appending

     

    name single item

         name single item

              name: baseline only to: blahblahblah

     

    move finder items

         to: destination folder

     

    get specified finder items    

         temp (the temp directory that we set up at the beginning)

     

    move finder items to trash

  • JerryDale Calculating status...

    This script works but i need to be able to have it rename the files a they come in and add a sequencial numbering. I can put a name in the subject line of the emails.  What do I add to this script?

     

    using terms from application "Mail"

        on perform mail action with messages matching_messages

            --set report_type to ":throughput"

            set folder_path to "Macintosh HD:Users:jkinder:Downloads:Mail Attach" -- & report_type

            set file_path to (folder_path & ":")

            tell application "Mail"

                set the num_messages to the count of matching_messages

                -- display dialog "0. Found " & num_messages & " messages!"

                repeat with i from 1 to the num_messages

                    set this_message to (item i of matching_messages)

                    tell this_message

                        -- display dialog "1. Processing \"" & (subject of this_message) & "\" which has " & (count of mail attachments of this_message) & " attachment(s)."

                        repeat with z from 1 to the (count of mail attachments of this_message)

                            set theAttachment to item z of (mail attachments of this_message)

                            -- display dialog "2. Got attachment " & (name of theAttachment)

                            with timeout of 10 seconds

                                set myname to do shell script "whoami"

                                -- display dialog "3.Trying to save " & (name of theAttachment) & " to the path " & (file_path & (name of theAttachment) as string) & " as user " & myname

                                try

                                    save theAttachment in (file_path & name of theAttachment)

                                on error number errNum

                                    display dialog "ERROR: problem saving file, ernum: " & errNum

                                    error number -128 --User Ended error; this should stop script execution

                                end try

                            end timeout

                        end repeat

                    end tell

                end repeat

            end tell

        end perform mail action with messages

    end using terms from

     

     

    Thank you,  JD

  • biggerfish Level 1 Level 1 (5 points)

    JD are you using Mountain Lion and the newest version of Mail? I can't get your script to run on my machine at all - I still get the -10000 error trying to save attachments from Mail.

     

    tell application "Mail"

     

              set folder_path to "Macintosh HD:Users:richg:BigPromotions:2012 ClientProjects:SavedAtt"

      set file_path to (folder_path & ":")

              set matching_messages to item 1 of (get selection)

      set the num_messages to the count of matching_messages

     

              display dialog "0. Found " & num_messages & " messages!"

     

              repeat with i from 1 to the num_messages

      set this_message to (item i of matching_messages)

      tell this_message

     

                                  display dialog "1. Processing \"" & (subject of this_message) & "\" which has " & (count of mail attachments of this_message) & " attachment(s)."

     

                                  repeat with z from 1 to the (count of mail attachments of this_message)

      set theAttachment to item z of (mail attachments of this_message)

     

                                            display dialog "2. Got attachment " & (name of theAttachment)

     

      with timeout of 30 seconds

                                                      set myname to do shell script "whoami"

                                                      display dialog "3.Trying to save " & (name of theAttachment) & " to the path " & (file_path & (name of theAttachment) as string) & " as user " & myname

                                                      try

      save theAttachment in (file_path & name of theAttachment)

                                                      on error number errnum

                                                                display dialog "ERROR: problem saving file, ernum: " & errnum

      error number -128 --User Ended error; this should stop script execution

                                                      end try

      end timeout

      end repeat

      end tell

      end repeat

    end tell

     

    How do I save the attachment "as a user"?

    OS X Mountain Lion (10.8.1)
  • JerryDale Level 1 Level 1 (0 points)

    Hi, Yes i am on Mountain Lion and have been working on setting this up.  The script works on my computer but it saves all the attachments as the same name so it overright the last image.  How do I tell it to rename the image with what is in the subject line and add the date and time stamp?  Here is the script again:

     

    using terms from application "Mail"

        on perform mail action with messages matching_messages

            --set report_type to ":throughput"

            set folder_path to "Macintosh HD:Users:jkinder:Downloads:Mail Attach" -- & report_type

            set file_path to (folder_path & ":")

            tell application "Mail"

                set the num_messages to the count of matching_messages

                -- display dialog "0. Found " & num_messages & " messages!"

                repeat with i from 1 to the num_messages

                    set this_message to (item i of matching_messages)

                    tell this_message

                        -- display dialog "1. Processing \"" & (subject of this_message) & "\" which has " & (count of mail attachments of this_message) & " attachment(s)."

                        repeat with z from 1 to the (count of mail attachments of this_message)

                            set theAttachment to item z of (mail attachments of this_message)

                            -- display dialog "2. Got attachment " & (name of theAttachment)

                            with timeout of 10 seconds

                                set myname to do shell script "whoami"

                                -- display dialog "3.Trying to save " & (name of theAttachment) & " to the path " & (file_path & (name of theAttachment) as string) & " as user " & myname

                                try

                                    save theAttachment in (file_path & name of theAttachment)

                                on error number errNum

                                    display dialog "ERROR: problem saving file, ernum: " & errNum

                                    error number -128 --User Ended error; this should stop script execution

                                end try

                            end timeout

                        end repeat

                    end tell

                end repeat

            end tell

        end perform mail action with messages

    end using terms from

     

    thanks

  • Niko Nyman Level 2 Level 2 (270 points)

    I  think you're hitting the same wall as people are describing in this other thread, where I also posted the result of my debugging: https://discussions.apple.com/message/19501549#19501549

     

    In short: there seems to be a bug in 10.8 Mail.app. The Applescript rules get incoming POP messages in an invisible "incoming messages" mailbox, but when Applescript tries to iterate over the mailbox messages, the mailbox is empty. And that's because the mail is already in your inbox, not in the "incoming mesages" mailbox.

     

    Feel free to post a bug report to Apple: http://www.apple.com/feedback/macosx.html

  • pfly Level 1 Level 1 (0 points)

    Not sure if this is significant, but I was actually running my script off of an Exchange server. 

     

    I've since moved on and am using some Exchange server side rules combined with an Automator script to do what I want.  It's not quite as elegant, but it takes care of my needs.

     

    Thanks for the other thread link.

  • adamcf Calculating status...

    I could really use some help here.  I have no idea how to use applescript but I could really use this to download attachments.  I've tried everything I can find to build an applescript to download messages but nothing works.  I have Mountain Lion and I can't get any script at all to do anything.  All I want to do is download attachments to a specific folder. 

     

    Does anyone have a complete applescript that does that?

     

    Thanks

  • Niko Nyman Level 2 Level 2 (270 points)

    Short answer: it cannot be done in Mountain Lion, because the Applescript support in Mail is broken.

  • Hound_Dog Level 1 Level 1 (5 points)

    It can be done with Mountain Lion. It is just more restrictive and not 100% reliable. I'll post what I have later when I have more time, hopefully later today.

  • jbublik Level 1 Level 1 (0 points)

    The script works for me. Just needs to be saved to correct spot in library and you can save your files ONLY to Downloads folder (whatever subfolder you want to create must be in Downloads) it will not save it into documents.

     

    just make sure you create new folder in Downloads and replace insertYourFolder name

     

    the script must be saved in Library > Application Scripts > com.apple.mail.  Then it works flawlesly

     

    on perform_mail_action(ruleData)

     

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

              set attachmentsFolder to ((path to home folder as text) & "Downloads:InsertYourFolder") as text

     

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

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

              set subFolder to "Production Sheets" as text

              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

     

    -- 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's date 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's mail attachments

                                            set originalName to name of theAttachment

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

     

                                  end repeat

                        end repeat

              end tell

     

    end perform_mail_action

     

     

    this works for me just fine. No issues whatsoever. Hope that helps

  • jbublik Level 1 Level 1 (0 points)

    This one works for me as well - probably easier

     

    using terms from application "Mail"

              on perform mail action with messages theselectedMessages

                        set attachmentsFolder to "Macintosh HD:Users:YourUserName:Downloads:YourNew Folder"

                        tell application "Mail"

                                  repeat with theMessage in theselectedMessages

      -- get the sender's name

                                            set senderName to extract name from theMessage's sender

                                            repeat with theAttachment in theMessage's mail attachments

                                                      set originalName to name of theAttachment

      -- name the file with the sender's name as well as the attachment name

                                                      set savePath to attachmentsFolder & ":" & senderName & "-" & originalName

                                                      try

      -- Save the attachment

      -- ** the code seems to get this far and then doesn't actually save the

      -- ** file in the location indicated

      save theAttachment in savePath

                                                      end try

                                            end repeat

                                  end repeat

                        end tell

              end perform mail action with messages

    end using terms from

     

    It saves to one folder I specify in Downloads - that's functionality I want, it does not create subfolders

    Hope that helps.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (4)

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.