Launch a Swift OSX app by dragging and dropping a file or folder on it(通过拖放文件或文件夹来启动SWIFT OSX应用程序)
问题描述
我正在尝试如何通过拖放文件或文件夹在OSX上启动SWIFT应用,并使其将所放资源的完整路径视为参数。
推荐答案
首先,在"项目导航器"(根节点)中选择您的项目,然后转到"信息"选项卡以声明您的应用程序支持的文件类型。它可以像仅CSV文件一样窄(";Only),也可以像任何文件和文件夹(";):一样宽(";Any File and Folder";:
接下来,在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
这篇关于通过拖放文件或文件夹来启动SWIFT OSX应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!