I want to add attachment in outgoing Mail message, at beginning.

G’day folks

This is a last ditch attempt at finding answers to this.

Shane Stanley has helped me so far, and I’m grateful for his help, but he doesn’t know the answer to these questions (if there are any).

He’s shown me how to make an outgoing message using ASObjC, but it appears that adding attachments to the message always adds them at the end, and I need to add them at the start, not at the beginning.

As well, I can’t find out how to actually send the message. All Finder and System Events to an outgoing message are ignored, do how do I send it?

Any advice appreciated, please?



on eMailit(the_subject, the_content, theCCRecipientsStore, textcolor)
tell application "Finder"
		set pathToMe to path to current application as text
		set logoPath to pathToMe & "Contents:Resources:Report Logo.png" as text
		set PosixLogoPath to POSIX path of logoPath
	on error
		set logoPath to ""
	end try
	set p to 2
	if ((count of logoPath) as text) < 4 or not (exists file logoPath) then
			set pathToMe to (path to applications folder) & "Mail Manager:Mail Manager.app:" as text
			set logoPath to pathToMe & "Contents:Resources:Report Logo.png" as text
			set PosixLogoPath to POSIX path of logoPath
		end try
	end if
end tell

set p to 2.1
set anNSURL to current application's |NSURL|'s fileURLWithPath:PosixLogoPath
-- make file wrapper from URL
set p to 2.2
set anNSFileWrapper to current application's NSFileWrapper's alloc()'s initWithURL:anNSURL options:0 |error|:(missing value)
-- make attachment from file wrapper
set p to 2.3
set anNSTextAttachment to current application's NSTextAttachment's alloc()'s initWithFileWrapper:anNSFileWrapper
-- make attributed string from attachment
set p to 2.4
set theAttachment to current application's NSAttributedString's attributedStringWithAttachment:anNSTextAttachment


set anNSString to current application's NSString's stringWithString:the_content
set p to 22
set anNSMutableAttributedString to current application's NSMutableAttributedString's alloc()'s initWithString:anNSString attributes:(missing value)


set mailShare to current application's NSSharingService's sharingServiceNamed:(current application's NSSharingServiceNameComposeEmail)

mailShare's setRecipients:{mailRecipients}
mailShare's setSubject:the_subject

# Adds attachment to end, not beginning
mailShare's performWithItems:{theAttachment, anNSMutableAttributedString}

Model: Late 2014 retina i7, Yosemite
AppleScript: 2.4
Browser: Safari 600.2.5
Operating System: Other

Have you tried to integrate the attachment inside the NSMutableAttributedString, at the beginning? No idea what the result would be though. Would only work if it’s an image I reckon…

Secondly, why use NSSharingService ? Why not just a plain old applesript? Just curious…

And maybe you could try to create an HTML message? I’ve tried to in the past, but couldn’t make it work, even though the command in AS is there.

Browser: Safari 534.51.22
Operating System: Mac OS X (10.8)

G’day Leon.

I, and Shane Stanley, have tried to put the Attachment at the start of the attributed text, but Mail moves it to the end when the attributed text is pasted in. Also, there’s actually no way of sending the Mail message that’s created with attributed text.

I want to use attributed text because the normal method of creating a Mail messages takes over 2 minutes to alter the text (colors of certain items, text size likewise), of one of my apps Annual reports, whereas attributed text does it in 1 second.

I’ve lodged a bug report.



I see. Did you check the pref in Mail (if you are using Mail) ? In the Edit menu > Attachment > Always put attachments to the end of message or something like that… if it’s checked, it may be causing the problem.

G’day Leon

It’s not checked, but seems to default to it anyway, regardless.

Also, the problem of not being able to send the complied message when using NSSharingService is also stopping me. The message won’t even respond to GUI commands.



Yeah, makes sense that NSSharingService would not be ideal, it’s waiting for the user’s interaction.

I’ve had similar problems in the past with creating emails, dunno why, but always been a pain in the ass for something that is so simple and also used a million times a day.

Did you check up the SBSendEmail sample project in the docs? It uses Scripting bridge, and although this is a framework that is difficult to use, this example might give you a head start on something. It’s basically what you could do with applescript but with the added bonus of having access to all other frameworks and classes.

Or even this SDK :


Or this:


Or even these :


This last one, scroll down to the bottom, it may be interesting since you are using NSSharedService.

using NSSharingService,
I have 2 additional questions:

  1. Is there a way to define the sender (I have mail configured with multiple accounts and like to programmatically set a certain account)?
  2. Can the mail be send out automatically, not just created ?

Alas, no to both.

Thank you for the quick answer Shane.
Unfortunately a bad answer.