我想从我的应用程序中运行几行applescript代码。标准方法是使用NSAppleScript类。但是,因为代码可能需要几分钟才能完成,所以我必须使用单独线程,否则接口将停止。最大的问题是,正如它所说的here,NSAppleScript类只能在主线程上运行。
因此,如果我在单独的线程上运行代码,我的应用程序就会崩溃;如果我在主线程上运行它,它就会停止。有什么想法吗?
另外,我也考虑过使用NSTask和osascript命令,但我发现osascript在某些地方(找不到链接)不支持用户输入,比如对话框和其他东西。我不确定这是否是真的,但如果是真的,那么osascript就不是一个解决方案。
发布于 2012-09-17 01:09:22
因此,我最终编写了一个帮助器,通过NSTask启动。下面是代码,如果有人感兴趣的话:
对于启动器:
NSArray* args = [NSArray arrayWithObject: <<AS code here>>];
task = [NSTask new];
[task setLaunchPath: [[NSBundle mainBundle] pathForResource: @"ASHelper" ofType: @""]];
[task setArguments: args];
[task launch];帮助者:
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString* source = [NSString stringWithCString: argv[1] length: strlen(argv[1])];
NSAppleScript* as = [[NSAppleScript alloc] initWithSource: source];
[as executeAndReturnError: nil];
}
return 0;
}https://stackoverflow.com/questions/12195203
复制相似问题