Andrew
Andrew

Reputation: 16051

Passing variables to an AppleScript

The code to run my AppleScript in Xcode is the following:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Script" ofType:@"scpt"];

NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

[script executeAndReturnError:nil];

Before executing it, I was wondering if it was possible to set some variables up for it to use. In other words, I want to pass variables from my app to an AppleScript.

Upvotes: 5

Views: 4754

Answers (3)

Serge Ivamov
Serge Ivamov

Reputation: 364

Version with return value processing. (based on Quinn's code)

import Carbon
// ...
let script = """
on displayMessage(message)
    display dialog message
    return "hello…"
end displayMessage
"""

let handler = NSAppleEventDescriptor(string: "displayMessage")
let message = NSAppleEventDescriptor(string: "Hello AppleScript!")

let parameters = NSAppleEventDescriptor.list()
parameters.insert(message, at: 0)

let event = NSAppleEventDescriptor(
    eventClass: AEEventClass(kASAppleScriptSuite),
    eventID: AEEventID(kASSubroutineEvent),
    targetDescriptor: nil,
    returnID: AEReturnID(kAutoGenerateReturnID),
    transactionID: AETransactionID(kAnyTransactionID)
)
event.setDescriptor(handler, forKeyword: AEKeyword(keyASSubroutineName))
event.setDescriptor(parameters, forKeyword: AEKeyword(keyDirectObject))

var error: NSDictionary?
if let appleScript = NSAppleScript(source: script) {
    if let outputString = appleScript.executeAppleEvent(event, error: &error).stringValue {
        print("output: ", outputString)
    } else if (error != nil) {
        print("error: ", error!)
    }
}

Upvotes: 1

pkamb
pkamb

Reputation: 35012

The best example I've found is this code from Quinn "The Eskimo!" on the Apple Developer Forums:

https://forums.developer.apple.com/thread/98830

AppleScript file:

on displayMessage(message)  
    tell application "Finder"  
        activate  
        display dialog message buttons {"OK"} default button "OK"  
    end tell  
end displayMessage 

Call the AppleScript method from Swift, passing parameters:

import Carbon

// ...

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

let appleScript = try! NSUserAppleScriptTask(url: yourAppleScriptFileURL)
appleScript.execute(withAppleEvent: event) { (appleEvent, error) in
    if let error = error {
        print(error)
    }
}

Upvotes: 6

sergio
sergio

Reputation: 69037

You could use the method:

- (id)initWithSource:(NSString *)source

and use stringWithFormat to build your applescript source and setting the arguments.

NSString* scriptTemplate = ...;
NSString* actualScript = [NSString stringWithFormat:scriptTemplate, arg1, arg2, ... argN];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:actualScript];

You could also devise a more advanced replacement mechanism, where you tag somehow your parameters in "Script.scpt" and then replace them using stringByReplacingOccurrencesOfString:withString:

Upvotes: 4

Related Questions