1

I'm having trouble escaping a path containing spaces using NSApplescript:

    // mybashscript is in the bundle app (NSlog grant that is ok!)

    NSDictionary*errorDict = nil;

    NSAppleScript*mycommand;
    NSString *mycommand = [mybashscript stringByReplacingOccurrencesOfString:@" " withString:@"\\ "]; 
    // NSString *mycommand = [[mybashscript stringByReplacingOccurrencesOfString:@" " withString:@"\\ "] stringByReplacingOccurrencesOfString:@"/" withString:@":"]; // another test made

    mycommand = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", escapedPath]];

    NSAppleEventDescriptor *eventDescriptor = [sudoPandoraMaker executeAndReturnError: &errorDict];


    if (([eventDescriptor descriptorType]) && (errorDict==nil)) {
        // if error is nil....is ok.. not this case :-(
    } else {
        NSLog(@"escapedPath รจ:%@", escapedPath);
        // what's was wrong???
    }

the above code works well only when the app is in paths that do not contain spaces, but when it is moved into folders or hard drives that contain spaces in their name, NSAppleScript fails. Any suggestion? Thanks

Mike97
  • 596
  • 5
  • 20

2 Answers2

0

You should at least inspect the returned eventDescriptor and errorDict to see what went wrong!

NSLog(@"result: %@", eventDescriptor);
NSLog(@"errors: %@", errorDict);

Offhand, I'd be suspicious of the bash script not handling spaces in paths correctly.

Ron Reuter
  • 1,287
  • 1
  • 8
  • 14
  • Thank you for responding, but as you can see the "event error handler" is set, so the problem that I have established, is that paths with spaces are difficult to solve (with NSString). I solved it with a workaround: the application must be in /Application folder... โ€“ Mike97 Aug 25 '14 at 11:13
0

NSApplescript consider asterisk as space, so this suppose to work:

NSString *badPath  = @"path with spaces"
NSString *goodPath = [badPath stringByReplacingOccurrencesOfString:@" " withString:@"*"]; //path*with*spaces
Chaya
  • 36
  • 4