macOS/iOS API解説

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

目次

launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier:

INDEX>AppKit>NSWorkspace

バンドルIDで指定したアプリケーションを起動します

Objective-C

- (BOOL)launchAppWithBundleIdentifier:(NSString *)bundleIdentifier
                              options:(NSWorkspaceLaunchOptions)options
       additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
                     launchIdentifier:(NSNumber **)identifier

Swift

func launchAppWithBundleIdentifier(_ bundleIdentifier: String,
                           options options: NSWorkspaceLaunchOptions,
    additionalEventParamDescriptor descriptor: NSAppleEventDescriptor?,
                  launchIdentifier identifier: AutoreleasingUnsafeMutablePointer<NSNumber?>) -> Bool

解説

バンドルIDで指定したアプリケーションを起動します。

返り値

Objective-C

- (BOOL)launchAppWithBundleIdentifier:(NSString *)bundleIdentifier
                              options:(NSWorkspaceLaunchOptions)options
       additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
                     launchIdentifier:(NSNumber **)identifier

Swift

func launchAppWithBundleIdentifier(_ bundleIdentifier: String,
                           options options: NSWorkspaceLaunchOptions,
    additionalEventParamDescriptor descriptor: NSAppleEventDescriptor?,
                  launchIdentifier identifier: AutoreleasingUnsafeMutablePointer<NSNumber?>) -> Bool

メニュー

引数

バンドルID
@"com.apple.iCal"などの形式

Objective-C

(NSString *)bundleIdentifier

Swift

_ bundleIdentifier: String

起動オプション
Objective-C
NSWorkspaceLaunchOptions 】起動オプション
●NSWorkspaceLaunchAndPrint 開いて印刷する
●NSWorkspaceLaunchInhibitingBackgroundOnly バックグラウンドで開く
●NSWorkspaceLaunchWithoutAddingToRecents
●NSWorkspaceLaunchWithoutActivation アプリケーションを開くが前面には持ってこない
●NSWorkspaceLaunchAsync 非同期で起動する
●NSWorkspaceLaunchAllowingClassicStartup クラシック環境で起動する
●NSWorkspaceLaunchPreferringClassic クラシック環境で起動する
●NSWorkspaceLaunchNewInstance 先にアプリケーションがあったらもうひとつインスタンスを作って起動する
●NSWorkspaceLaunchAndHide 起動してすぐに隠す
●NSWorkspaceLaunchAndHideOthers 起動して他のアプリケーションを隠す
●NSWorkspaceLaunchDefault = NSWorkspaceLaunchAsync |
NSWorkspaceLaunchAllowingClassicStartup

(NSWorkspaceLaunchOptions)options

Swift
NSWorkspaceLaunchOptions 】起動オプション
NSWorkspaceLaunchOptions
  .AndPrint
  .WithErrorPresentation
  .InhibitingBackgroundOnly
  .WithoutAddingToRecents
  .WithoutActivation
  .Async
  .AllowingClassicStartup
  .PreferringClassic
  .NewInstance
  .AndHide
  .AndHideOthers
  .Default

options: NSWorkspaceLaunchOptions

起動するアプリケーションに渡されるアップルイベントデスクリプタ
開く文書を指定する時などに渡す。
Objective-C

(NSAppleEventDescriptor *)descriptor

Swift

descriptor: NSAppleEventDescriptor?

起動IDが返される
Objective-C

(NSNumber **)identifier

Swift

identifier: AutoreleasingUnsafeMutablePointer<NSNumber?>

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.3

参照

cocoaapi.hatenablog.com

更新時のバージョン

OS X 10.10.3
Swift1.2


例文

#import "SetImage.h"

@implementation SetImage

- (IBAction)set:(id)sender
{
    [NSWorkspace sharedWorkspace];
     NSNumber *num = [[NSNumber alloc] init] ;

    NSAppleEventDescriptor* targetDesc2 = [NSAppleEventDescriptor nullDescriptor];
   
    
    [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.oomori.monzai" //@"com.apple.iCal"
                                            options:NSWorkspaceLaunchAsync
                                            additionalEventParamDescriptor:targetDesc2
                                            launchIdentifier: &num
                                            ];
    NSLog(@"%@",[[num longValue] description]);
}


@end

Swift
指定のアプリを起動することはできるが、パラメータを渡すことが出来ない。調査中です。

    //NSWorkspace launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier:
    //指定のアプリを起動することはできるが、パラメータを渡すことが出来ない
    func val4 (s: String) -> UInt32
    {
        var n: UInt32 = 0
        var r: String = ""
        if (count(s) > 4)
        {
            r = s.substringToIndex(advance(s.startIndex, 4))
        }
        else
        {
            r = s + "    "
            r = r.substringToIndex(advance(r.startIndex, 4))
        }
        for UniCodeChar in r.unicodeScalars
        {
            n = (n << 8) + (UInt32(UniCodeChar.value) & 255)
        }
        
        return (n)
    }
    @IBAction func function036(sender: AnyObject) {
        let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
        //var asDescriptor : NSAppleEventDescriptor? = NSAppleEventDescriptor.nullDescriptor()
        var urlDescriptor : NSAppleEventDescriptor? = NSAppleEventDescriptor(string: "http://www.apple.com/")

        var asDescriptor : NSAppleEventDescriptor? = NSAppleEventDescriptor.recordDescriptor()
        
        let aeKeyword : AEKeyword = val4("URL")
        asDescriptor?.setParamDescriptor(urlDescriptor!, forKeyword: aeKeyword )
        

        var launchID : NSNumber? = NSNumber()
        var result : Bool = theWorkspace.launchAppWithBundleIdentifier("com.apple.safari",
            options: NSWorkspaceLaunchOptions.Default,
            additionalEventParamDescriptor: asDescriptor,
            launchIdentifier: &launchID )
        if result {
            NSLog("launchID %d",launchID!)
            // -> launchID 311
        }
    }