将变量传递给 AppleScript
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现的最好的例子是 Quinn “The Eskimo!” 中的这段代码。在 Apple 开发者论坛上:
https://forums.developer.apple.com/thread/98830< /a>
AppleScript 文件:
从 Swift 调用 AppleScript 方法,传递参数:
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:
Call the AppleScript method from Swift, passing parameters:
您可以使用方法:
并使用 stringWithFormat 来构建您的 applescript 源并设置参数。
您还可以设计一种更高级的替换机制,在“Script.scpt”中以某种方式标记参数,然后使用 stringByReplacingOccurrencesOfString:withString: 替换它们
You could use the method:
and use
stringWithFormat
to build your applescript source and setting the arguments.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:
带返回值处理的版本。 (基于 Quinn 的代码)
Version with return value processing. (based on Quinn's code)