1

It seems like such a simple thing I'm trying to accomplish, much less than what Xcode / Interface Builder are capable of. I've searched and searched and not come up with my answer but most searches lead me here so I created an account to ask the experts. I want to create a very simple GUI that will have four to five buttons, each button executing a simple shell script, a terminal window won't be necessary but I can live with one starting if that's the way it is. Along with the shell scripts I need to have adb (Android debug bridge) and fastboot utility also within the app. I'm assuming I need to place adb and fastboot and my other files within the Resources folder, I'm also assuming I need to place my shell scrips within the Classes folder. I really just need to know how to connect the buttons to the scripts, I'm hoping it's just something simple that I'm overlooking. Thanks in advance.

MacBook Pro 7,1 OSX 10.6.8 Xcode 3.2.6

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
Bif Pib
  • 11
  • 1
  • 2
  • You don't get it right. It's not about "connecting buttons to scripts". What you have to do is to connect the buttons to some action in your implementation file, which in turn will run your script. Now, as for your script's location, it'd better be somewhere in your bundle, so that you could access its path with something like `[[NSBundle mainBundle] pathForResource:@"yourscript" ofType:@"py"]`. Have a look at my answer for a... "command execution function". (Be careful, and study it; it's asynchronous). – Dr.Kameleon Apr 10 '12 at 02:03
  • As a sidenote : YES, Xcode and Cocoa DO make a developer's life easy, IMHO. But, this doesn't mean by any means that's it a "push that button and there you go" thing, huh? – Dr.Kameleon Apr 10 '12 at 02:07
  • Understood, I realize this is a whole new language for me, when first looking into making an app, the youtube video "tutorials" making a "hide" app really do misrepresent how XCode works and does give a false sense of ability. I appreciate your reply, I will study the code below and try to make sense of it. – Bif Pib Apr 10 '12 at 02:14
  • 1
    IMHO (just a subjective one, huh?), If you're THAT new to Cocoa and have started from youTube tutorials, please do a favor to yourself and do it properly. Buy a book (I'd recommend Hillegass's classic : http://www.amazon.com/Cocoa-Programming-Mac-3rd-Edition/dp/0321503619) and start doing some tests on your own, with something simpler (external script execution is NOT). Once you get a good idea, you'll love it. (Unfortunately, for now, my code below (with so many Cocoa concepts that you may be unfamiliar with) will most likely not make much sense to you...) Just my 2 cents... Good luck! ;-) – Dr.Kameleon Apr 10 '12 at 02:19
  • Thanks again Doc, I am brand spankin' new to Cocoa so that script is almost Latin to me. I'll check out that Amazon link, it does seem like I need to get my feet wet a bit before jumping in the deep end. Again, I appreciate your time. – Bif Pib Apr 10 '12 at 02:27
  • You're welcome, my friend! :-) – Dr.Kameleon Apr 10 '12 at 02:33
  • possible duplicate of [Execute a terminal command from a Cocoa app](http://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app) – mmmmmm Sep 14 '14 at 21:33

1 Answers1

8

Try this :

- (void)runCmd:(NSString *)cmd withArgs:(NSArray *)args
{
    if (task)
    {
        [task interrupt];

    }
    else
    {
        task = [[NSTask alloc] init];
        [task setLaunchPath:cmd];

        [task setArguments:args];

        [pipe release];

        pipe = [[NSPipe alloc] init];
        [task setStandardOutput:pipe];

        NSFileHandle* fh = [pipe fileHandleForReading];

        NSNotificationCenter* nc;

        nc = [NSNotificationCenter defaultCenter];
        [nc removeObserver:self];
        [nc addObserver:self 
               selector:@selector(dataReady:) 
                   name:NSFileHandleReadCompletionNotification 
                 object:fh];
        [nc addObserver:self selector:@selector(dataAvailable:) name:NSFileHandleDataAvailableNotification object:fh];
        [nc addObserver:self 
               selector:@selector(taskTerminated:) 
                   name:NSTaskDidTerminateNotification 
                 object:task];

        [task launch];
        [fh readInBackgroundAndNotify];
    }
}

- (void)dataAvailable:(NSNotification*)n
{
    NSLog(@"Data Available : %@",n);
}

- (void)dataReady:(NSNotification*)n
{
    NSData* d;

    d = [[n userInfo] valueForKey:NSFileHandleNotificationDataItem];

    if ([d length])
    {
        NSLog(@"Data Ready : %@",[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]);
        [[[task standardOutput] fileHandleForReading] readInBackgroundAndNotify];
    }
}

- (void) taskTerminated:(NSNotification*)note
{
    [task release];
    task = nil;
}
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223