2011-02-19 1 views
8

명령 줄을 사용하여 Path Finder 응용 프로그램을 실행할 때 open -a Path Finder.app /Users/을 사용합니다. 이 아이디어를 바탕으로 다음 코드를 사용하여 Path Finder를 시작합니다.Objective-C/Cocoa가 포함 된 Mac 응용 프로그램 시작

open 명령 줄을 사용하지 않고 앱을 시작할 수 있습니까?

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

NSArray *arguments; 
arguments = [NSArray arrayWithObjects: @"-a", @"Path Finder.app", @"/Users/", nil]; 
[task setArguments: arguments]; 

NSPipe *pipe; 
pipe = [NSPipe pipe]; 
[task setStandardOutput: pipe]; 

NSFileHandle *file; 
file = [pipe fileHandleForReading]; 

[task launch]; 

답변

24
if(![[NSWorkspace sharedWorkspace] launchApplication:@"Path Finder"]) 
    NSLog(@"Path Finder failed to launch"); 

NSTask *task = [[NSTask alloc] init]; 
NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"Path Finder"]]]; 
[task setLaunchPath:[bundle executablePath]]; 
NSArray *arguments = [NSArray arrayWithObjects:@"Argument1", @"Argument2", nil]; 
[task setArguments:arguments]; 
[task launch]; 
+0

매개 변수를 제공하는 방법? – prosseek

+1

이렇게하려면 launchApplicationAtURL : options : configuration : error :를 사용해야합니다. 나는 그것을 내 게시물에 사용하는 예제를 추가 할 것이다. – ughoavgfhw

+0

글쎄, 잘 모르겠지만, 간결함의 측면에서, 내 대답 (openFile : withApplication 사용)이 더 잘 (이해하기 쉽고 짧음) 보인다. – prosseek

3

different posting에서 유우지의 답변에 따라, NSWorkspace가 사용하는 도구이며, 나는 코드의 두 줄과 같은 결과를 얻을 수 있습니다.

openFile은 매개 변수를 Path Finder으로 전달하는 데 사용할 수 있습니다. 보통은 파일이 아니고 디렉토리입니다. 그러나, 그것은 잘 작동합니다.

NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; 
NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:@"Path Finder"]]; 
//Handle url==nil 
NSError *error = nil; 
NSArray *arguments = [NSArray arrayWithObjects:@"Argument1", @"Argument2", nil]; 
[workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:arguments forKey:NSWorkspaceLaunchConfigurationArguments] error:&error]; 
//Handle error 

또한 인수를 전달하는 NSTask를 사용할 수 있습니다 : 매개 변수

[[NSWorkspace sharedWorkspace] openFile:string2 withApplication:@"Path Finder"]; 
[[NSApplication sharedApplication] terminate:nil]; 
+0

이이 _parameter_이 파일 것이 아니라, 그것을 작동하지 않습니다 ** 매개 변수 ** 응용 프로그램의 – Shebuka

관련 문제