本文介绍了使用 Objective-C/Cocoa 启动 Mac 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用命令行启动路径查找器应用程序时,我使用 open -a Path Finder.app/Users/
.基于这个想法,我使用下面的代码来启动Path Finder.
When launching Path Finder app with command line, I use open -a Path Finder.app /Users/
.Based on this idea, I use the following code to launch Path Finder.
我可以在不使用 open
命令行的情况下启动应用程序吗?
Can I have launch app without using open
command line?
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];
推荐答案
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 来传递参数:
You could also use NSTask to pass arguments:
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];
这篇关于使用 Objective-C/Cocoa 启动 Mac 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!