Ben*_*der 7 applescript objective-c
我在我的Obj-C可可项目中使用了一些AppleScript来控制QuickTime播放器(播放,暂停,停止,慢跑前进和后退等)并取得了巨大的成功,尽管我对AppleScript的了解非常有限.然而,我最想要的是电影的"当前时间"偏移量,可以转换成用于编写字幕脚本的时间戳.
下面的简单方法在对话框中显示(浮点)秒的精确当前位置,但我真的希望AppleScript 返回一个我可以在其余应用程序中使用的变量.我怎么能修改下面的代码呢?甚至可以访问此值吗?提前一百万感谢:-)
-(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?
}
Run Code Online (Sandbox Code Playgroud)
NSG*_*God 17
这是您想要运行的相应AppleScript:
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
Run Code Online (Sandbox Code Playgroud)
如果您不熟悉它,property可以在AppleScript中指定全局变量.此外,missing valueAppleScript与nilObjective-C 相当.因此,此脚本首先定义一个名为的变量currentPosition,并将值设置为missing value.然后它进入tell块,如果成功,将改变currentPosition变量.然后,在tell块之外,它返回currentPosition变量.
在Objective-C代码中,当您NSAppleScript使用上面的代码创建一个时,它的-executeAndReturnError:方法将返回一个currentPosition变量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:¤tPosition length:[data length]];
NSLog(@"currentPosition == %f", currentPosition);
}
Run Code Online (Sandbox Code Playgroud)
您可以提取NSAppleEventDescriptor如上所示的内容.
使用Scripting Bridge框架确实有一个轻微的学习曲线,但允许使用本机类型(如NSNumbers),而不必采用从AppleEvent描述符中提取原始字节的"混乱"路径.
| 归档时间: |
|
| 查看次数: |
4638 次 |
| 最近记录: |