我似乎想完成的一件简单的事情,比Xcode / Interface Builder的功能要少得多。我已经搜索了很多,没有找到答案,但是大多数搜索都把我引到了这里,所以我创建了一个帐户来询问专家。我想创建一个非常简单的GUI,它具有四个到五个按钮,每个按钮都执行一个简单的shell脚本,不需要终端窗口,但是如果那样的话,我可以从一个开始。除了shell脚本外,我还需要在应用程序中包含adb(Android调试桥)和fastboot实用程序。我假设需要将adb和fastboot以及其他文件放置在Resources文件夹中,还假设需要将shell脚本放置在Classes文件夹中。我真的只需要知道如何将按钮连接到脚本,我希望它只是我所忽略的简单事物。提前致谢。

MacBook Pro 7,1
OSX 10.6.8
Xcode 3.2.6

最佳答案

尝试这个 :

- (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;
}

10-08 14:26
查看更多