当前位置: 首页 > 知识库问答 >
问题:

在macOS中使用AppleScript与Apple Events-Script无法工作

卢阳泽
2023-03-14
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)

        }
    }


共有1个答案

梁丘诚
2023-03-14

这段代码的问题--请注意,这是一个令人难以置信的不明显的问题-- 是,您使用的代码旨在运行一个脚本处理程序(方法或子例程),试图运行完整的脚本。OBJ-C的AppleScript类的一个奇怪之处在于,没有一种简单的方法来运行带有参数的脚本,因此解决方法是将要执行的代码封装在脚本处理程序中,并使用调用该处理程序的Apple事件。为了使代码正常工作,您将执行以下操作...

首先,更改脚本,使代码位于处理程序中:

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

    -- This is our handler definition
    on sendMyEmail(theSubject, theContent, recipientName, recipientAddress, attachmentPath)
        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}

                make new attachment with properties {file name:POSIX file attachmentPath}

               -- Mail.app needs a moment to process the attachment, so...
               delay 1

               -- Send the message
               send 
            end tell
        end tell
    end sendMyEmail
    """  
    )!  

然后更改您构造的Apple事件,使其传递参数并调用我们刚才定义的处理程序:

func runScript() {
    let parameters = NSAppleEventDescriptor.list()  
    parameters.insert(NSAppleEventDescriptor(string: "Some Subject"), at: 0)  
    parameters.insert(NSAppleEventDescriptor(string: "Some content of the email"), at: 0)  
    parameters.insert(NSAppleEventDescriptor(string: "Some Name"), at: 0)  
    parameters.insert(NSAppleEventDescriptor(string: "someone@example.com"), at: 0)  
    parameters.insert(NSAppleEventDescriptor(string: attachmentFileURL.path), 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)

    }
}
 类似资料:
  • 我有运行一些Applescript的bash函数。如果我在Applescript编辑器或Textate中运行Applescript部分,它可以正常工作,但是在命令行上,函数失败了... 错误是: 我知道(认为)问题在于这一行的第一个bash转义序列: 但我不知道为什么失败了。。。你知道为什么这行不通吗? 编辑1:我也尝试过这个,但失败了: 错误消息:

  • 试图使用https://github.com/japlscript/obstmusic交谈苹果音乐应用程序在macOS与Java,我曾经编写原生AppleScript,然后java Applescript库,但从Java删除。 在这个方法中,它会查找一个名为songkong的现有文件夹播放列表,找到后返回。如果不存在,则创建这样一个文件夹,然后返回。 我第一次运行它时,我必须创建一个文件夹播放列表

  • All-In-One Mac Update script �� Inspired by the articleKeeping macOS clean. This is a zsh Mac update script that updates all software I could find to be updated via Terminal on macOS. Missing commands

  • 问题内容: 这是Websphere上@StartupEJB的用户上下文的后续操作 我有以下情况: EJB 1: EJB 2: 简而言之:我有一个EJB,它需要一个角色“ SomeRole”,以及一个用于使用该角色的启动EJB 。 据我了解,这应该工作。 但是,我得到以下异常(类和角色名称已更改为与我的示例匹配): 就我而言,这只是误解吗? 我正在使用WebSphere 8.0.0.9 问题答案:

  • 感谢您的点击。几个小时以来,我一直试图解决这个问题,但似乎没有任何效果。 我的操作系统:MacOS sierra OS版本:10.12.3 R版本:3.3.3 我已经按照数据表下载说明在这里:https://github.com/Rdatatable/data.table/wiki/Installation,但它仍然没有工作 错误消息如下所示:

  • 这是我第一次面对这个问题,但想不出原因。 我正在使用d3创建一个冰柱图。 有一个click事件正在激发并调用。我看到控制台日志,这意味着我确实有权访问,但当我尝试设置它时,什么也没有发生:没有新页面,没有错误页面,什么也没有!