1

我正在尝试使用 NSTask 启动另一个应用程序

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

虽然这有效,但主 gui 窗口不会出现在前面。

当使用不同的文件名重复调用时,即使只有一个应用程序实例正在运行,新文件也会加载到应用程序中

任何指针?我确实尝试SetFrontProcess过,但即使在引入延迟之后似乎也没有效果

我确实查看了 NSRunningApplication 但它似乎在 10.5 上不可用,而我需要一个适用于 10.5 和 10.6 的解决方案

4

4 回答 4

3

Don't use NSTask to launch applications. Use NSWorkspace, which has several methods (e.g. -launchApplication:) to launch and activate an application.

于 2011-02-08T01:21:49.397 回答
1

我从我的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
于 2011-02-08T13:17:15.810 回答
0

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

- (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appName
    andDeactivate:(BOOL)flag
于 2011-02-08T03:52:29.047 回答
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];
于 2011-02-08T03:53:44.933 回答