Hi,
As you have worked out, NSOpenPanel & NSSavePanel open file dialogs - not really Finder windows, but they open the dialogs that allow you to select files for opening or saving. And you have worked out how to limit the selections to certain file types.
Your code for filtering out various file types from a selected folder is fine, but if you need to allow the user to select a folder and then list all the matching files in it, you might want to use a directory enumerator instead of contentsOfDirectory
.
You could try something like this:
let fileManager = FileManager.default
let validFileExtensions = ["mp3", "aax", "m4b", "m4a"]
let dirEnum = fileManager.enumerator(at: folder,
includingPropertiesForKeys: [],
options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants])
while let url = dirEnum?.nextObject() as? URL {
if validFileExtensions.contains(url.pathExtension) {
print(url.lastPathComponent)
}
}
And one last thing… if you want to open a new Finder window at a folder, you can use this:
NSWorkspace.shared.open(selectedFolderUrl)
I hope this answers your questions, but please get back to me if there is anything that is still unclear.
Sarah