2012-04-10 116 views
1

这似乎是我想要完成的一件简单的事情,远远少于Xcode/Interface Builder的功能。我已经搜索过,并没有拿出我的答案,但大多数搜索引导我在这里,所以我创建了一个帐户,以问专家。我想创建一个非常简单的GUI,它有四到五个按钮,每个按钮执行一个简单的shell脚本,终端窗口不是必需的,但如果事情是这样,我可以用一个开始。除了shell脚本之外,我还需要在应用程序中使用adb(Android调试桥)和fastboot实用程序。我假设我需要在资源文件夹中放置adb和fastboot以及其他文件,我还假设我需要将我的shell脚本放在Classes文件夹中。我真的只需要知道如何将按钮连接到脚本,我希望这只是我忽略的一些简单的东西。提前致谢。(Mac)创建执行shell脚本的Xcode应用程序

的MacBook Pro 7,1 OSX 10.6.8 的Xcode 3.2.6

+0

你没有得到它的权利。这不是“将按钮连接到脚本”。你必须做的是将按钮连接到你的实现文件中的一些动作,这反过来会运行你的脚本。现在,至于你的脚本的位置,最好在你的包中的某个地方,这样你就可以用'[[NSBundle mainBundle] pathForResource:@“yourscript”ofType:@“py”]'来访问它的路径。看看我的答案......“命令执行功能”。 (小心点,学习它,它是异步的)。 – 2012-04-10 02:03:00

+0

作为一个注释:YES,Xcode和Cocoa DO让开发者的生活变得轻松,恕我直言。但是,这并不意味着它是一个“推动按钮,你去那里”的东西,是吧? – 2012-04-10 02:07:15

+0

我明白,对于我来说这是一种全新的语言,当初次考虑制作应用程序时,制作“隐藏”应用程序的youtube视频“教程”确实歪曲了XCode的工作原理,确实给人一种错误的能力感。感谢您的回复,我将研究下面的代码并尝试理解它。 – 2012-04-10 02:14:13

回答

8

试试这个:

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