2

I am using NSAppleScript for sending email from my OSX application. It was working fine before but now when I use new XCode 9.0 with Sierra 10.12.6 I have one problem. Application will create email and open Mail window containing email and message content but it wont perform SEND. I can click on send button and everything is fine, mail will send but I would like to avoid manual click-on-send-button action.

I am using entitlements:

<plist version="1.0">
<dict>
    <key>com.apple.security.scripting-targets</key>
    <dict>
        <key>com.apple.mail</key>
        <array>
            <string>com.apple.mail.compose</string>
        </array>
    </dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
</dict>
</plist>

And this is the code:

    NSString *emailString = [NSString stringWithFormat:@"\
                                 tell application \"Mail\"\n\
                                 set newMessage to make new outgoing message with properties {subject:\"%@\", content:\"%@\" & return} \n\
                                 tell newMessage\n\
                                 set visible to false\n\
                                 set sender to \"%@\"\n\
                                 make new to recipient at end of to recipients with properties {name:\"%@\", address:\"%@\"}\n\
                                 tell content\n\
                                 ",subjectt, bodyText, @"Company", toAddress, toAddress];
emailString = [emailString stringByAppendingFormat:@"\
                   end tell\n\
                   set visible to false\n\
                   send newMessage\n\
                   end tell\n\
                   end tell"];


    NSLog(@"%@",emailString);
    NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString];
    [emailScript executeAndReturnError:nil];

So everything is in place but send action is failing. Again it was working well with older OSX version(s).. Am I missing entitlement or something?

Thank you!

rrodic
  • 21
  • 2

2 Answers2

0

It looks like you tell the newMessage to send the newMessage (double reference)

Delete newMessage in the send line

vadian
  • 274,689
  • 30
  • 353
  • 361
0

Long story short. This is the wrong way. With modern OSX use NSSharingService.

NSArray * shareItems = [NSArray arrayWithObjects:bodyText, nil]; NSArray * recepiants = [NSArray arrayWithObjects:toAddress, nil];

NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
[service setRecipients:recepiants];
[service setSubject:subjectt];

service.delegate = self;
[service performWithItems:shareItems];

When it comes down to sending email with some sort of results from application NSAppleScript is not your friend anymore. OSX is becoming more like iOS nowadays.

rrodic
  • 21
  • 2