在 Obj-C 中获取 AppleScript 返回值 [英] Getting AppleScript return value in Obj-C

查看:34
本文介绍了在 Obj-C 中获取 AppleScript 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Obj-C cocoa 项目中使用了一些 AppleScript 来控制 QuickTime 播放器(播放、暂停、停止、向前和向后慢跑等)并取得了巨大的成功,尽管我对 AppleScript 的了解非常有限.但是,我最想要的是将电影的当前时间"偏移量转换为用于编写字幕脚本的时间戳.

I'm using some AppleScript in my Obj-C cocoa project to control QuickTime player (play, pause, stop, jog forward and back etc.) with great success, though my knowledge of AppleScript is very limited. However, what I want most of all is the movie's 'Current Time' offset to convert into time-stamps for writing a subtitle script.

以下简单方法在对话框中以(浮动)秒显示精确的当前位置,但我真的希望 AppleScript 返回我一个 变量,我可以在应用程序的其余部分使用.我怎样才能修改下面的代码来做到这一点?甚至可以访问这个值吗?预先感谢一百万:-)

The following simple method shows the precise current position in (float) seconds in a dialog, but I'd really like the AppleScript to return me a variable that I can use in the rest of app. How could I modify the code below to do that? Is it even possible to access this value? Thanks a million in advance :-)

-(IBAction)currentPlayTime:(id)sender
{
    NSString *scriptString=[NSString stringWithFormat:
        // get time of current frame... (works perfectly)!
        @"tell application \"QuickTime Player\"\n"
            @"set timeScale to 600\n"
            @"set curr_pos to current time of movie 1/timeScale\n"
            @"display dialog curr_pos\n" // ...not in a practical form to use
        @"end tell\n"];

    NSDictionary *errorDict= nil;
    NSAppleScript *appleScriptObject=[[NSAppleScript alloc] initWithSource:scriptString];
    NSAppleEventDescriptor *eventDescriptor=[appleScriptObject executeAndReturnError:   &errorDict];
    // handle any errors here (snipped for brevity)
    [appleScriptObject release]; // can I retain this?
}

推荐答案

以下是您想要运行的适当 AppleScript:

Here's the appropriate AppleScript that you'd want to run:

property timeScale : 600

set currentPosition to missing value

tell application "QuickTime Player"
    set currentPosition to (current time of document 1) / timeScale
end tell

return currentPosition

如果您不熟悉它,property 是一种在 AppleScript 中指定全局变量的方法.此外,missing value 是 Objective-C 中 nil 的 AppleScript 等价物.所以,这个脚本首先定义了一个名为currentPosition的变量,并将其值设置为missing value.然后它进入 tell 块,如果它成功,将改变 currentPosition 变量.然后,在 tell 块之外,它返回 currentPosition 变量.

In case you're not familiar with it, property is a way to specify a global variable in AppleScript. Also, missing value is the AppleScript equivalent of nil in Objective-C. So, this script first defines a variable named currentPosition, and sets the value to missing value. It then enters the tell block which, if it succeeds, will alter the currentPosition variable. Then, outside of the tell block, it returns the currentPosition variable.

在Objective-C代码中,当你用上面的代码创建一个NSAppleScript时,它的-executeAndReturnError:方法将返回currentPositionNSAppleScriptEventDescriptor 中的变量.

In the Objective-C code, when you create an NSAppleScript with the above code, its -executeAndReturnError: method will return the currentPosition variable in an NSAppleScriptEventDescriptor.

-(IBAction)currentPlayTime:(id)sender {

    NSDictionary *error = nil;

    NSMutableString *scriptText = [NSMutableString stringWithString:@"property timeScale : 600\n"];
    [scriptText appendString:@"set currentPosition to missing value\n"];
    [scriptText appendString:@"tell application \"QuickTime Player\"\n "];
    [scriptText appendString:@"set currentPosition to (current time of document 1) / timeScale\n"];
    [scriptText appendString:@"end tell\n"];
    [scriptText appendString:@"return currentPosition\n"];

    NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:scriptText] autorelease];

    NSAppleEventDescriptor *result = [script executeAndReturnError:&error];

    NSLog(@"result == %@", result);

    DescType descriptorType = [result descriptorType];

    NSLog(@"descriptorType == %@", NSFileTypeForHFSTypeCode(descriptorType));

    // returns a double

    NSData *data = [result data];
    double currentPosition = 0;

    [data getBytes:&currentPosition length:[data length]];

    NSLog(@"currentPosition == %f", currentPosition);
}

您可以如上所示提取NSAppleEventDescriptor的内容.

You can extract the contents of the NSAppleEventDescriptor as shown above.

使用 Scripting Bridge 框架确实有一个轻微的学习曲线,但允许使用诸如 NSNumbers 之类的本机类型,而不必走一些更麻烦"的路线来提取原始字节AppleEvent 描述符.

Using the Scripting Bridge framework does have a slight learning curve, but would allow working with native types such as NSNumbers rather than having to go the somewhat "messier" route of extracting the raw bytes out of AppleEvent descriptor.

这篇关于在 Obj-C 中获取 AppleScript 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆