views:

43

answers:

2

Ok here is part of the code that is causing the error:

    char charlieReturn[10000];
charlieReturn[10000] = system("osascript /applications/jarvis/scripts/getTextCharlieResponce.scpt");


 self.charlieOutput.stringValue = charlieReturn;

The getTextCharlieResponce.scpt returns something like this: "Hi my name is charlie" and maybe sometimes it will be longer than that. The script returns it plain text. I need help FAST!

Thanks in advance! :D

Elijah

A: 

You're trying to assign a character array to what is presumably an NSString. Try this instead:

self.charlieOutput.stringValue = [NSString stringWithUTF8String:charlieReturn];
Brian
Ok, it makes charlieOutput blank... :( Any ideas??
Elijah W.
+6  A: 

Unfortunately there are many problems in your code.

  1. The C function system does not return the standard output of the script as char*.

  2. Even with a function which returns char*, you can't assign it to a array of char as you did:

     char* str="aeiou";
     char foo[100];
     foo[100]=str;   /* doesn't work */
     foo=str;   /* doesn't work either */
    
  3. Cocoa's string class, NSString*, is not a C string char*, although you can easily convert between the two:

     NSString* str=[NSString stringWithUTF8String:"aeiou"];
    

If you want a string out of a call to an Apple script, you need to do the following:

  1. Prepare an NSAppleScript:

    NSDictionary* errorDict;
    NSAppleScript* script=[[NSAppleScript alloc] 
                            initWithContentsOfURL:[NSURL fileURLWithPath:@"path/to/script" ]
                                            error:&errorDict];
    
  2. Execute and get a reply:

    NSAppleEventDescriptor* desc=[script executeAndReturnError:&errorDict];
    NSString* result=[desc stringValue];
    
  3. Release the script:

    [script release];
    

Learn C & Objective-C and have fun!

Yuji
+1 Ok, much faster and in more depth... no need to replicate.
Eiko