2012-01-18 80 views
1

在Xcode中运行我的AppleScript代码如下:变量传递到一个AppleScript

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

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

    [script executeAndReturnError:nil]; 

之前执行它,我想知道是否有可能将一些变量为它的使用。换句话说,我想将我的应用程序中的变量传递给applescript。

回答

4

你可以使用方法:

- (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:

+3

该解决方案替换它们并不好,因为它会导致重新编译脚本每次都可能是低效的。这不是真正的参数传递问题。更合适的答案在这里:http://stackoverflow.com/questions/6963072/execute-applescript-from-cocoa-app-with-params – 2014-07-04 14:12:18

相关问题