使用Objective-C / Cocoa启动Mac应用程序

时间:2022-09-07 20:13:08

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.

使用命令行启动Path Finder应用程序时,我使用open -a Path Finder.app / Users /。基于这个想法,我使用以下代码来启动Path Finder。

Can I have launch app without using open command line?

我可以在不使用开放命令行的情况下启动app吗?

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];

2 个解决方案

#1


26  

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

With Parameters:

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

You could also use NSTask to pass arguments:

您还可以使用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];

#2


3  

Based on yuji's answer in different posting, NSWorkspace is the tool to use, and I could get the same result with only two lines of code.

基于yuji在不同帖子中的答案,NSWorkspace是使用的工具,我只用两行代码就能得到相同的结果。

The openFile can be used for passing the parameter to Path Finder, which is normally the directory, not a file. However, it works fine.

openFile可用于将参数传递给Path Finder,它通常是目录,而不是文件。但是,它工作正常。

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

#1


26  

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

With Parameters:

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

You could also use NSTask to pass arguments:

您还可以使用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];

#2


3  

Based on yuji's answer in different posting, NSWorkspace is the tool to use, and I could get the same result with only two lines of code.

基于yuji在不同帖子中的答案,NSWorkspace是使用的工具,我只用两行代码就能得到相同的结果。

The openFile can be used for passing the parameter to Path Finder, which is normally the directory, not a file. However, it works fine.

openFile可用于将参数传递给Path Finder,它通常是目录,而不是文件。但是,它工作正常。

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