将变量传递给 AppleScript

发布于 2024-12-27 15:21:44 字数 362 浏览 3 评论 0原文

在 Xcode 中运行 AppleScript 的代码如下:

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

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

[script executeAndReturnError:nil];

在执行它之前,我想知道是否可以设置一些变量供其使用。换句话说,我想将变量从我的应用程序传递到 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2025-01-03 15:21:44

我发现的最好的例子是 Quinn “The Eskimo!” 中的这段代码。在 Apple 开发者论坛上:

https://forums.developer.apple.com/thread/98830< /a>

AppleScript 文件:

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

从 Swift 调用 AppleScript 方法,传递参数:

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

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)
    }
}
我最亲爱的 2025-01-03 15:21:44

您可以使用方法:

- (id)initWithSource:(NSString *)source

并使用 stringWithFormat 来构建您的 applescript 源并设置参数。

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

您还可以设计一种更高级的替换机制,在“Script.scpt”中以某种方式标记参数,然后使用 stringByReplacingOccurrencesOfString:withString: 替换它们

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:

零度° 2025-01-03 15:21:44

带返回值处理的版本。 (基于 Quinn 的代码)

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

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!)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文