Using Apple Mail with attachment in sandbox?

I can’t figure out how to open Apple Mail from a sandboxed app.

This AppleScript is working for me, as long as I am not in a Sandbox:

on run {theSubject, theBody, theAttachment} set theFile to POSIX file theAttachment tell application "Mail" set newMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theBody & return & return} tell content of newMessage make new attachment with properties {file name:theFile} at after the last paragraph end tell activate end tell end run

In sandbox I’m getting this error from OSX:

[quote]17.12.13 07:45:04.614 appleeventsd[78]: Sandboxed application with pid 1491 attempted to lookup App:“Mail”/921/0x0:0x38038 emal1110 sess=100005 but was denied due to sandboxing. (___ZL13handleMessageP17_xpc_connection_sPv_block_invoke260()/appleEventsD.cp #2413) client-reqs-q
[/quote]

What other ways would there be to make this work? Are there any MBS plugin functions to launch Apple Mail? Or sandbox save declares? Or do I have to make the script an app and codesign it?

Anybody done that before? Please share your insight. Thanks!
Oliver

Maybe you could try to use “mailto” via show url. The syntax would be:

mailto:someone@somewhere.com?subject=my report&body=see attachment&attachment=“my_file.doc”

@Max: The mailto never really worked for me with a longer body. Let alone with an attachment.

@Oliver: Have you tried to reduce your AppleScript to the basics? Something like “tell Mail to activate”. I’m not an expert in Sandbox topics, but have you done the temporary entitlements? Of course, MBS has functions to launch AppleMail. There are also functions for handling AppleScripts. But the basic functionality should be the same.

If you have the MBS or the MacOSLib, use the openFile:withApplication: function from NSWorkSpace, it works a treat.

Max: I did try this in the past, but it does not reliably work with attachments.
Beatrix: Yes, I have tried with a simple activate: same error (access denied)
Sam: Thanks for the hint, I have MBS but I did not find anything this morning. Now you gave me the hint of NSWorkSpace and I guess I will find some kind of example in Christian’s work.

Thanks everybody!

I’m using MBS now, and this code launches Apple Mail and attaches the folderitem fAttachment. It works:

Call NSWorkspaceMBS.openFile(fAttachment,"Mail",true) 

However: does anybody know how I can add subject, body and receiver email?

[quote=53348:@Oliver Osswald]I’m using MBS now, and this code launches Apple Mail and attaches the folderitem fAttachment. It works:

Call NSWorkspaceMBS.openFile(fAttachment,"Mail",true) 

However: does anybody know how I can add subject, body and receiver email?[/quote]
Ah, you got it… As for setting the other properties, I’m guessing that this would still need to be done via Apple Script.

If you have App Wrapper, there is a section in Sandboxing for adding a Temporary Entitlement to script Apple Mail. The trouble with Temporary Entitlements, is that it’s down to the reviewer if you can use it or not and it’s only a temporary solution.