【发布时间】:2022-01-05 18:30:32
【问题描述】:
我试图弄清楚如何通过拖放文件或文件夹在 OSX 上启动 Swift 应用程序,并让它将删除资源的完整路径视为参数。
【问题讨论】:
标签: swift macos drag-and-drop
我试图弄清楚如何通过拖放文件或文件夹在 OSX 上启动 Swift 应用程序,并让它将删除资源的完整路径视为参数。
【问题讨论】:
标签: swift macos drag-and-drop
首先,在 Project Navigator(根节点)中选择您的项目,然后转到 Info 选项卡以声明您的应用支持的文件类型。它可以像“仅 CSV 文件”一样窄,也可以像“任何文件和文件夹”一样宽:
接下来,在您的 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 opened the file successfully, false otherwise
return true
}
}
OS X 中的文件类型由统一类型标识符 (UTI) 的层次结构决定。例如,JPEG文件的UTI为public.jpeg,它是public.image的子分支,public.data的子分支等。更多信息请参见Uniform Type Identifier Overview和System-Declared Uniform Type Identifiers。
要找出文件或文件夹的 UTI 层次结构,请使用 mdls:
mdls -name kMDItemContentTypeTree /path/to/file_or_folder
【讨论】: