AppleScript电子邮件附件在处理程序中不起作用 [英] AppleScript Email Attachment not working in handler

查看:219
本文介绍了AppleScript电子邮件附件在处理程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a working AppleScript that creates an email with an attachment and it works fine in the Script Editor. However, we cannot get the attachment to work in the handler used in our macOS app. Difficult to find doc and figure out the difference between activate and send commands. Thanks for any help you can provide.

这是AppleScript

Here is the 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文件).

Passing a path string will not work; the sandbox will block it. You must pass a file object (alias/POSIX file).

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

The sandbox will also restrict your own app’s ability to access arbitrary file system locations.

这篇关于AppleScript电子邮件附件在处理程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆