gpt4 book ai didi

swift - AppleScript 电子邮件附件在处理程序中不起作用

转载 作者:行者123 更新时间:2023-11-28 09:21:40 24 4
gpt4 key购买 nike

我们有一个可用的 AppleScript,它可以创建带有附件的电子邮件,并且在脚本编辑器中运行良好。但是,我们无法让附件在我们的 macOS 应用程序中使用的处理程序中工作。很难找到文档并找出激活和发送命令之间的区别。感谢您提供的任何帮助。

这是 AppleScript

set recipientAddress to "joe@joe.com"
set recipientName to "Joe"
set theSubject to "Estimate"
set theContent to "This is your Estimate. Please call with any questions."
set theAttachment to "/Users/iMac/Library/Containers/com.jzmobile.JZMac/Data/Documents/PDF1"

tell application "Mail"
set outgoingMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
tell outgoingMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
tell content of outgoingMessage
make new attachment with properties {file name:theAttachment}
end tell
end tell
activate
end tell

这是处理程序:

var script: NSAppleScript = { 
let script = NSAppleScript(source: """

-- This is our handler definition
on sendMyEmail(theSubject, theContent, recipientName, recipientAddress, attachment)
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}
tell content of outgoingMessage
make new attachment with properties {file name:attachment}

#activate
end tell
-- Send the message
send
end tell

end tell
#activate

end sendMyEmail
"""
)!
let success = script.compileAndReturnError(nil)
assert(success)
return script
}()

@objc func runScript() {

let parameters = NSAppleEventDescriptor.list()
parameters.insert(NSAppleEventDescriptor(string: subject), at: 0)
parameters.insert(NSAppleEventDescriptor(string: "Some content of the email"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: "John Smith"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: "john@email.com"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: attachmentFileURL), at: 0)

let event = NSAppleEventDescriptor(
eventClass: AEEventClass(kASAppleScriptSuite),
eventID: AEEventID(kASSubroutineEvent),
targetDescriptor: nil,
returnID: AEReturnID(kAutoGenerateReturnID),
transactionID: AETransactionID(kAnyTransactionID)
)

// this line sets the name of the target handler
event.setDescriptor(NSAppleEventDescriptor(string: "sendMyEmail"), forKeyword: AEKeyword(keyASSubroutineName))

// this line adds the parameter list we constructed above
event.setDescriptor(parameters, forKeyword: AEKeyword(keyDirectObject))

var error: NSDictionary? = nil
_ = self.script.executeAppleEvent(event, error: &error) as NSAppleEventDescriptor?

print ("runScript",self.script)

}

最佳答案

传递路径字符串是行不通的;沙盒会阻止它。您必须传递一个文件对象(别名/POSIX 文件)。

沙盒还会限制您自己的应用访问任意文件系统位置的能力。

关于swift - AppleScript 电子邮件附件在处理程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57777548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com