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

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

问题描述

我在我的OBJ-C可可项目控制QuickTime播放器(播放,暂停,停止,跑步前进和后退等)取得了巨大成功,虽然我的AppleScript的了解是非常有限的一些使用AppleScript的。
然而,我想最重要的是电影的当前时间偏移转换成时间戳写字幕脚本。

以下简单的方法显示(浮动)秒钟在对话框中的precise当前的位置,但我真的很喜欢的AppleScript为 收益 变量我可以在应用程序的其余部分使用。我怎么能修改下面的code这样做呢?它甚至有可能访问此价值?万分感谢事先: - )

   - (IBAction为)currentPlayTime:(ID)发送者
{
    * NSString的scriptString = [的NSString stringWithFormat:
        //获取时间当前帧的...(作品完美)!
        @告诉应用程序\\QuickTime播放器\\\\ n
            @设定时间表,以600 \\ n
            @设置为curr_pos电影1 /时间表的\\ n当前时间。
            @显示对话框curr_pos的\\ n// ...不是一个实际的形式使用
        @结尾告诉\\ n];    *的NSDictionary = errorDict零;
    NSAppleScript * appleScriptObject = [[NSAppleScript页​​头] initWithSource:scriptString];
    NSAppleEventDescriptor * eventDescriptor = [appleScriptObject executeAndReturnError:放大器; errorDict]。
    //处理任何错误在这里(剪断为了简洁)
    [appleScriptObject发布] //我能留住吗?
}


解决方案

下面是你想要运行相应的AppleScript:

 属性的时间表:600集currentPosition缺少值告诉应用程序QuickTime播放器
    集currentPosition到/时间表(文件1的当前时间)
告诉结束返回currentPosition

在万一你不熟悉它,属性是指定的AppleScript中的一个全局变量的方法。此外,缺失值是等价的AppleScript 的Objective-C中。所以,这个脚本首先定义一个名为 currentPosition 变量,将值设置为缺失值。然后,它进入告诉块,如果成功,将改变 currentPosition 变量。然后,告诉块外,它返回 currentPosition 变量。

在Objective-C的code,当你创建一个 NSAppleScript 上述code,其 -executeAndReturnError:方法将在 NSAppleScriptEventDescriptor 返回 currentPosition 变量。

   - (IBAction为)currentPlayTime:(ID)发送{    *的NSDictionary错误=零;    *的NSMutableString = scriptText [的NSMutableString stringWithString:@属性的时间表:600 \\ n];
    [scriptText appendString:@设置为currentPosition缺失值\\ n];
    [scriptText appendString:@告诉应用程序\\QuickTime播放器\\\\ n];
    [scriptText appendString:@设置为currentPosition /时间表\\ N(当前文档1次)];
    [scriptText appendString:@到底告诉\\ n];
    [scriptText appendString:@返回currentPosition \\ n];    NSAppleScript *脚本= [[[NSAppleScript页​​头] initWithSource:scriptText]自动释放];    NSAppleEventDescriptor *结果= [脚本executeAndReturnError:放大器;错误]    的NSLog(@结果==%@,结果);    DescType的descriptorType = [导致的descriptorType]    的NSLog(@的descriptorType ==%@,NSFileTypeForHFSType code(的descriptorType));    //返回一个double    NSData的*数据= [结果数据]
    双currentPosition = 0;    [数据的getBytes:放大器; currentPosition长度:[数据长度]];    的NSLog(@currentPosition ==%F,currentPosition);
}

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

使用脚本桥框架并有轻微的学习曲线,但将允许使用原生类型,如的NSNumber 真是让人不必去有点混乱的路线工作中提取的原始字节出来的AppleEvent描述符。

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.

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?
}

解决方案

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

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.

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

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

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.

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

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