views:

34

answers:

2

Here is the code:

- (IBAction) charlieImputText:(id)sender {

NSAppleScript *keystrokeReturn = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to keystroke return"];
[keystrokeReturn executeAndReturnError:nil];

[progressBarText startAnimation:self];

charlieImputSelf = [sender stringValue];

NSAppleScript *sendCharlieImput = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"tell application \"Terminal\" to do shell script %@", charlieImputSelf]];
[sendCharlieImput executeAndReturnError:nil];

NSDictionary* errorDict;
NSAppleScript* script=[[NSAppleScript alloc] 
                       initWithContentsOfURL:[NSURL fileURLWithPath:@"/applications/jarvis/scripts/getTextCharlieResponce.scpt" ]
                       error:&errorDict];
NSAppleEventDescriptor* desc=[script executeAndReturnError:&errorDict];
NSString* result=[desc stringValue];
self.charlieOutput.stringValue = result;
charlieOutput.textColor = [NSColor greenColor];
[script release];

[progressBarText stopAnimation:self];
}

Ok what this does is it sends what's ever in the text field to terminal and displays terminal's response on a text label. However, this does not work the way I want it to. It does not send the user input to terminal. Any ideas??

Thanks, Elijah

A: 

First: Does charlieImputSelf the value you expect?

Second: try passing an NSError into your [sendCharlieImput executeAndReturnError:nil]; Do something like:

NSAppleEventDescriptor * ourRes = [theScript executeAndReturnError: &errorDict];
if (ourRes == nil)
{
    // error...
    [NSApp activateIgnoringOtherApps: YES];
    showNSAlert( @"AppleScript error", [errorDict valueForKey:NSAppleScriptErrorMessage]);
}

(I'll leave you to provide the implementation of showNSAlert, or just use NSLog here...)

RyanWilcox
Yes, when I do this: NSLog(charlieImputSelf);It does what I want. Any ideas??
Elijah W.
Then see if Applescript is returning any errors to you (Like @Chuck mentioned, you probably need to change your applescript code to: "tell application \"Terminal\" to do shell script \"%@\""
RyanWilcox
+1  A: 

You need to quote the argument to do shell script. Also, this depends on there being a directory at the path /Applications/jarvis/scripts, which is kind of odd though not impossible.

Chuck
quote the argument? Can you show an example?? thanks for your help! :D
Elijah W.