2012-06-21 181 views
1

我一直在使用腳本橋使郵件發送附件爲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 

從AppleScript的好像我需要使用的別名附件路徑(在我的情況下是一個臨時文件),而不是現在使用的郵件無法訪問的路徑。 有沒有簡單的方法來使用腳本brige添加此步驟?

回答

3

找到了解決方案:爲了在Mountain Lion中使用沙盒,您必須提供附件的NSURL而不是NSString的文件路徑。

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

(這也適用於獅子,但在雪豹,你必須使用作爲NSString的文件路徑)