Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

magn2o

macrumors member
Original poster
Sep 22, 2008
43
0
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? :)
 
What happens if you do this:

Code:
NSString *msg = [NSString stringWithFormat:@"tell application \"itunes\"\n"
                           "activate\n"
                           "playpause"];
 
What happens if you do this:

Code:
NSString *msg = [NSString stringWithFormat:@"tell application \"itunes\"\n"
                           "activate\n"
                           "playpause"];

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.
 
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.
 
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.

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!
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.