我试图弄清楚如何通过拖放文件或文件夹来在 OSX 上启动 Swift 应用程序,并让它将拖放资源的完整路径视为参数。
首先,在“项目导航器”(根节点)中选择您的项目,然后转到“信息”选项卡以声明您的应用程序支持的文件类型。它可以窄到“仅 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
等。有关更多信息,请参见统一类型标识符概述 and 系统声明的统一类型标识符.
要找出文件或文件夹的 UTI 层次结构,请使用mdls
:
mdls -name kMDItemContentTypeTree /path/to/file_or_folder
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)