简体   繁体   中英

How to pass arguments to AppleScripts in Objective-C?

I've embedded an AppleScript into my Xcode project. The script processId.scpt requires an argument. How can I add this when executing the script in Objective-C?

NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"processId" ofType:@"scpt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:scriptPath]) {
    NSURL *scriptUrl = [[NSURL alloc] initWithString:scriptPath];

    NSDictionary *error = nil;
    NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:scriptUrl error:&error];
    NSAppleEventDescriptor *result = [script executeAndReturnError:nil];

    NSData *data = [result data];
    int pid = 0;
    [data getBytes:&pid length:[data length]];

    NSLog(@"Result: %d. %@. %@.", pid, [result stringValue], [error description]);        
}

我不确定您如何在脚本中使用参数,但是请看一下这篇文章,以获取有关创建AEMEvent ,将参数附加到它,然后将其发送到脚本的信息。

Depending on your base SDK setting (it would need to be at least 10.6), another option would be to add the script as an AppleScriptObjC class and call the handlers/methods like normal Objective-C. See this page for instructions (and an example project), and the ASObjC Release Notes for naming conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM