I’ve been using scripting bridge to make Mail send messages with attachments in 10.6 and 10.7 but form 10.8 the Mail app itself is sandboxed so it cannot access the attachment file.
MailApplication *mail = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"];
MailOutgoingMessage *mailMessage = [[[mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:subject, @"subject",body, @"content", nil]];
[[mail outgoingMessages] addObject:mailMessage];
[mailMessage setVisible:YES];
for (NSString *eachPath in paths) {
if ( [eachPath length] > 0 ) {
MailAttachment *theAttachment = [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:eachPath, @"fileName",nil]];
[[mailMessage.content attachments] addObject: theAttachment];
}
}
[mail activate];
I read a suggestion to look at the way iCal uses AppleScript to open Mail with an attachment:
on send_mail_sbp(subjectLine, messageText, invitationPath)
set pfile to POSIX file invitationPath
set myfile to pfile as alias
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail
tell content
make new attachment with properties {file name:myfile} at after the last word of the the last paragraph
end tell
end tell
set visible of mymail to true
activate
end tell
end send_mail_sbp
From the applescript it seems like I need to use an alias to the attachment path (a temp file in my case), instead of the path now used which is not reachable by Mail.
Is there a simple way to add this step using scripting brige?
Found the solution: for it to work with sandboxing in Mountain Lion you have to provide the NSURL of the attachment instead of the file path as an NSString.
(this also works in Lion but in Snow Leopard you must use the file path as NSString)