2017-10-13 45 views
2

我正在使用NSAppleScript從我的OSX應用程序發送電子郵件。 它之前工作正常,但現在當我用新的XCode 9.0與Sierra 10.12.6我有一個問題。應用程序將創建電子郵件並打開包含電子郵件和消息內容的郵件窗口,但不會執行發送。我可以點擊發送按鈕,一切都很好,郵件將發送,但我想避免手動點擊發送按鈕操作。與Sierra一起使用NSAppleScript - 未發生發送操作

我使用的權利:

<plist version="1.0"> 
<dict> 
    <key>com.apple.security.scripting-targets</key> 
    <dict> 
     <key>com.apple.mail</key> 
     <array> 
      <string>com.apple.mail.compose</string> 
     </array> 
    </dict> 
    <key>com.apple.security.app-sandbox</key> 
    <true/> 
    <key>com.apple.security.network.client</key> 
    <true/> 
</dict> 
</plist> 

這是代碼:

NSString *emailString = [NSString stringWithFormat:@"\ 
           tell application \"Mail\"\n\ 
           set newMessage to make new outgoing message with properties {subject:\"%@\", content:\"%@\" & return} \n\ 
           tell newMessage\n\ 
           set visible to false\n\ 
           set sender to \"%@\"\n\ 
           make new to recipient at end of to recipients with properties {name:\"%@\", address:\"%@\"}\n\ 
           tell content\n\ 
           ",subjectt, bodyText, @"Company", toAddress, toAddress]; 
emailString = [emailString stringByAppendingFormat:@"\ 
        end tell\n\ 
        set visible to false\n\ 
        send newMessage\n\ 
        end tell\n\ 
        end tell"]; 


    NSLog(@"%@",emailString); 
    NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString]; 
    [emailScript executeAndReturnError:nil]; 

所以一切都在地方,但送的行動失敗。 它再次與舊的OSX版本(s)運作良好。 我是否缺少權利或什麼?

謝謝!

回答

0

看起來你tellnewMessagesendnewMessage(雙參考)

send

+0

謝謝你的回覆!將代碼更改爲:send \ n \我有相同的結果。沒有不同。 – rrodic

0

長話短說刪除newMessage。這是錯誤的方式。使用現代OSX使用NSSharingService。

NSArray * shareItems = [NSArray arrayWithObjects:bodyText,nil]; NSArray * recepiants = [NSArray arrayWithObjects:toAddress,nil];

NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail]; 
[service setRecipients:recepiants]; 
[service setSubject:subjectt]; 

service.delegate = self; 
[service performWithItems:shareItems]; 

當它歸結爲帶有某種來自應用NSAppleScript結果發送電子郵件是不是你的朋友了。現在OSX變得更像iOS了。