Replace " with \"

Discussion in 'Mac Programming' started by LinusR, Jan 1, 2012.

  1. macrumors regular

    Joined:
    Jan 3, 2011
    #1
    Good evening,

    I would like to use an AppleScript. This AppleScript contains names with quotation marks (""). But to use them in AppleScript I have to use \". Now I tried to replace them by
    Code:
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
    But how shall I declare the quotation mark? My first try looked like this:

    Code:
    NSString *name = [[textField stringValue] stringByReplacingOccurrencesOfString:@"\"" withString:@"\""];
    
    But because of the (\) Objective-C thinks I want to replace " with ". Can anyone help? My AppleScript looks like this:

    Code:
    NSString *script = [NSString stringWithFormat:@"tell application \"iTunes\" \n play (every track of playlist 1 whose name is \"%@\") \n end tell", name];
    
    iTunes doesn't play the track when the track title contains quotation marks. So they have to be replaced by (\"). Does anyone know a solution?

    Thanks in advance,

    Linus

    EDIT: Wrong orthography
     
  2. macrumors regular

    Joined:
    Jun 7, 2009
    #2
    \\ will insert a backslash into a string.

    For example:
    [string stringByReplacingOccurrencesOfString:mad:"\"" withString:mad:"\\\""]
     
  3. thread starter macrumors regular

    Joined:
    Jan 3, 2011
    #3
    Thanks, this worked awesome!

    Thanks for your help,

    Have a nice day,

    Linus
     

Share This Page