openFile:withApplication:
指定したファイルを、指定したアプリケーションで開きます
- (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appNamefunc openFile(_ fullPath: String, withApplication appName: String) -> Bool
解説
指定したファイルを、指定したアプリケーションで開きます。
アプリケーションはフルパスで指定しなくてもアプリケーション名だけでいいです。
開くことができればYESを返します。
そうでなければNOを返します。
10.6からスレッドセーフです。
返り値
開けたかYES/NO
Objective-C(BOOL)
Bool
引数
アプリケーションのフルパス
Objective-C(NSString *)fullPathfullPath: String
アプリケーション名
Objective-C(NSString *)appNameappName: String
フレームワーク
ApplicationKit
クラス
NSWorkspace
使用可能
10.0
更新時のバージョン
OS X 10.10
関連記事(外部サイト)
例文
#import "SetImage.h" @implementation SetImage - (IBAction)set:(id)sender { //開けるファイル拡張子の配列 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"]; } } @end//NSWorkspace openFile:withApplication: @IBAction func function004(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:"Safari") { NSLog("OK") }else{ NSLog("NO") } }