在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]);
            }
        }
    }



相关文章:

  • 2022-02-16
  • 2021-08-05
  • 2021-07-24
  • 2022-12-23
  • 2022-03-03
  • 2021-12-22
猜你喜欢
  • 2021-06-16
  • 2022-01-04
  • 2021-08-15
  • 2021-05-25
  • 2021-12-14
  • 2021-10-19
相关资源
相似解决方案