macOS/iOS API解説

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

目次

openFile:withApplication:andDeactivate:

INDEX>AppKit>NSWorkspace

指定したファイルを、指定したアプリケーションで開きます、オプションとして、アクティブ・非アクティブをセットできます。

Objective-C

- (BOOL)openFile:(NSString *)fullPath
 withApplication:(NSString *)appName
   andDeactivate:(BOOL)flag

Swift

   func openFile(_ fullPath: String,
withApplication appName: String,
  andDeactivate flag: Bool) -> Bool

解説

指定したファイルを、指定したアプリケーションで開きます。
オプションとして、アクティブ・非アクティブをセットできます。
アプリケーションはフルパスで指定しなくてもアプリケーション名だけでいいです。
appNameがnilなら、ファイルのタイプ用のデフォルトのアプリケーションが使われます。
開くことができればYESを返します。
そうでなければNOを返します。

返り値

開けたかYES/NO
Objective-C

(BOOL)

Swift

Bool

引数

フルパス
Objective-C

(NSString *)fullPath

Swift

_ fullPath: String

アプリ名
Objective-C

(NSString *)appName

Swift

appName: String

アクティブかYES/NO
Objective-C

(BOOL)flag

Swift

flag: Bool

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.0

参照

openFile:
openFile:withApplication:
application:openFile:(NSApplication)

更新時のバージョン

OS X 10.10

関連記事(外部サイト)

例文

Objective-C

//開けるファイル拡張子の配列
    NSArray      *fileTypes    = [ NSArray arrayWithObject : @"tiff" ];
    //OpenPanelを作る
    NSOpenPanel  *opPanel       = [ NSOpenPanel openPanel ];

    //OpenPanelの結果のボタン番号
    int		  opRet;
    BOOL	openResult;
     
        //OpenPanelでファイル選択   
    opRet = [ opPanel runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Pictures" //どのどのファイルを選択しておくか
                                    types : fileTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
    
        openResult = [[NSWorkspace sharedWorkspace] 
            openFile:[ opPanel filename ] 
            withApplication:@"QuickTime Player"
            andDeactivate:NO
            ];

    }

Swift

    //NSWorkspace openFile:withApplication:andDeactivate:
    @IBAction func function005(sender: AnyObject) {
        let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
        let theFilePath : NSString = NSString(string: "~/Desktop/textured.gif")
        //チルダ付きのファイルパスをフルパスに変換する
        let fileFullPath : NSString = theFilePath.stringByExpandingTildeInPath
        NSLog(fileFullPath)
        if theWorkspace.openFile(fileFullPath, withApplication:"Preview", andDeactivate: true) {
            NSLog("OK")
        }else{
            NSLog("NO")
        }
    }