Cocoa API解説(macOS/iOS)

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。

目次

openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:

INDEX>AppKit>NSWorkspace

指定したファイル(複数)を指定したアプリケーションで開く

Objective-C

- (BOOL)openURLs:(NSArray *)urls
withAppBundleIdentifier:(NSString *)bundleIdentifier
         options:(NSWorkspaceLaunchOptions)options
additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
launchIdentifiers:(NSArray **)identifiers

Swift

func openURLs(_ urls: [AnyObject],
       withAppBundleIdentifier bundleIdentifier: String?,
                       options options: NSWorkspaceLaunchOptions,
additionalEventParamDescriptor descriptor: NSAppleEventDescriptor?,
             launchIdentifiers identifiers: AutoreleasingUnsafeMutablePointer<NSArray?>) -> Bool

解説

指定したファイル(複数)を指定したアプリケーションで開く
NSWorkspaceLaunchOptions
NSWorkspaceLaunchAndPrint = 0x00000002,
NSWorkspaceLaunchInhibitingBackgroundOnly = 0x00000080,
NSWorkspaceLaunchWithoutAddingToRecents = 0x00000100,
NSWorkspaceLaunchWithoutActivation = 0x00000200,
NSWorkspaceLaunchAsync = 0x00010000,
NSWorkspaceLaunchAllowingClassicStartup = 0x00020000,
NSWorkspaceLaunchPreferringClassic = 0x00040000,
NSWorkspaceLaunchNewInstance = 0x00080000,
NSWorkspaceLaunchAndHide = 0x00100000,
NSWorkspaceLaunchAndHideOthers = 0x00200000,
// NSWorkspaceLaunchAndDisplayFailures
NSWorkspaceLaunchDefault = NSWorkspaceLaunchAsync |
NSWorkspaceLaunchAllowingClassicStartup

返り値

Objective-C

BOOL

Swift

Bool

メニュー

引数

URL

Objective-C

openURLs:(NSArray *)urls

Swift

_ urls: [AnyObject]

バンドルフォルダ
Objective-C

(NSString *)bundleIdentifier

Swift

bundleIdentifier: String?

オプション

Objective-C

(NSWorkspaceLaunchOptions)options

Swift

options: NSWorkspaceLaunchOptions

アップルイベントデスクリプタ
Objective-C

(NSAppleEventDescriptor *)descriptor

Swift

descriptor: NSAppleEventDescriptor?

Objective-C

(NSArray **)identifiers

Swift

identifiers: AutoreleasingUnsafeMutablePointer<NSArray?>)

フレームワーク

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];

    NSArray *retID = [NSArray array];
    NSAppleEventDescriptor* targetDesc2 = [NSAppleEventDescriptor nullDescriptor];
    



    [[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObjects:[NSURL URLWithString:@"file:///Applications/iCal.app"],
																		[NSURL URLWithString:@"file:///Applications/Chess.app"],
																		nil] 
                                            withAppBundleIdentifier:@"com.apple.Finder"
                                            options:NSWorkspaceLaunchDefault
                                            additionalEventParamDescriptor:targetDesc2
                                            launchIdentifiers: &retID
                                            ];
                                            
                                            
    NSLog(@"%@",[retID description]);
}

@end


Swift

    //NSWorkspace openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:
    //launchIdentifiers の戻し方が不明
    @IBAction func function037(sender: AnyObject) {
        //複製するファイルを選択
        var openPanel = NSOpenPanel()
        openPanel.allowsMultipleSelection = true
        openPanel.canChooseDirectories = true
        openPanel.canCreateDirectories = false
        openPanel.canChooseFiles = true
        openPanel.beginWithCompletionHandler { (result) -> Void in
            //オープンパネルでOKを選択したら
            if result == NSFileHandlingPanelOKButton {
                //選択したファイルを複製
                let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
                var asDescriptor : NSAppleEventDescriptor? = NSAppleEventDescriptor.nullDescriptor()
                
                var retID : [AnyObject?] = []
                var anError: NSError?
                let options: NSWorkspaceLaunchOptions = .Async
                
                var result : Bool =
                theWorkspace.openURLs(openPanel.URLs,
                    withAppBundleIdentifier: "com.apple.Finder",
                    options: options,
                    additionalEventParamDescriptor: asDescriptor ,
                    launchIdentifiers: nil )
             
                if result {
                    NSLog("OK ")
                }
            
            }//if result
        }//openPanel.beginWithCompletionHandler
        
    }