私は自分のMDFoundationAdditions
とMDAppKitAdditions
カテゴリからこれらをつかみました。
このソリューションは、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