macOS/iOS API解説

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

目次

iconForFiles:

INDEX>AppKit>NSWorkspace

ファイルパスで複数のファイルのアイコンを返します

Objective-C

- (NSImage *)iconForFiles:(NSArray *)fullPaths

Swift

func iconForFiles(_ fullPaths: [AnyObject]) -> NSImage?

解説

ファイルパスで複数のファイルのアイコンを返します。
fullPathsが1つのファイルを指定する場合は、そのアイコンが返されます。
fullPathsが複数のファイルを指定する場合は、複数選択をしているというアイコンが返されます。

返り値

イコン画
Objective-C

NSImage *

Swift

NSImage?

引数

Objective-C

(NSArray *)fullPaths

Swift

_ fullPaths: [AnyObject]

フルパスの配列

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.0

参照

- iconForFile:
- iconForFileType:

更新時のバージョン

OS X 10.10.3
Swift1.2

例文

#import "SetImage.h"

@implementation SetImage

- (IBAction)set:(id)sender
{

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

    //OpenPanelの結果のボタン番号
    int		  opRet;

     [opPanel setAllowsMultipleSelection:YES];//複数選択可能
        //OpenPanelでファイル選択   
    opRet = [ opPanel runModalForDirectory : @"/Applications" //どこのディレクトリを出すか
                                     file : @"" //どのファイルを選択しておくか
                                    types : fileTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
    

    [image setImage:[[NSWorkspace sharedWorkspace] iconForFiles:[ opPanel filenames ]]];
    }

}

@end

Swift

    //NSWorkspace iconForFiles
    @IBAction func function031(sender: AnyObject) {
        var aWindow : NSWindow = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300, 200), styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing: .Buffered, defer: false , screen: NSScreen.mainScreen())
        windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        
        //複数ファイルを選択
        var openPanel = NSOpenPanel()
        openPanel.allowsMultipleSelection = true
        openPanel.canChooseDirectories = false
        openPanel.canCreateDirectories = false
        openPanel.canChooseFiles = true
        openPanel.beginWithCompletionHandler { (result) -> Void in
            //オープンパネルでOKを選択したら
            if result == NSFileHandlingPanelOKButton {
                //選択した複数ファイルを
                let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
                let theURLs : [AnyObject] = openPanel.URLs
                
                var theView : NSImageView = NSImageView(frame: NSMakeRect(0.0, 0.0, 100.0, 100.0))
                theView.image = theWorkspace.iconForFiles(theURLs)
                aWindow.contentView.addSubview(theView)
                aWindow.center()//ウインドウをスクリーンの中心に
                aWindow.orderFront(self)//前面に
                aWindow.makeKeyAndOrderFront(self)//表示
                
                
            }//if result
        }//openPanel.beginWithCompletionHandler
    }