Beemerang

Q: Save Attachment script gives error "Mail got an error: AppleEvent handler failed." number -10000

Hi All,

 

I am completely ignorant when it comes to Apple Script and I am trying to get this simple script to work. The goal of the script is to save the attachments to a designated folder when I run it on selected Apple Mail messages through a rule.

 

This is the code:

 

          tell application "Mail"   

      set theMessages to selection

     repeat with thisMessage in theMessages

            set theAttachments to thisMessage's mail attachments

            repeat with thisAttachment in theAttachments

                set theAttachmentFileName to ((path to home folder rule type rich text) & "Documents:Hazel Processing Folder") & thisAttachment's name        

                 save thisAttachment in file theAttachmentFileName

            end repeat

       end repeat

          end tell   


The folder does exist but I receive a "error "Mail got an error: AppleEvent handler failed." number -10000" when I run it.

 

The code editor in Script Editor also inserts "rule type" in my code so I assume that my syntax is faulty.

 

Any assistance would be greatly appreciated.

 

Regards,

Beem

iMac (21.5-inch, Late 2013), Mac OS X (10.7.5)

Posted on May 9, 2016 12:16 PM

Close

Q: Save Attachment script gives error "Mail got an error: AppleEvent handler failed." number -10000

  • All replies
  • Helpful answers

  • by Pierre L.,

    Pierre L. Pierre L. May 9, 2016 1:33 PM in response to Beemerang
    Level 5 (4,484 points)
    May 9, 2016 1:33 PM in response to Beemerang

    If folder “Hazel Processing Folder” already exists in the Documents folder, the following GUI script, which seems to work flawlessly under OS X 10.11.3, should do what you are asking for:

     

    try

         set theDestinationFolder to ((path to documents folder) as text) & "Hazel Processing Folder" as alias

    on error -- the folder doesn't exist

         return beep 3

    end try

     

    tell application "Mail"

         activate

         set theMessages to selection

         repeat with thisMessage in theMessages

               open thisMessage

               tell application "System Events" to tell process "Mail"

                     click menu item "Save Attachments…" of menu 1 of menu bar item "File" of menu bar 1

                     repeat until sheet 1 of window 1 exists

                     end repeat

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

                     keystroke "Hazel"

                     click button "Save" of sheet 1 of window 1

                     repeat while sheet 1 of window 1 exists

                     end repeat

                     keystroke "w" using {command down} -- close this message

               end tell

         end repeat

    end tell

  • by Pierre L.,

    Pierre L. Pierre L. May 9, 2016 4:00 PM in response to Pierre L.
    Level 5 (4,484 points)
    May 9, 2016 4:00 PM in response to Pierre L.

    It's not even necessary to open and close each message.

    You can delete the following two lines of code:

         open thisMessage

    and

         keystroke "w" using {command down} -- close this message

  • by Pierre L.,Helpful

    Pierre L. Pierre L. May 9, 2016 10:03 PM in response to Beemerang
    Level 5 (4,484 points)
    May 9, 2016 10:03 PM in response to Beemerang

    An even simpler solution.

     

    If folder “Hazel Processing Folder” already exists in the Documents folder, the following GUI script, which seems to work flawlessly under OS X 10.11.3, should do what you are asking for:


    try

         set theDestinationFolder to ((path to documents folder) as text) & "Hazel Processing Folder" as alias

    on error -- the folder doesn't exist

         return beep 3

    end try

     

    tell application "Mail"

         activate

         set theMessages to selection

         tell application "System Events" to tell process "Mail"

               click menu item "Save Attachments…" of menu 1 of menu bar item "File" of menu bar 1

               repeat until sheet 1 of window 1 exists

               end repeat

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

               keystroke "Hazel"

               click button "Save" of sheet 1 of window 1

         end tell

    end tell

     

    Also have a look to this article.

  • by Camelot,Helpful

    Camelot Camelot May 9, 2016 10:06 PM in response to Beemerang
    Level 8 (47,285 points)
    Mac OS X
    May 9, 2016 10:06 PM in response to Beemerang

    Something certainly is amiss - the 'rule type' keyword is a big red flag to me, and I suspect it's some kind of dictionary conflict where Mail.app is (mis)interpreting the 'folder' keyword.

     

    I got around that by working out the home folder path outside of the 'tell application "Mail'' block:

     

    set homeFolder to (path to home folder as text)

    tell application "Mail"

      set theMessages to selection

      repeat with thisMessage in theMessages

      set theAttachments to thisMessage's mail attachments

      repeat with thisAttachment in theAttachments

      set theAttachmentFileName to (homeFolder & "Documents:Hazel Processing Folder:") & thisAttachment's name

      save thisAttachment in file theAttachmentFileName

      end repeat

      end repeat

    end tell


    Note that you also need a trailing ':' after the 'Hazel Processing Folder', otherwise the concatenated string will end up like: ...:Documents:Hazel Processing Folderfilename.ext', which is certainly going to fail.

  • by Beemerang,

    Beemerang Beemerang May 9, 2016 10:06 PM in response to Pierre L.
    Level 1 (4 points)
    Mac OS X
    May 9, 2016 10:06 PM in response to Pierre L.

    Hi Pierre,

     

    Thank you very much for the help!

     

    This has proved really useful but I found that while it works perfectly if I select messages in mail, and then run the script from within Script Editor, it doesn't work if I assign the script to an Apple Mail rule. Since your code executes flawlessly when I use the first option, I must be doing something wrong. Any suggestions for where I could start troubleshooting?

     

    Cheers,

    Beem

  • by Beemerang,

    Beemerang Beemerang May 9, 2016 10:10 PM in response to Camelot
    Level 1 (4 points)
    Mac OS X
    May 9, 2016 10:10 PM in response to Camelot

    Hi Camelot,

     

    Thanks for taking the time to reply, I appreciate it!

     

    When I run this code I still get an "error "Mail got an error: AppleEvent handler failed." number -10000" error. I have double-checked that I am inserting your code correctly but I may still be doing something wrong.

     

    Any suggestions?

     

    Cheers,

    Beem

  • by Pierre L.,

    Pierre L. Pierre L. May 9, 2016 11:56 PM in response to Beemerang
    Level 5 (4,484 points)
    May 9, 2016 11:56 PM in response to Beemerang

    No, you're not doing anything wrong.

    For a reason I don't know, it seems GUI scripts cannot be run via Apple Mail rules. Sorry.

    However, you could easily run the script from the Script menu:

    Screen Shot 2016-05-10 at 2.38.36 AM.png

  • by Beemerang,

    Beemerang Beemerang May 9, 2016 11:58 PM in response to Pierre L.
    Level 1 (4 points)
    Mac OS X
    May 9, 2016 11:58 PM in response to Pierre L.

    Ah I see. Thanks again for taking the time to help, your script provided some much needed help!

     

    Cheers,

    Beem

  • by Camelot,

    Camelot Camelot May 10, 2016 10:55 AM in response to Beemerang
    Level 8 (47,285 points)
    Mac OS X
    May 10, 2016 10:55 AM in response to Beemerang

    The two most likely places this could fail is if the directory path is invalid (i.e. does the 'Hazel Processing Folder' exist, and you have permission to write to it), or if there's already a file in the directory with the same name (there doesn't appear to be any option within Mail.app's dictionary to deal with overwriting files).

     

    Are either of these two possible?

  • by Frederick Northrop,

    Frederick Northrop Frederick Northrop Sep 19, 2016 10:48 AM in response to Beemerang
    Level 1 (23 points)
    Mac App Store
    Sep 19, 2016 10:48 AM in response to Beemerang

    That is because you need to envelope the procedure in a handler.  When you run a script in the editor you are invoking the implicit "on run" handler, but when the script is initiated by a mail rule, the "on run" handler will not be started.

     

    You have to put your code inside a handler like this

     

    using terms from application "Mail"

         on perform mail action with messages theMessages

     

              -- code goes here

     

         end perform mail action

    end using terms from

     

    You do not need the "tell application" wrapper nor the "set the messages to selection" command. When the rule triggers, the message or messages are handed off to the handler.