2011-02-19 10 views
8

Al iniciar la aplicación Path Finder con línea de comando, uso open -a Path Finder.app /Users/. Basado en esta idea, utilizo el siguiente código para iniciar Path Finder.Lanzar una aplicación Mac con Objective-C/Cocoa

¿Puedo tener la aplicación de inicio sin usar la línea de comando 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]; 

Respuesta

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

Con Parámetros:

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 

También es posible usar NSTask para pasar argumentos:

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

¿Cómo se dan los parámetros? – prosseek

+1

Tendrá que utilizar launchApplicationAtURL: options: configuration: error: para hacer eso. Agregaré un ejemplo usando eso para mi publicación. – ughoavgfhw

+0

Bueno, no estoy seguro, pero en términos de brevedad, mi respuesta (utilizando openFile: withApplication :) parece mejor (más fácil de entender y más corta). – prosseek

3

Sobre la base de la respuesta de Yuji en different posting, NSWorkspace es la herramienta a utilizar, y que podría conseguir el mismo resultado con sólo dos líneas de código.

El openFile se puede utilizar para pasar el parámetro a Path Finder, que normalmente es el directorio, no un archivo. Sin embargo, funciona bien.

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

esto no funcionará es el _parameter_ no es un archivo sino un ** parámetro ** de la aplicación – Shebuka

Cuestiones relacionadas