Skip navigation.
 
mlNSAppleScript question
FROM : Matthew Delves
DATE : Sat Feb 02 08:12:43 2008

Greetings,
I'm currently bashing my head against the wall (rather painfully) 
trying to get NSAppleScript functioning the way I want.
How I currently have this setup is there are several applescript 
scripts that are located in the resources section of the main bundle 
and I am trying to load them with the following code.

   NSDictionary *loadError = [[NSDictionary alloc] init];
   NSDictionary *error = [[NSDictionary alloc] init];
   NSAppleEventDescriptor *returnDescriptor = nil;
   NSString *scriptLocation = [[NSString alloc] initWithFormat:@"%@/%@", 
[[NSBundle mainBundle] resourcePath], 
@"RSITunesLaunchApplescript.scpt"];
   NSURL        *fileURL = [[NSURL alloc] initWithString:scriptLocation];
   NSFileManager *manager = [NSFileManager defaultManager];
   NSAppleScript *scriptObject = nil;

   // Create the script here
   if([manager fileExistsAtPath:scriptLocation]) {
       scriptObject = [[NSAppleScript alloc] initWithContentsOfURL:fileURL 
error:&loadError];

       // Both scriptObject and loadError are null for some unknown reason

       if(scriptObject == nil) {
           NSLog(@"The error is: %@", [loadError 
valueForKey:NSAppleScriptErrorMessage]);
       }
   }
   
   NSLog(@"%@", scriptObject);

   // Run the script
   NSLog(@"executing script");
   if(scriptObject != nil)
       returnDescriptor = [scriptObject executeAndReturnError: &error];
   
    if ([returnDescriptor descriptorType]) {
        NSLog(@"script executed sucessfully.");
    } else {
        NSLog(@"Script execution has gone a bit pear shaped: %@", 
[error objectForKey: @"NSAppleScriptErrorMessage"]);
    }

What is happening is that the NSFileManager is correctly showing the 
existance of the applescript file though when I use NSAppleScript 
initWithContentsOfURL: error: I get both objects becoming nil.

As far as the configuration for the project, I'm using XCode 3, 
Leopard and have Garbage Collection turned on.

Any help would be greatly appreciated as my head is rather sore.

Thanks,
Matthew Delves

Related mailsAuthorDate
mlNSAppleScript question Matthew Delves Feb 2, 08:12
mlRe: NSAppleScript question Matthew Delves Feb 2, 10:34
mlRe: NSAppleScript question Nir Soffer Feb 5, 00:23