通过沙箱发送脚本桥的邮件附件

时间:2012-06-21 14:14:43

标签: macos sandbox email-attachments

我一直在使用脚本桥来使邮件在10.6和10.7中发送带附件的邮件,但是表单10.8邮件应用程序本身是沙箱,因此它无法访问附件文件。

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];

我读了一个建议来看看iCal使用AppleScript打开带附件的邮件的方式:

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

从Apple脚本看来,我需要使用附件路径的别名(在我的情况下是临时文件),而不是现在使用的路径,而Mail无法访问。 有没有一种简单的方法可以使用脚本brige添加此步骤?

1 个答案:

答案 0 :(得分:3)

找到解决方案:要使其在Mountain Lion中使用沙盒,您必须提供附件的NSURL而不是文件路径作为NSString。

MailAttachment *theAttachment = [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:[NSURL fileURLWithPath:eachPath], @"fileName",nil]];

(这也适用于Lion,但在Snow Leopard中你必须使用文件路径作为NSString)