2017-01-22 70 views

回答

0

首先,在Project Navigator(根節點)中選擇您的項目,並轉到Info選項卡來聲明您的應用程序支持的文件類型。它可以作爲「唯一的CSV文件」窄或寬「的任何文件和文件夾」:

enter image description here

接下來,在你的AppDelegate.swift文件中,添加application(_:openFile:)

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate { 

    func application(_ sender: NSApplication, openFile filename: String) -> Bool { 
     print("openning file \(filename)") 

     // You must determine if filename points to a file or folder 

     // Now do your things... 

     // Return true if your app openned the file successfully. 
     return true 
    } 
} 

文件類型OS X由統一類型標識符(UTI)的層次結構確定。例如,JPEG文件的UTI爲public.jpeg。它是public.image的分支,它是public.data的子分支,等等。有關更多信息,請參見Uniform Type Identifier OverviewSystem-Declared Uniform Type Identifiers

要找出一個文件的UTI層次,使用mdls

mdls -name kMDItemContentTypeTree /path/to/myFile.ext 
相關問題