Replace " with \"

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

  1. macrumors regular

    Jan 3, 2011
    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
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
    But how shall I declare the quotation mark? My first try looked like this:

    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:

    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,


    EDIT: Wrong orthography
  2. macrumors regular

    Jun 7, 2009
    \\ will insert a backslash into a string.

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

    Jan 3, 2011
    Thanks, this worked awesome!

    Thanks for your help,

    Have a nice day,


Share This Page