macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

launchApplicationAtURL:options:configuration:error:

INDEX>AppKit>NSWorkspace

指定したオプションでアプリケーションを起動します

Objective-C

- (NSRunningApplication *)launchApplicationAtURL:(NSURL *)url
                                         options:(NSWorkspaceLaunchOptions)options
                                   configuration:(NSDictionary *)configuration
                                           error:(NSError **)error

Swift

func launchApplicationAtURL(_ url: NSURL,
                    options options: NSWorkspaceLaunchOptions,
              configuration configuration: [NSObject : AnyObject],
                      error error: NSErrorPointer) -> NSRunningApplication?

解説

指定したオプションでアプリケーションを起動します
例えば、32ビットモードで起動・64ビットモードで起動などが出来ます。

OS X 10.10の64ビットマシンで32ビットモードで起動しようとすると下記のようなメッセージが出て起動できません。
f:id:jjj777:20150427075042p:plain

現在はX86_64で落ち着いているのであまり必要とされませんが、またプロセッサのアーキテクチャが変更されるときには使うことがあるかもしれません。

返り値

Objective-C

(NSRunningApplication *)

Swift

NSRunningApplication?

引数

Objective-C

(NSURL *)url

Swift

_ url: NSURL

NSWorkspaceLaunchOptions

Objective-C

(NSWorkspaceLaunchOptions)options

Swift

options: NSWorkspaceLaunchOptions

Objective-C
NSString * const NSWorkspaceLaunchConfigurationAppleEvent;
NSString * const NSWorkspaceLaunchConfigurationArguments;
NSString * const NSWorkspaceLaunchConfigurationEnvironment;
NSString * const NSWorkspaceLaunchConfigurationArchitecture;

(NSDictionary *)configuration

Swift
let NSWorkspaceLaunchConfigurationAppleEvent: NSString!
let NSWorkspaceLaunchConfigurationArguments: NSString!
let NSWorkspaceLaunchConfigurationEnvironment: NSString!
let NSWorkspaceLaunchConfigurationArchitecture: NSString!

configuration: [NSObject : AnyObject]

Objective-C

(NSError **)error

Swift

error: NSErrorPointer

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.6-

更新時のバージョン

OS X 10.10.3
Swift1.2

参照

cocoaapi.hatenablog.com

関連記事(外部サイト)

stackoverflow.com

例文

    //NSWorkspace launchApplicationAtURL:options:configuration:error:
    @IBAction func function009(sender: AnyObject) {
        let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
        var anError: NSError?
        let andPrint = NSWorkspaceLaunchOptions.AndPrint.rawValue
        let mask = Int( andPrint ) // cast from UInt
        
        let theURL : NSURL = NSURL(fileURLWithPath: "/Applications/iTunes.app")!
        let theOption : NSNumber = NSNumber(integer: 0)
        //32ビットモードで起動
        let theArc : NSNumber = NSNumber(integer: NSBundleExecutableArchitectureI386)
        //64ビットモードで起動
        //let theArc : NSNumber = NSNumber(integer: NSBundleExecutableArchitectureX86_64)
        
        let theConfig : [NSObject : AnyObject] = [NSWorkspaceLaunchConfigurationArchitecture : theArc]
        if (theWorkspace.launchApplicationAtURL(theURL,
                options: NSWorkspaceLaunchOptions.Async,
                configuration: theConfig,
                error: &anError) != nil){
            NSLog("YES")
        }else{
            NSLog("NO")
        }

        

    }