问题描述
在 Xcode 中运行我的 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];
在执行它之前,我想知道是否可以设置一些变量供它使用.换句话说,我想将变量从我的应用程序传递给 AppleScript.
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.
推荐答案
您可以使用以下方法:
- (id)initWithSource:(NSString *)source
并使用 stringWithFormat
构建您的 applescript 源并设置参数.
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];
您还可以设计一种更高级的替换机制,在Script.scpt"中以某种方式标记您的参数,然后使用 stringByReplacingOccurrencesOfString:withString:
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:
这篇关于将变量传递给 AppleScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!