在Objective-C里其实也可以运行AppleScript
第一种方式是Source 将脚本写到变量字符串里
NSAppleEventDescriptor *eventDescriptor = nil;
NSAppleScript *script = nil;
NSBundle *bunlde = [NSBundle mainBundle];
NSString *scriptSource = @"tell application \"Finder\"\r"
"display dialog \"test\"\r"
"end tell";
if (scriptSource)
{
script = [[NSAppleScript alloc] initWithSource:scriptSource];
if (script)
{
eventDescriptor = [script executeAndReturnError:nil];
if (eventDescriptor)
{
NSLog(@"%@", [eventDescriptor stringValue]);
}
}
}第二种方式是将File, 将脚本写到文件里
NSAppleEventDescriptor *eventDescriptor = nil;
NSAppleScript *script = nil;
NSBundle *bunlde = [NSBundle mainBundle];
NSString *scriptPath = @"/Users/exchen/Documents/test.scpt";
if (scriptPath)
{
script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:scriptPath] error:nil];
if (script)
{
eventDescriptor = [script executeAndReturnError:nil];
if (eventDescriptor)
{
NSLog(@"%@", [eventDescriptor stringValue]);
}
}
}
本文介绍了两种在 Objective-C 中执行 AppleScript 的方法:一种是通过源代码字符串直接执行;另一种是从文件中加载并执行。文章提供了具体的代码示例。
386

被折叠的 条评论
为什么被折叠?



