(Mac) Creating Xcode app that executes shell scrip

2019-04-02 16:15发布

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

1条回答
Root(大扎)
2楼-- · 2019-04-02 17:03

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;
}
查看更多
登录 后发表回答