shell script in AppleScript ?

  • 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?
  • On 18/01/2008, at 3: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?

    You should be using NSTask if you want to run shell scripts with your
    Cocoa application. Is there a reason you want to use AppleScript?

    --
    Rob Keniger
  • 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
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today