我们需要使用 AppleScript 在 macOS 中创建外发电子邮件。该脚本在脚本编辑器中运行良好。使用 DTS https://forums.developer.apple.com/message/301006#301006推荐的代码没有结果、警告或错误。与论坛中的示例脚本结果相同。在这里需要 Swift 和 Apple Events 专业知识。谢谢!
import Foundation
import Carbon
class EmailDoc: NSObject {
var script: NSAppleScript = {
let script = NSAppleScript(source: """
set theSubject to "Some Subject"
set theContent to "Some content of the email"
set recipientName to "Some Name"
set recipientAddress to "someone@example.com"
tell application "Mail"
# Create an email
set outgoingMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
# Set the recipient
tell outgoingMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
# Send the message
send
end tell
end tell
"""
)!
let success = script.compileAndReturnError(nil)
assert(success)
return script
}()
// Script that is referenced by DTS at https://forums.developer.apple.com/message/301006#301006
// that goes with runScript method below -- runs with no results
/*var script: NSAppleScript = {
let script = NSAppleScript(source: """
on displayMessage(message)
tell application "Finder"
activate
display dialog message buttons {"OK"} default button "OK"
end tell
end displayMessage
"""
)!
let success = script.compileAndReturnError(nil)
assert(success)
return script
}() */
@objc
func runScript() {
let parameters = NSAppleEventDescriptor.list()
parameters.insert(NSAppleEventDescriptor(string: "Hello Cruel World!"), at: 0)
let event = NSAppleEventDescriptor(
eventClass: AEEventClass(kASAppleScriptSuite),
eventID: AEEventID(kASSubroutineEvent),
targetDescriptor: nil,
returnID: AEReturnID(kAutoGenerateReturnID),
transactionID: AETransactionID(kAnyTransactionID)
)
event.setDescriptor(NSAppleEventDescriptor(string: "displayMessage"), forKeyword: AEKeyword(keyASSubroutineName))
event.setDescriptor(parameters, forKeyword: AEKeyword(keyDirectObject))
var error: NSDictionary? = nil
_ = self.script.executeAppleEvent(event, error: &error) as NSAppleEventDescriptor?
print ("runScript",self.script)
}
}