• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: shell script in AppleScript ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >shell script in AppleScript ? (From: Caroline <email@hidden>)

  • Prev by Date: Re: Leopard - interaction with Spaces from own applications
  • Next by Date: Re: System preferences panes on Leopard
  • Previous by thread: Re: shell script in AppleScript ?
  • Next by thread: Implementing Leopard features with Tiger compatibility
  • Index(es):
    • Date
    • Thread