OK, for those who want to add some additional scripting capabilities to their movie view by implementing an Objective-C bridge but don’t know how. Here it is:
- Add the QuickTime framework to your project (by dragging /System/Library/Frameworks/QuickTime.framework to Frameworks)
- Add a new empty file to your project. Name it “methods.m”
- Paste this into that file:
#import <Cocoa/Cocoa.h>
#import <QuickTime/QuickTime.h>
@interface methods : NSObject
+ (NSDictionary *)getCurrentTime:(NSMovieView *)movieView;
+ (NSDictionary *)goToTime:(NSMovieView *)movieView startTime:(int)startTime;
+ (NSDictionary *)setSelection:(NSMovieView *)movieView startTime:(int)startTime endTime:(int)endTime;
+ (NSDictionary *)setRate:(NSMovieView *)movieView value:(double)value;
@end
@implementation methods
+ (NSDictionary *)getCurrentTime:(NSMovieView *)movieView {
Movie qtmovie = [[movieView movie] QTMovie];
TimeScale timeScale = GetMovieTimeScale(qtmovie);
return [NSNumber numberWithInt:( GetMovieTime (qtmovie, nil) / timeScale )];
}
+ (NSDictionary *)goToTime:(NSMovieView *)movieView startTime:(int)startTime {
Movie qtmovie = [[movieView movie] QTMovie];
TimeScale timeScale = GetMovieTimeScale(qtmovie);
TimeValue startMovieTime = (TimeValue) startTime * timeScale;
SetMovieTimeValue(qtmovie, startMovieTime);
return nil;
}
+ (NSDictionary *)setSelection:(NSMovieView *)movieView startTime:(int)startTime endTime:(int)endTime {
Movie qtmovie = [[movieView movie] QTMovie];
TimeScale timeScale = GetMovieTimeScale(qtmovie);
TimeValue selectionStart = (TimeValue) startTime * timeScale;
TimeValue selectionDuration = (TimeValue) (endTime - startTime) * timeScale;
SetMovieSelection(qtmovie, selectionStart, selectionDuration);
return nil;
}
+ (NSDictionary *)setRate:(NSMovieView *)movieView value:(double)value {
Movie qtmovie = [[movieView movie] QTMovie];
SetMovieRate(qtmovie, X2Fix(value));
return nil;
}
@end
- Now, from within your applescript, you can call these methods anytime you like. To get the current time, use:
set theMovieView to movie view 1 of window 1
set curTime to (call method "getCurrentTime:" of class "methods" with parameters {theMovieView})
--> returns current time in seconds
To set the current selection:
set theMovieView to movie view 1 of window 1
set selectionStart to 0 -- time in seconds
set selectionEnd to 10 -- time in seconds
call method "setSelection:startTime:endTime:" of class "methods" with parameters {theMovieView, selectionStart, selectionEnd}
To go to a specified time:
set theMovieView to movie view 1 of window 1
set selectionStart to 10 -- time in seconds
call method "goToTime:startTime:" of class "methods" with parameters {theMovieView, selectionStart}
To set the playback rate:
set theMovieView to movie view 1 of window 1
set theRate to 2 -- 2=double speed, 1=normal playback, -1=normal speed backwards
call method "setRate:value:" of class "methods" with parameters {theMovieView, theRate}
Additionally, if you would like to add a movie view programatically to an existing window, you can add this line to the interface part of your “methods.m” file:
+ (NSMovieView *)makeMovieView:(NSWindow *)theWindow movie:(NSMovie*)theMovie withFrame:(NSArray *)frameArray;
With the following implementation:
+ (NSMovieView *)makeMovieView:(NSWindow *)theWindow movie:(NSMovie*)theMovie withFrame:(NSArray *)frameArray {
// Create the movie view
int x, y, w, h;
x = [[frameArray objectAtIndex:0] intValue];
y = [[frameArray objectAtIndex:1] intValue];
w = [[frameArray objectAtIndex:2] intValue];
h = [[frameArray objectAtIndex:3] intValue];
NSMovieView* movieView = [[NSMovieView alloc] initWithFrame:NSMakeRect(x, y, w, h)];
// Load the movie
[movieView setMovie];
// Show the controller?
[movieView showController:NO adjustingSize:NO];
// Play selection only?
[movieView setPlaysSelectionOnly:YES];
// Add the movie view to the window
[[theWindow contentView] addSubview:movieView];
[movieView release];
return movieView;
}
To create a new movie view from within your script, all you have to do is to:
set theWindow to window 1
set theMovie to (load movie "MyMovie")
set x to 0
set y to 0 -- {0, 0} = bottom left corner of window
set w to 800 -- width of movie view in pixels
set h to 600 -- height of movie view in pixels
set newMovieView to (call method "makeMovieView:movie:withFrame:" of class "methods" with parameters {theWindow, theMovie, {x, y, w, h}})
In this example I have only implemented a few methods. To add some more, have a look at “ADC Home > Reference Library > Documentation > Cocoa > Objective-C Language > Application Kit Reference for Objective-C > NSMovieView” in the XCode documentation. By studying the examples provided here you should easily be able to do the neccessary substitutions. Have fun.