1

我一直在使用脚本桥使 Mail 在 10.6 和 10.7 中发送带有附件的消息,但从 10.8 开始,Mail 应用程序本身是沙盒的,因此它无法访问附件文件。

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

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

4

1 回答 1

3

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

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

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

于 2012-06-26T05:02:34.910 回答