Skip to content

Commit b809d97

Browse files
committed
Inquiry file format window
New window, accessible from menu view, for test the UTI associated to a file and check if it's supported by the quicklook extension and/or by highlight.
1 parent 252f44c commit b809d97

File tree

4 files changed

+180
-179
lines changed

4 files changed

+180
-179
lines changed

Application/AppDelegate.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,48 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6565
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
6666
return false
6767
}
68+
69+
/// Get the url of the quicklook extension.
70+
func getQLAppexUrl() -> URL? {
71+
guard let base_url = Bundle.main.builtInPlugInsURL else {
72+
return nil
73+
}
74+
do {
75+
for url in try FileManager.default.contentsOfDirectory(at: base_url, includingPropertiesForKeys: nil, options: []) {
76+
// Suppose only one appex on the plugin dir.
77+
if url.pathExtension == "appex" {
78+
return url
79+
}
80+
}
81+
} catch {
82+
return nil
83+
}
84+
return nil
85+
}
86+
87+
/// Get all handled UTIs.
88+
func fetchHandledUTIs() -> [UTIDesc] {
89+
// Get the list of all uti supported by the quicklook extension.
90+
guard let url = getQLAppexUrl(), let bundle = Bundle(url: url), let extensionInfo = bundle.object(forInfoDictionaryKey: "NSExtension") as? [String: Any], let attributes = extensionInfo["NSExtensionAttributes"] as? [String: Any], let supportedTypes = attributes["QLSupportedContentTypes"] as? [String] else {
91+
return []
92+
}
93+
94+
var fileTypes: [UTIDesc] = []
95+
for type in supportedTypes {
96+
let uti = UTIDesc(UTI: type)
97+
if uti.isValid {
98+
fileTypes.append(uti)
99+
} else {
100+
print("Ignoring \(type) uti.")
101+
}
102+
}
103+
104+
// Sort alphabetically.
105+
fileTypes.sort { (a, b) -> Bool in
106+
return a.description < b.description
107+
}
108+
109+
return fileTypes
110+
}
68111
}
69112

0 commit comments

Comments
 (0)