// // APApplescriptHandler.h // Helper Class to perform simple Applescript Message to an external // Application from within a Cocoa App. // // You are free to examine, use, alter and redistribute this code - // but please drop me a mail in any case - especially if you // changed something for better. // // Please excuse any bugs (of course, I do not take any warranties) // and even more my ugly english. // // Created by Stefan Mueller on 06.07.06. // Copyright 2006 Arabian Penguins. All rights reserved. // #import "APApplescriptHandler.h" @interface APApplescriptHandler (Private) - (NSString *) createScriptForMessage:(NSString *)message; - (NSAppleScript *) createAppleScriptForSource:(NSString *)sourceCode; @end @implementation APApplescriptHandler // Designated Initializer -(id)initWithApplicationName:(NSString *)applicationName { if ([self init]) { application = [applicationName copy]; error = [[NSDictionary alloc]init]; } return self; } // Accessors -(NSString *)applicationName{return application;} -(NSDictionary *)error{return error;} - (NSString*)createScriptForMessage:(NSString *)message { if ([self applicationName] && [[self applicationName] length ]> 1) { NSString * string = [[[NSString alloc] initWithFormat:@"tell application \"%s\"\n%s\nend tell\n", [[self applicationName] cString], [message cString]] autorelease]; return string; } return nil; } - (NSAppleScript*)createAppleScriptForSource:(NSString *)sourceCode { NSAppleScript * script = [[[NSAppleScript alloc] initWithSource:sourceCode] autorelease]; if ([script compileAndReturnError:&error]) { return script; } else return nil; } //Helper Methods -(bool)performMessage:(NSString *)message { return [self performScriptWithSource:[self createScriptForMessage:message]]; } -(NSString *)getStringForMessage:(NSString *)message{ return [self getStringForScriptWithSource:[self createScriptForMessage:message]]; } -(NSArray *)getArrayOfStringsForMessage:(NSString *)message{ return [self getArrayOfStringsForScriptWithSource:[self createScriptForMessage:message]]; } //Worker Methods -(bool)performScriptWithSource:(NSString *)sourceCode{ NSAppleScript * script = [self createAppleScriptForSource:sourceCode]; if (script) { NSAppleEventDescriptor * desc = [script executeAndReturnError:&error]; return (desc && [error count]==0 ); } return FALSE; } -(NSString *)getStringForScriptWithSource:(NSString *)sourceCode{ NSAppleScript * script = [self createAppleScriptForSource:sourceCode]; if (script) { NSAppleEventDescriptor *result = [script executeAndReturnError:&error]; if (result) { return [result stringValue]; } } return false; } -(NSArray *)getArrayOfStringsForScriptWithSource:(NSString *)sourceCode{ NSMutableArray * array = [[[NSMutableArray alloc] init] autorelease]; NSAppleScript * script = [self createAppleScriptForSource:sourceCode]; if (script) { NSAppleEventDescriptor *result = [script executeAndReturnError:&error]; if (result) { if ([result descriptorType]!=typeAEList) { NSException * myException = [NSException exceptionWithName:@"NoArrayException" reason:@"Returned Apple Event Descriptor is not an Array" userInfo:nil]; [myException raise]; }; int index; int total = [result numberOfItems]; for(index = 1; index <= total; index++) { NSAppleEventDescriptor * singleEvent = [result descriptorAtIndex:index]; [array addObject:[singleEvent stringValue]]; } return array; } } return false; } -(void)dealloc{ [application release]; [error release]; [super dealloc]; } @end