Has anyone embedded Applescript in Cocoa/Objective-C applications using NSAppleScript. This seems extremely powerful as it can be used to write a program that controls other programs. Is there a way to control other applications without Applescript?
Has anyone embedded Applescript in Cocoa/Objective-C applications using NSAppleScript. This seems extremely powerful as it can be used to write a program that controls other programs. Is there a way to control other applications without Applescript?
NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithContentsOfURL:scriptURL error:&errorDict];
returnDescriptor= [scriptObject executeAppleEvent:event error:&errorDict];
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
NSAppleScript *run;
NSAppleScript *start;
NSAppleScript *nextTrack;
NSAppleScript *backTrack;
}
-(IBAction)play:(id)sender;
-(IBAction)next:(id)sender;
-(IBAction)previous:(id)sender;
@end
#import "AppController.h"
@implementation AppController
-(void)awakeFromNib
{
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to run"];
[run executeAndReturnError:nil];
}
-(IBAction)play:(id)sender
{
NSAppleScript *start = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to playpause"];
[start executeAndReturnError:nil];
}
-(IBAction)next:(id)sender
{
NSAppleScript *nextTrack = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to next track"];
[nextTrack executeAndReturnError:nil];
}
-(IBAction)previous:(id)sender
{
NSAppleScript *backTrack = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to back track"];
[backTrack executeAndReturnError:nil];
}
@end
Can you elaborate?
How would one write this application using script bridges and then without using any Applescript at all?
// To create glue: osaglue -o ITGlue -p IT iTunes
@interface AppController : NSObject {
ITApplication *itunes;
}
-(IBAction)play:(id)sender;
-(IBAction)next:(id)sender;
-(IBAction)previous:(id)sender;
@end
@implementation AppController
-(void)awakeFromNib
{
itunes = [[ITApplication alloc] initWithBundleID: @"com.apple.itunes"];
}
-(void)dealloc {
[itunes release];
[super dealloc];
}
-(IBAction)play:(id)sender
{
[[itunes play] send];
}
-(IBAction)next:(id)sender
{
[[itunes nextTrack] send];
}
-(IBAction)previous:(id)sender
{
[[itunes previousTrack] send];
}
@end
Also, how would one retrieve the name of the song that is currently playing from iTunes in the form of an NSString?
tell application "iTunes"
name of current track
end tell
// To create glue: osaglue -o ITGlue -p IT iTunes
ITApplication *itunes = [[ITApplication alloc] initWithName: @"iTunes"];
ITReference *ref = [[itunes currentTrack] name];
id result = [ref getItem];