【问题标题】:Sending mail attachment with scripting bridge through sandbox通过沙箱使用脚本桥发送邮件附件
【发布时间】:2012-06-21 14:14:43
【问题描述】:

我一直在使用脚本桥使 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看来,我需要使用附件路径的别名(在我的例子中是一个临时文件),而不是现在使用的路径,邮件无法访问。 有没有一种简单的方法可以使用脚本桥添加此步骤?

【问题讨论】:

    标签: macos sandbox email-attachments


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多