Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript/ Automator Term "mail attachments" fails with "-10000" general error - OS X 10.10.1

All, There appears to be a very reproducible error "-10000" "Apple Event Handler" regarding the access / use of Apple Mail.app email attachments by the OS X applescript terms "mail attachments" in OS X V10.10.1 yosemite. (2014/2015) .


I'd like to entertain any expertise on the matter. Please realise that the syntax (usage) is correct because my applications using the "mail attachments" term / facility have been in production for 7-8 years and are now inoperable in OS X 10.10.1


Issue: Any Apple Script or Automator object that references "mail attachments" fails with OS return code "-10000" with error info as "Apple event Handler".


For the sake of brevity and to focus on the precise issue, these EXAMPLE script snippets have ready access to the Active Mail.app's the current set of email(s) via OSASCRIPT, Applescript, Automator, Selection or other in var called "processing_email_messages" as :

tell application "Mail" to set processing_email_messages to

... any email or list

Example #1

tell application "Mail" to get count of mail attachments of first item in processing_email_messages

Example #2

if current_email's mail attachments is not {} then

Example #3

tell application "Mail" to get count of the mail attachments in (first item in processing_email_messages)

This error has been reproduced on six independent / different 10.10.1 OS X systems and different users spaces (logons ~/)with the same error symptoms and therefore conclude that this issue is within the current OS X 10.10.1.


Symptom of any of these in any construct:

  1. AppleScripts error "
    Mail got an error: AppleEvent handler failed." number -10000
  2. Automator with nodes Get Selected Mail Messages ->> Get Attachments from Maill Messages

Get Attachments from Mail Messages Failed - Error 1

An error occurred while executing a script: The Operation couldn't be completed. (OSStatus error -10000)

/var/syslog/system.log: these are very consistent.

Mail[49641]: *** Assertion failure in -[MCMimeBody dataForMimePart:], /SourceCache/Mail/Mail-1993/MailCore/MIME/MCMimeBody.m:362

Mail[49641]: An exception was thrown during execution of an NSScriptCommand...

Mail[49641]: This method must be called off the main thread


It appears from my copies web searches that this is not unique to myself as many others have the precise issue that is UNRELATED to syntactic use of Applescript in OSx Mail.app.

I'd appreciate any help or comments on the matter or a workaround would be also very helpful.

Many Thanks

Warwick

Hong Kong

Posted on Jan 17, 2015 8:32 PM

Reply
17 replies

Jan 19, 2015 5:23 PM in response to Warwick Teale

I'm reasonably certain what you're seeing is a caching problem. I had the same issue as you when I began experimenting, but then I ran this script (the important part is the with timeout block):


tell application "Mail"

with timeout of 3600 seconds

repeat with thisMessage in messages in inbox


log (count of thisMessage'smail attachments)

end repeat

end timeout

end tell


When I ran this, the script and Mail.app went through a long silent period where Mail.app was contacting a large number of remote servers, and then the script began logging all of the message attachments correctly. Quitting and restarting Mail.app had no effect, which indicates it had spent that long period downloading and caching attachments.


It did produce the error once more in a later test, because (as best I can tell) Mail.app went through a mail-check cycle while the script was running and must have retrieved a message with attachments that weren't cached. Or maybe there was another cause, but the point is it was an error specific to a particular message, not a general error.

Jan 19, 2015 10:13 PM in response to twtwtw

Hi twtwttw, the "caching" aspect you point out is quite interesting because we had looked at this as well and couldn't conclude anything.

In our testing the attachments of the targeted emails (e.g. mail id 68612) of the targeted mailbox (INBOX) and mail account (warwicks_test_account) were certainly downloaded and resident in the mail attachments in this example file path

~/Library/Mail/V2/IMAP-warwicks_test_account@mail.warwicks-exampleserver.com/INBOX.mbox/A3A9877F-2EDB-48C4-A8BA-1DF69DA39108/Data/8/6/Attachments/68612/


As an example had previously been monitoring these two directory paths that MIGHT contain mail caches and had deleted them (rm) to establish some cache relationship to this "-10000" error.


these paths were my user's:

~/Library/Mail/V2

and OS X wide :

/private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs/

the latter provisioned the general apple mail cache at:

/private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs//C/com.apple.scriptmanage r2.le.cache

/private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs/C/com.apple.MailServiceAg ent/mds/mdsDirectory.db


Amongst others I monitored specifically we're the plausible "caches" in the indexes at:

~/Library/Mail/V2/MailData/BackupTOC.plist

~/Library/Mail/V2/MailData/Envelope Index

~/Library/Mail/V2/MailData/Envelope Index-wal


My procedure to see what was changing was a simple find by what was modified in the last 10 seconds of an email receipt with attachments as follows:

  1. send an email with 1 or more attachments of any kind whose total sizes did not exceed the quota limits of the email server
  2. on receipt of the email in step 1
  3. run the following unix find commands at T+0secs , T+5 secsand T+10secs using this syntax in 4.:
  4. find ~/Library/Mail/V2 -type f -atime -10s

    find /private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs/ -type f -atime -10s


Jan 19, 2015 10:34 PM in response to Warwick Teale

(reposted due to erroneously saving the post prior to completion)

Hi twtwttw, the "caching" aspect you point out is quite interesting because we had looked at this as well and couldn't conclude anything.

In our testing the attachments of the targeted emails (e.g. mail id 68612) of the targeted mailbox (INBOX) and mail account (warwicks_test_account) were certainly downloaded and resident in the mail attachments in this example file path

~/Library/Mail/V2/IMAP-warwicks_test_account@mail.warwicks-exampleserver.com/INBOX.mbox/A3A9877F-2EDB-48C4-A8BA-1DF69DA39108/Data/8/6/Attachments/68612/


As an example had previously been monitoring these two directory paths that MIGHT contain mail caches and had deleted them (rm) to establish some cache relationship to this "-10000" error.


these paths were my user's:

~/Library/Mail/V2

and OS X wide :

/private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs/

the latter provisioned the general apple mail cache at:

/private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs//C/com.apple.scriptmanage r2.le.cache


Amongst others I monitored specifically we're the plausible "caches" in the indexes at:

~/Library/Mail/V2/MailData/BackupTOC.plist

~/Library/Mail/V2/MailData/Envelope Index

~/Library/Mail/V2/MailData/Envelope Index-wal


My procedure to see what was changing was a simple find by what was modified in the last 10 seconds of an email receipt with attachments as follows:

  1. send an email with 1 or more attachments of any kind whose total sizes did not exceed the quota limits of the email server
  2. on receipt of the email in step 1
  3. then run the following unix find commands at T+0secs , T+5 secsand T+10secs using this syntax in 4. reveals any changed files or directories in the last 10 seconds when an email has been received the output reveals paths to te attachments etc that are all valid and also accessible directly by the OS X finder.
  4. find ~/Library/Mail/V2 -type f -atime -10sfind /private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs/ -type f -atime -10s
  5. this is typical of the find ~/Library/Mail/V2 -type f -atime -10s command...

/Users/warwick/Library/Mail/V2/AosIMAP-warwick_teale/INBOX.mbox/A3Ayyy77F-2EDB-4 xx4-zzBA-1DFxxxxx9108/Data/8/6/Messages/68611.emlx

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exa mpleserver.com/Drafts.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Messages/68608.17.em lxpar

/Users/warwick/Library/Mail/V2/AosIMAP-warwick_teale/INBOX.mbox/A3Ayyy77F-2EDB-4 xx4-zzBA-1DFxxxxx9108/Data/8/6/Messages/68611.emlx

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exa mpleserver.com/Drafts.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Messages/68608.17.em lxpart

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exa mpleserver.com/Drafts.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Messages/68608.19.em lxpart

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exa mpleserver.com/Drafts.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Messages/68608.parti al.emlx

Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exam pleserver.com/INBOX.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Attachments/68612/10/ screenshot 2011-03-06 um 18.47.16.png

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exa mpleserver.com/INBOX.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Attachments/68612/11/ screenshot 2011-03-06 um 18.47.49.png

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-exa mpleserver.com/INBOX.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Attachments/68612/12/ screenshot 2011-03-06 um 18.48.04.png

/Users/warwick/Library/Mail/V2/IMAP-IMAP-warwicks_test_account@mail.warwicks-

exampleserver.com/INBOX.mbox/A3Ayyy77F-2EDB-4xx4-zzBA-1DFxxxxx9108/Data/8/6/Atta chments/68612/13/screenshot 2011-03-06 um 18.48.53.png

AT present I've been unable to find a specific "cache" that contains the instances of the emails and attachments that we're after. I did rm /private/var/folders/_5/hpwjtsb17t1bz461glmr_tnr0000gs//C/com.apple.scriptmanage r2.le.cache and restart mail and the OS X however the error -10000 with "mail attachments" prevailed.


I'm of the belief this is a genuine flaw in processing the Appleacript term "mail attachments" for mail.app in OS X 10.10.1. It has been logged as an bug report in Nov 2014 and is referenced in the macscripter.net post at http://macscripter.net/viewtopic.php?pid=178192#p178192


Thanks again for your comments.

Warwick

Hong Kong

Jan 20, 2015 12:01 AM in response to Warwick Teale

Warwick Teale wrote:


AT present I've been unable to find a specific "cache" that contains the instances of the emails and attachments that we're after. [...]


I'm of the belief this is a genuine flaw in processing the Appleacript term "mail attachments" for mail.app in OS X 10.10.1.


Well, Mail.app is an odd bird in some ways, and its applescript support has always been sketchy. Keep in mind three things:


  1. Unlike a conventional email client, Mail.app stores its messages in individual files (they did this to support spotlight searching), and individual parts of each message are stored (as you discovered) in an arcane system of private, obscurely labeled folders. The whole thing is held together by a SQL database (that's the Envelope Index file), and synchronized with remote servers lazily, so that certain attachments may not download at all until they are requested for viewing. It's a Rube Goldberg mess that can cause unexpected bottlenecks.
  2. Mail.app is heavily threaded, and most of what it does it does asynchronously (not surprising given that it consistently has to wait for responses from servers), but applescript is not threaded, and doesn't always deal with threading issues well.
  3. Mail attachments (and mail messages) are applescript objects, not simple data, and applescript will often pass objects by reference rather than directly (i.e. it passes a pointer to the data rather than the data itself). The difficulty with that (I suspect) is that because Mail works asynchronously it can pass references to objects that are there in name only (e.g. pass a reference to where an attachment would be if/when mail retrieves it from the server). You can solve those problems sometimes by using parentheses and the get keyword (e.g. say if current_email's (get mail attachments) is not {} then ...), but if it's passing a reference to something that's not yet there, that may not help.


If this is a significant problem, it would help to know what your overarching goal is; it may be better to think about workarounds than to try to puzzle out precisely what's going wrong.

Jan 20, 2015 3:01 AM in response to Warwick Teale

Hello


I remember this error message:


This method must be called off the main thread



and found this 4-year old thread:


AS called from mail .app rule - *** Assertion failure in -[MimeBody dat

https://discussions.apple.com/thread/2708103



which was about the said error raised when processing script as a mail rule and a remedy was to put code in a user-defined handler and call it instead of putting code directly in "on perform mail action with messages" handler.



Now the error returned in different context? And the old remedy no longer works?


H

Jan 20, 2015 9:55 PM in response to twtwtw

Hi twtwtw, many thanks for that great information and insight (experience). It certainly addresses some of the reasons for the misbehaviour of Mail and applescripts. there's good information here.

Specifically:

You can solve those problems sometimes by using parentheses and the get keyword (e.g. say if current_email's (get mail attachments) is not {} then ...), but if it's passing a reference to something that's not yet there, that may not help.


At your suggestion, I went ahead and tried permutations of this as follows however to no avail as the usual error "Mail got an error: AppleEvent handler failed." number -10000 occurred: and the term "mail attachments" was the focus of error and indeed these /var/log/messages occurred as:

Jan 21 13:45:13 macpro Mail[23783]: *** Assertion failure in -[MCMimeBody dataForMimePart:], /SourceCache/Mail/Mail-1900/MailCore/MIME/MCMimeBody.m:3nn

Jan 21 13:45:13 macpro Mail[23783]: An exception was thrown during execution of an NSScriptCommand...

Jan 21 13:45:13 macpro Mail[23783]: This method must be called off the main thread

script snippets example's that both fail:

tell application "Mail"

get (mail attachments) of (the first message of mailbox "INBOX" of account "warwicks Test Imap Email")

get (thefirstmessageofmailbox "INBOX" ofaccount "warwicks Test Imap Email")'s (mail attachments)

end tell

If this is a significant problem, it would help to know what your overarching goal is; it may be better to think about workarounds than to try to puzzle out precisely what's going wrong.

Yes. The issue manifested itself in a series of Mail.app rules (invoke apple script) for examining email attachments on OS X Mail.app when our production Mac were migrated from 10.9.5 directly to OS X 10.10.1 in late December 2014.


This fails in OSx 10.10.1 either in line or as a separate handler.


I would certainly welcome a workaround because one of our automated workflows rely of an email attachment event. We are handling them manually at present. Any suggestion would be considered.


Thanks again for your insightful reply to my problem.


warwick

Hong Kong

Jan 20, 2015 10:00 PM in response to Hiroto

Hi Horito, yes you are right. I too recall having to relocate some of the 10.6 mail.app applescript code to a separate event handler in a mail rule such as the "on perform mail action with messages" handler on OS X 10.7. They are still in play and worked well to OS X 10.9.5. They fail in OSx 10.10.1.


Sadly as I think you have pointed out the term "mail attachment" still fails in 10.10.1 in ether situation.


Thanks for another good idea to look at.


B/R


warwick

Hong Kong

Jan 21, 2015 12:17 AM in response to Warwick Teale

I'm sorry, you need to clarify more. Is this 'automated workflow' you mention a Mail.app rule action, an Automator Workflow or service, a folder action, a separate script you run on Mail.app from the script editor, or something else? I need to know because Automator, Rule Actions, and stand-alone scripts have different dynamics (and if you're using an applescript from within Automator, or an automator Folder Action, that's also different). for instance, I know I can get Automator to extract attachments in 10.10.1 into a folder or a workflow variable, but since I don't know what you're doing with them, I have no idea if that helps.


I also discovered that attachments are accessible if you go through the message viewer rather than through the data hierarchy. In other words, things like the following seem to work well (probably because Mail.app retrieves the pieces of messages internally before it loads them into a message viewer):


tell application "Mail"

set b to selected messages of message viewer 1


mail attachments of item 1 of b

end tell


but - again - since you haven't really specified your workflow, I don't know if that helps.


I think you've gone past the stage of making your script work like it used to work, and are looking at the process of revising your scripts so they work in a different way. I don't know if that's what you want to do, but if it is, and you would like assistance, you need to give a somewhat larger perspective.

Jan 22, 2015 6:23 AM in response to twtwtw

Hi twtwtw! may thanks again for your response. To be quiet clear for anyone reading this, the PRIME workflow where is fails is an APPLESCRIPT that implemented as s Apple OSx Mail.app RULE from the OSx 10.10.1 OS.

The code snippets ALWAYS fail with the -10000 Apple Event Handler error when the term 'mail attachment" is used to reference one of more mail attachments.

This is REGARDLESS of whether the code snipped (statements) are in a simple SELECTION from a window, a MAIL RULE as an apple script etc etc. (This include eh OSSAttus error manifested is GET nmail attachments events is used in Automator).

So.. is an applescript in an OS X 10.10.Mail.app RULE.

Thanks for the statement I deal. Ofcourse I gleefully tried it as follows and sadly it fails on the second and subsequent attempts attachmmt with 2 or more attachments. Now it fails with a single attachment. (there's that cache idea again!)

The statement are thus (getting the most recent email)


tell application "Mail"

set b to the first message of mailbox "INBOX" of account "warwick map test account"

mail attachments of item 1 of b

endtell

fails as follows run from Script editor with Mail.app active:

tell application "Mail"

get message 1 of mailbox "INBOX" of account "warwick map test account"

--> message id 68947 of mailbox "INBOX" of account "warwick map test account"

get every mail attachment of item 1 of message id 68947 of mailbox "INBOX" of account "warwick map test account"

--> error number -10000

Result:

error "Mail got an error: AppleEvent handler failed." number -10000

OT: Automator. Strange, despite the simple correctness of a simple Automator flow, I can get the mail attachments into a variable because of this error in OSx 10.10.1.

Yes, it's a most reasonable request you have to rethink the statements in the applescript. I'm certainly keen to try ant reasonable new approach.

Thanks for your idea.. it was certainly worth a try!

Warwick

Hong Kong

Jan 22, 2015 10:18 AM in response to Warwick Teale

Let me say first that I'm surprised this worked before 10.10.1. Mail Rules are weird hybrid: emails download and are processed asynchronously, and Mail.app send references to individual emails to Mail Rule AppleScripts as the emails arrive, not after they are finished being processed. Mail rules are meant for things like labelling and sorting, not for in-depth processing of email contents.


That being said, there are a couple of different things I can suggest. The first is more of a hack than a solution, but it may serve your purposes. It looks like this:


tell application "Mail"

set b to the first message of mailbox "INBOX" of account "warwick map test account"

set ma to false

set loopCount to 0

repeat while loopCount < 100

try

set ma to mail attachments of item 1 of b

exit repeat

on error number eNumb

if eNumb = 10000 then

set loopCount to loopCount + 1

delay 1

end if

end try

end repeat


-- ma wile either be a list of attachments, or will be false if the loop never resolved or if some error other than 10000 occurs

end tell


I dislike using delay loops of that sort, but...


If it were me, however, I'd avoid using Mail Rules in this way. I might, for instance, use a mail rule to mark incoming messages that need to be processed with a label color, but then use a stand-alone script or script app to do the actual work on the messages (changing the label color afterwords to mark the message as processed). That script could be a stay-open script app that periodically polls Mail.app for color-coded messages, or a script that fires off from a separate rule action placed at the very end of rule actions (or be launched from launchd, or by hand, or etc.); the point is to break away from the immediacy of the passed-in message object of an applescript rule action so that Mail.app has some time to process the incoming messages.

Jan 22, 2015 5:52 PM in response to twtwtw

Hi twtwtw, thanks this is an example of an idea to work around the issue at hand. I will also try the construct same above.

twtwtw wrote:


I dislike using delay loops of that sort, but...


If it were me, however, I'd avoid using Mail Rules in this way. I might, for instance, use a mail rule to mark incoming messages that need to be processed with a label color, but then use a stand-alone script or script app to do the actual work on the messages (changing the label color afterwords to mark the message as processed). That script could be a stay-open script app that periodically polls Mail.app for color-coded messages, or a script that fires off from a separate rule action placed at the very end of rule actions (or be launched from launchd, or by hand, or etc.); the point is to break away from the immediacy of the passed-in message object of an applescript rule action so that Mail.app has some time to process the incoming messages.

Thanks again I will try this meanwhile staying is the bound of BLACK text at 10-12 point for affected forum readers. 😉


Warwick

Hong Kong

Jan 22, 2015 6:18 PM in response to Warwick Teale

Hello


Regarding the assertion failure stating the method [MCMimeBody dataForMimePart:] is being called not-off (i.e. on) the main thread, I doubt basically we have control over what thread it is called on. But possibly we could hope new AppleScript instance might spawn new thread in Mail and might experiment with the following scripts.


The former uses simple "run script" command and the latter uses more convoluted osascript(1) approach. You may define main code in new_script() handler. Currently it only counts the mail attachments in the given messages.


Scripts are tested to work with Mail 4.6 under OS X 10.6.8. But no way to tell whether they spawn new thread in Mail. Indeed, in my test environment, I don't get the said assertion failure in the first place. You may just try them to see if they do.


Good luck,

H



--SCRIPT 1 on run tell application "Mail" my _main(get selection) end tell end run using terms from application "Mail" on perform mail action with messages argv my _main(argv) end perform mail action with messages end using terms from on _main(argv) (* list argv : list of Mail message objects *) set scpt to my new_script(argv) set k to run script scpt display dialog "number of attachments = " & k end _main on new_script(argv) (* list argv : list of Mail message objects *) script o property |ARGV| : argv property |RESULT| : {} set k to 0 tell application "Mail" repeat with a in my |ARGV| set k to k + (count a's mail attachments) end repeat end tell set my |RESULT| to k end script end new_script --END OF SCRIPT 1





--SCRIPT 2 on run tell application "Mail" my _main(get selection) end tell end run using terms from application "Mail" on perform mail action with messages argv my _main(argv) end perform mail action with messages end using terms from on _main(argv) (* list argv : list of Mail message objects *) set scpt to my new_script(argv) set k to osascript(scpt) display dialog "number of attachments = " & k end _main on osascript(scpt) set f to do shell script "echo /tmp/$(uuidgen).scpt" store script scpt in POSIX file f do shell script "osascript " & f's quoted form set r to (load script POSIX file f)'s |RESULT| do shell script "rm " & f's quoted form return r end osascript on new_script(argv) (* list argv : list of Mail message objects *) script o property |ARGV| : argv property |RESULT| : {} set k to 0 tell application "Mail" repeat with a in my |ARGV| set k to k + (count a's mail attachments) end repeat end tell set my |RESULT| to k end script end new_script --END OF SCRIPT 2

AppleScript/ Automator Term "mail attachments" fails with "-10000" general error - OS X 10.10.1

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.