Using AppleScript with Apple Events in macOS - Script not working

前端 未结 1 1491
野的像风
野的像风 2021-01-15 04:01

We need to use a AppleScript to create an outgoing email message in macOS. The script works fine in the Script Editor. Using the code recommended by DTS https://forums.dev

相关标签:
1条回答
  • 2021-01-15 04:04

    The problem with this code — which is an incredibly un-obvious problem, mind you — is that you're using code meant to run a script handler (a method or subroutine) to try to run the full script. One of the oddnesses of Obj-C's AppleScript classes is that there is no easy way to run a script with parameters, so the workaround is to enclose the code to be executed within a script handler, and use an Apple Event that calls that handler. To make your code work, you'll do something like the following...

    First, change the script so that the code is in a handler:

    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
        """  
        )!  
    

    Then alter the Apple Event you construct so that it passes the parameters and calls the handler we just defined:

    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)
    
        }
    }
    

    If you don't need to pass parameters, you could run the script directly using script.executeAndReturnError(&error), but if you need to pass parameters, you'll need to use this 'handler' approach.

    0 讨论(0)
提交回复
热议问题