2012-04-10 113 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; 
}