PDA

View Full Version : Replace " with \"




LinusR
Jan 1, 2012, 02:33 PM
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,

Linus

EDIT: Wrong orthography



rossipoo
Jan 1, 2012, 05:09 PM
\\ will insert a backslash into a string.

For example:
[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]

LinusR
Jan 2, 2012, 05:45 AM
\\ will insert a backslash into a string.

For example:
[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]

Thanks, this worked awesome!

Thanks for your help,

Have a nice day,

Linus