Can not delete the mail attachment (Apple Event handler failed). Why?

Hello!

I’m trying to delete a mail attachment from mail message. Here is my attempt:

tell application "Mail"
	tell mailbox "Junk" of account id "XXX"
		set aMsg to item 38 of messages --message id 172 of mailbox "Junk" of account id "XXX"
		set aFile to mail attachment of aMsg --{mail attachment id "2" of message id 172 of mailbox "Junk" of account id "XXX"}
		delete first item of aFile --Error: the Apple Event handler failed (errAEEventFailed:-10000)
	end tell
end tell

This gives "Error: the Apple Event handler failed (errAEEventFailed:-10000)" on ‘delete’ line. Why this happens? Am I doing something wrong?

There is smth similar with Notes, as far as I can see. Is this the same issue?

Thank you for help!

As far as I know, a mail attachment doesn’t respond to the delete command. And thinking about what it would mean to delete a mail attachment, it doesn’t make sense to me in practical terms, unless it is an attachment to an unsent, outgoing draft.

What are you wanting to see happen if you had code that deleted mail attachments, and how would this normally be done within the application if you were to do it manually for a single message?

Hm, @Valery_Kondakoff, there’s a Remove Attachment menu item I’ve not noticed before. Does this actually allow a message in, say, the Inbox to be “replaced” with an edited version that has had its attachment(s) removed ? I’m not in a position to test this myself yet, as I haven’t set up my Mac fully since I broke it.

Yes, looks like you are right: direct deletion of mail attachments is not supported. I just tested this by pointing the mail attachment in SD Explorer and choosing the right click pop up menu item "Delete mail attachment ". This causes and "Error deleting mail attachment "name": -10000".

But there is a way to do this by clicking the ‘Remove Attachments’ menu item with System Events (this was a suggestion from Reddit AppleScript subreddit):

tell application "Mail"
	activate
	set selected messages of message viewer 1 to message 1 of mailbox "XX" of account id "XX"
end tell
tell application "System Events" to click menu item "Remove Attachments" of menu 1 of menu bar item "Message" of menu bar 1 of process "Mail"

And thinking about what it would mean to delete a mail attachment, it doesn’t make sense to me in practical terms, unless it is an attachment to an unsent, outgoing draft.

Deleting big mail attachments is a quick way to free some space (on local disk and on an IMAP server: the attachments are deleted from the server as well).

Does this actually allow a message in, say, the Inbox to be “replaced” with an edited version that has had its attachment(s) removed ?

Remove Attachment menu item just replaces the existing mail attachment with text file “Mail attachment.txt” with a following contents: "<BR><BR>[The attachment <attachment name> has been manually removed]<BR><BR>". So, it is the same message with all the attachments replaced by small text-file placeholders.

1 Like