Evaluate \n in NSData to NSString conversion

Discussion in 'Mac Programming' started by magn2o, Apr 11, 2010.

  1. magn2o macrumors member

    Joined:
    Sep 22, 2008
    #1
    First off, let me just get this out of the way: I am not a Obj-C/Cocoa programmer by any stretch of the imagination. I've fumbled my way through this enough to get the result I'm looking for, sans this one issue.

    Ok then.

    In the below code, I'm simply converting NSData to NSString. This is working.
    Code:
    	NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
    	NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
    
    However, I'm then feeding that NSString result to NSAppleScript:
    Code:
    		NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:msg];
    Again, this is working -- however, only if providing a single line/command (ie. 'tell application "iTunes" to launch')

    If I attempt to provide a multi-line command, such as:
    Code:
    tell application "iTunes"\nactivate\nplaypause\nend tell
    Then it attempts to evaluate the string 'as-is', rather than considering the \n as a line-feed.

    If i make *msg a static value:
    Code:
    NSString *msg = @"tell application \"itunes\"\n"
                               "activate\n"
                               "playpause";
    
    This works fine. So, I figure, it has to be somewhere in the NSString to NSData conversion.

    Am I making any sense? :)
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    What happens if you do this:

    Code:
    NSString *msg = [NSString stringWithFormat:@"tell application \"itunes\"\n"
                               "activate\n"
                               "playpause"];
     
  3. magn2o thread starter macrumors member

    Joined:
    Sep 22, 2008
    #3
    Looking in the log, yes, the \n is properly evaluated. Each command reports on a newline as expected.

    Just to clarify, the app takes the AppleScript command to send as program input -- it's not static. I'm able to set a static value and get the desired result just fine, just not as an input.
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    If I understand correctly, then the user is typing in a literal backslash followed by a literal n. You then need to translate this to a newline character.

    There is an NSString method that can do replacement. Read the NSString reference doc and find the word "replace".

    Because backslash has special meaning in source code, you must code the user-entered value as @"\\n", i.e. a literal backslash followed by a literal n.

    This assumes I understand the problem and the goal correctly. I think you need to explain where the NSData originally comes from, that you then turn into an NSString, and then attempt to execute as an AppleScript. It's the content of the NSData that ultimately determines what happens.
     
  6. magn2o thread starter macrumors member

    Joined:
    Sep 22, 2008
    #6
    This is exactly what I was looking for, thank you! stringByReplacingOccurrencesOfString is what I needed, and everything is working as I had hoped.

    Thanks again!
     
  7. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    OOOOhhh that makes sense. I didn't know someone was typing it in with the \n's in the line input.. very odd but I understand now what you were looking for.
     

Share This Page