Re: shell script in AppleScript ?
Re: shell script in AppleScript ?
- Subject: Re: shell script in AppleScript ?
- From: Christopher Nebel <email@hidden>
- Date: Mon, 21 Jan 2008 12:20:41 -0800
On Jan 17, 2008, at 9:50 PM, Caroline wrote:
I'm using NSAppleScript & "do shell script" in my Cocoa App.
But, I have some problems.
In Terminal,
script is -> cmd option "Picture 1" "Picture 2".
It is good.
But in cocoa App,
script is -> NSString *sCmd = @"cmd option \"Picture 1\" \"Picture
2\";
NSAppleScript *script = [NSAppleScript alloc] initWithSource:
[NSString strintWithFormat:@"do shell script %@",sCmd]];
Using '\"' because whitespace. (between Picture and 1)
It has error.
(NSAppleScriptErrorMessage : "A identifier can\U2019t go after this
\U201c\"\U201d.";)
How can I deal whitespace?
The direct answer is that you have to write a valid AppleScript
script, which means dealing with three levels of quoting: sh's,
AppleScript's, and Objective-C's. Your original command is this:
cmd option "Picture 1" "Picture 2"
AppleScript's "do shell script" command takes a string, which it
passes to sh, so you need to put quotes around the whole string and
then escape the interior quotes, much like C:
do shell script "cmd option \"Picture 1\" \"Picture 2\""
And finally you need to put that as a string literal in C, which means
escaping the interior quotes and backslashes again:
NSString *script = @"do shell script \"cmd option \\\"Picture 1\\\" \\
\"Picture 2\\\"\""
The indirect answer is that this is all a colossal waste of time, and
you should use NSTask instead. You get better control, it's faster
because you don't invoke two intermediate interpreters, and you don't
have to deal with the quoting mess. Pseudo-coding the constants for
brevity:
NSArray *args = [ @"option", @"Picture 1", @"Picture 2" ];
// notice, no quotes in the args, because we're going directly to
the command.
[NSTask launchedTaskWithLaunchPath:"@/full/path/to/cmd"
arguments:args];
--Chris Nebel
AppleScript Engineering
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden