Skip to content

Instantly share code, notes, and snippets.

@GregoryMaks
Created February 16, 2022 17:29
Show Gist options
  • Save GregoryMaks/69afd0ec43a9ec705cc254574f0c313c to your computer and use it in GitHub Desktop.
Save GregoryMaks/69afd0ec43a9ec705cc254574f0c313c to your computer and use it in GitHub Desktop.
File extensions for specified UTType and all its descenders
import UniformTypeIdentifiers
let allTypes: [UTType] = [
.item,
.content,
.compositeContent,
.diskImage,
.data,
.directory,
.resolvable,
.symbolicLink,
.executable,
.mountPoint,
.aliasFile,
.urlBookmarkData,
.url,
.fileURL,
.text,
.plainText,
.utf8PlainText,
.utf16ExternalPlainText,
.utf16PlainText,
.delimitedText,
.commaSeparatedText,
.tabSeparatedText,
.utf8TabSeparatedText,
.rtf,
.html,
.xml,
.yaml,
.sourceCode,
.assemblyLanguageSource,
.cSource,
.objectiveCSource,
.swiftSource,
.cPlusPlusSource,
.objectiveCPlusPlusSource,
.cHeader,
.cPlusPlusHeader,
.script,
.appleScript,
.osaScript,
.osaScriptBundle,
.javaScript,
.shellScript,
.perlScript,
.pythonScript,
.rubyScript,
.phpScript,
.makefile,
.json,
.propertyList,
.xmlPropertyList,
.binaryPropertyList,
.pdf,
.rtfd,
.flatRTFD,
.webArchive,
.image,
.jpeg,
.tiff,
.gif,
.png,
.icns,
.bmp,
.ico,
.rawImage,
.svg,
.livePhoto,
.heif,
.heic,
.webP,
.threeDContent,
.usd,
.usdz,
.realityFile,
.sceneKitScene,
.arReferenceObject,
.audiovisualContent,
.movie,
.video,
.audio,
.quickTimeMovie,
.mpeg,
.mpeg2Video,
.mpeg2TransportStream,
.mp3,
.mpeg4Movie,
.mpeg4Audio,
.appleProtectedMPEG4Audio,
.appleProtectedMPEG4Video,
.avi,
.aiff,
.wav,
.midi,
.playlist,
.m3uPlaylist,
.folder,
.volume,
.package,
.bundle,
.pluginBundle,
.spotlightImporter,
.quickLookGenerator,
.xpcService,
.framework,
.application,
.applicationBundle,
.applicationExtension,
.unixExecutable,
.exe,
.systemPreferencesPane,
.archive,
.gzip,
.bz2,
.zip,
.appleArchive,
.spreadsheet,
.presentation,
.database,
.message,
.contact,
.vCard,
.toDoItem,
.calendarEvent,
.emailMessage,
.internetLocation,
.internetShortcut,
.font,
.bookmark,
.pkcs12,
.x509Certificate,
.epub,
.log
]
func fileExtensions(for type: UTType, level: Int = 0) -> [String] {
// For debugging purpposes
//let delimiter = String(repeating: " ", count: level)
//print("\(delimiter)\(type)")
var extensions = [String]()
if let extensionsForSoleType = type.tags[.filenameExtension] {
extensions.append(contentsOf: extensionsForSoleType)
}
allTypes.forEach { subtype in
if subtype.isSubtype(of: type) {
extensions.append(contentsOf: fileExtensions(for: subtype, level: level + 1))
}
}
return extensions
}
print(fileExtensions(for: .text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment