问题描述
在code运行我以x code 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!