使用脚本桥创建邮件

Blu*_*hou 0 email cocoa scripting-bridge

我正在尝试使用cocoa脚本桥创建一个新的电子邮件,但似乎无法让它工作.我到目前为止:

MailApplication *mail = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"];
MailOutgoingMessage *emailMessage = [[[[mail classForScriptingClass:@"outgoing message"]     alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys: [saveFileName substringToIndex:[saveFileName length]-4], @"subject", @"something about app.", @"content", nil]] autorelease];
// add the object to the mail app 
[[mail outgoingMessages] addObject: emailMessage];
emailMessage.visible = YES;

if ( [FileFullName length] > 0 ) {

    MailAttachment *theAttachment = 
    [[[mail classForScriptingClass:@"attachment"] alloc]
     initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:
                         FileFullName, @"fileName",nil]];
    // add it to the list of attachments 
    [[emailMessage.content attachments] addObject: theAttachment];
    [theAttachment release];
}
Run Code Online (Sandbox Code Playgroud)

当我尝试设置visible属性时,我收到了错误消息:

-[SBProxyByClass setVisible:]: object has not been added to a container yet; selector not recognized [self = 0x7fd4fae3cc90]
Run Code Online (Sandbox Code Playgroud)

当我为app添加沙箱时出现此问题.谁能指出正确的方向?

6 1*_*6 1 6

如果您的代码在启用沙箱之前可以正常工作,那么您应该在发送邮件之前先执行此操作.将以下代码添加到Entitlements.plist.

    <key>com.apple.security.temporary-exception.apple-events</key>
    <array>
    <string>com.apple.mail</string>
    </array>
Run Code Online (Sandbox Code Playgroud)