使用命令行启动Path Finder应用程序时,我使用open -a Path Finder.app /Users/
。
基于这个想法,我使用以下代码来启动Path Finder。
我可以在不使用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];
答案 0 :(得分:26)
if(![[NSWorkspace sharedWorkspace] launchApplication:@"Path Finder"])
NSLog(@"Path Finder failed to launch");
使用参数:
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传递参数:
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];
答案 1 :(得分:3)
根据yuji在different posting中的回答,NSWorkspace是要使用的工具,我只能用两行代码得到相同的结果。
openFile
可用于将参数传递给Path Finder
,[[NSWorkspace sharedWorkspace] openFile:string2 withApplication:@"Path Finder"];
[[NSApplication sharedApplication] terminate:nil];
通常是目录,而不是文件。但是,它工作正常。
{{1}}