简体   繁体   中英

I need to put an NSString variable in another string inside the initWithSource function, but I don't know how

I need to put an NSString variable between another string code to get the 'pathToSerialDevice' variable between the other code. Like this:

NSAppleScript *appleScript = [[NSAppleScript alloc]
                initWithSource:@"Tell application \"Terminal\" \n\
                                do script with command \"screen %@", pathToSerialDevice, "\" in front window\n\
                                end tell"];

And my pathToSerialDevice string is taken from a text field

pathToSerialDevice = [NSString stringWithFormat:_pathTextField.stringValue];

When I display it in the Log, it works, I do it so:

NSLog(@"Your path is %@", pathToSerialDevice);

How to do the same, but in the NSAppleScript? It doesn't work now and I have no idea how to do it. Please help me.

PS I have Xcode 6.1 on OS X 10.10 and it's an OS X app.

initWithSource: takes a regular string, not a format string. Read these docs on the difference .

The easiest thing to do is to just make another string:

NSString *pathToSerialDevice = [NSString stringWithFormat:_pathTextField.stringValue];
NSString *source = [NSString stringWithFormat:@"Tell application \"Terminal\" \n\
                                              do script with command \"screen %@ \" in front window\n\
                                              end tell", pathToSerialDevice];

NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:source];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM