2012-03-22 80 views
0

我有一個應用程序:TexturePacker。 如果我點擊應用程序文件夾中的應用程序圖標,它會啓動gui。 如果我在終端中鍵入「texturepacker」,它將運行命令行版本。如何以編程方式從命令行啓動命令行版本的應用程序?

我想以編程方式啓動命令行版本!當我使用下面的代碼時,它啓動GUI。我應該使用什麼shell命令,以便應用程序(命令行版本)啓動,就好像我在終端中輸入「texture packer」一樣。

NSTask *theProcess = [[NSTask alloc] init];  
[theProcess setLaunchPath:@"/usr/bin/open"]; 

[theProcess setArguments:[NSArray arrayWithObjects: 
           @"-a", 
           @"/Applications/TexturePacker.app", 
           nil]]; 
      // Arguments to the command: the name of the 
      // Applications directory 

      [theProcess launch]; 
      // Run the command 

      [theProcess release]; 

如果這是一個菜鳥問題。我道歉。我是noobtastic。 :S

編輯:找出它的一部分。我需要指定應用程序內部二進制文件的路徑來啓動它。但是,我如何傳遞參數呢?如果我向該數組添加更多參數,則shell假定它是「打開」命令的參數。如果我將它添加到帶紋理打包器的路徑的字符串中,shell會說應用程序未找到。 :S

+0

爲什麼你使用'在/ usr/bin中/ open'的啓動程序?要啓動這樣的命令行程序,您應該能夠將NSTask啓動路徑設置爲您的texturepacker二進制文件,然後您可以將setArguments簡單地設置爲包含texturepacker參數的數組。 – tcovo 2012-03-22 14:28:10

+0

如果你真的想使用'/ usr/bin/open',那麼你可以通過添加它們到數組來傳遞紋理包裝參數,但是通過「--args」與前面的參數分開(參見[open] https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/open.1.html))。但我不認爲在這種情況下「開放」是恰當的。 – tcovo 2012-03-22 14:30:30

+0

你是對的!我應該直接訪問二進制文件,然後傳遞參數。如果你回答這個問題,我會將其標記爲正確的! :D謝謝。 – 2012-03-22 15:14:50

回答

2

對於啓動可執行程序,不需要使用open。您可以設置NSTask啓動路徑您texturepacker二進制文件,然後你就可以setArguments包含的論據texturepacker數組:

NSTask *theProcess = [[NSTask alloc] init]; 

[theProcess setLaunchPath:@"/path/to/texturepacker"]; 

// Set arguments for invoking texturepacker 
[theProcess setArguments:[NSArray arrayWithObjects: 
           @"-x", 
           @"-y", 
           @"-z", 
           nil]]; 

// Run the task 
[theProcess launch]; 

[theProcess release]; 
+0

耶!你是對的! :) – 2012-03-22 16:33:19