2011-02-08 62 views
1

我试图推出使用NSTask启动一个NSTask,并把它前面

NSArray* argArray = [NSArray arrayWithObjects:fileName, nil]; 
NSTask* task = [NSTask launchedTaskWithLaunchPath:appName arguments:argArray]; 

另一个应用程序,而这个工程的主要GUI窗口不来的前面。

时不同的文件名的新文件,并在应用中得到加载一边喊即使只有1应用的实例运行

任何poiners?我也尝试SetFrontProcess但似乎有甚至引入的延迟

我没有考虑NSRunningApplication但现在看来,这是不提供10.5,而我需要两个10.5和10.6

回答

3

不要一个解决方案后没有效果使用NSTask来启动应用程序。使用NSWorkspace,它有几种方法(例如-launchApplication:)来启动和激活应用程序。

+0

有使用NSWorkspace一个问题,我想传递一个文件名作为NSWorkspace函数的参数,如果应用程序已经运行意味着新的实例不会创建参数部分会被忽略这使得它在这种情况下无法使用 – user549164 2011-02-08 01:47:20

+0

那么在这种情况下,可能使用NSTask启动应用程序,然后立即使用其中一种NSWorkspace启动方法,因为NSWorkspace方法会在应用程序已经运行的情况下将它们带到前端。 – indragie 2011-02-08 03:30:25

+0

@ user549164:您可以提供`NSWorkspaceLaunchNewInstance`来始终启动一个新实例(如果适用的话)。 – 2013-05-18 15:11:48

0

如果您要启动的任务是一个正确的应用程序,你可以使用NSWorkspace的

- (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appName 
    andDeactivate:(BOOL)flag 
0

要扩大indragie的答案,如果你想用一个文件参数推出了新的实例,这样做(未经测试):

NSDictionary *config = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSArray arrayWithObject:filePath], NSWorkspaceLaunchConfigurationArguments, 
         nil]; 
NSError *error = nil; 
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:yourAppURL options:NSWorkspaceLaunchNewInstance | NSWorkspaceLaunchDefault configuration:config error:&error] 

在10.5,你可以试试(未测试):

NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 
[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:fileURL] withAppBundleIdentifier:@"com.foo.someapp" options:NSWorkspaceLaunchNewInstance | NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifiers:nil]; 
1

我GRA从我的MDFoundationAdditionsMDAppKitAdditions类别中剔除这些。

这个解决方案应该为Mac OS X 10.4.X及更高的工作(这是LSOpenApplication()被引入时):

MDAppKitAdditions.h:

#import <Cocoa/Cocoa.h> 
#import "MDFoundationAdditions.h" 

@interface NSWorkspace (MDAdditions) 
- (BOOL)launchApplicationAtPath:(NSString *)path 
      arguments:(NSArray *)argv 
      error:(NSError **)error; 
@end 

MDAppKitAdditions.m:

#import "MDAppKitAdditions.h" 
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 
#include <ApplicationServices/ApplicationServices.h> 
#elif MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 
#include <CoreServices/CoreServices.h> 
#endif 

@implementation NSWorkspace (MDAdditions) 

- (BOOL)launchApplicationAtPath:(NSString *)path arguments:(NSArray *)argv 
      error:(NSError **)error { 
    BOOL success = YES; 
     if (error) *error = nil; 

     if (path) { 
      FSRef itemRef; 
      if ([path getFSRef:&itemRef error:error]) { 
       LSApplicationParameters appParameters = 
        {0, kLSLaunchDefaults, &itemRef, NULL, NULL, 
       (argv ? (CFArrayRef)argv : NULL), NULL }; 

       OSStatus status = noErr; 
       status = LSOpenApplication(&appParameters, NULL); 

       if (status != noErr) { 
        success = NO; 
        NSLog(@"[%@ %@] LSOpenApplication() returned %hi for %@", 
         NSStringFromClass([self class]), 
         NSStringFromSelector(_cmd), status, path); 
        if (error) *error = 
     [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]; 
      } 
     } 
    } 
    return success; 
} 
@end 

MDFoundationAdditions.h:

#import <Foundation/Foundation.h> 
#import <CoreServices/CoreServices.h> 

@interface NSString (MDAdditions) 
- (BOOL)getFSRef:(FSRef *)anFSRef error:(NSError **)anError; 
@end 

MDFoundationAdditions.h:

#import "MDFoundationAdditions.h" 
#import <sys/syslimits.h> 

@implementation NSString (MDAdditions) 

- (BOOL)getFSRef:(FSRef *)anFSRef error:(NSError **)anError { 
    if (anError) *anError = nil; 
    OSStatus status = noErr; 
    status = FSPathMakeRef((const UInt8 *)[self UTF8String], anFSRef, NULL); 
    if (status != noErr) { 
     if (anError) 
    *anError = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]; 
    } 
    return (status == noErr); 
} 
@end 
相关问题