Skip to content

Instantly share code, notes, and snippets.

@peterkos
Created May 12, 2024 21:26
Show Gist options
  • Save peterkos/09d6c3649336acb1bd0572650e69b9b4 to your computer and use it in GitHub Desktop.
Save peterkos/09d6c3649336acb1bd0572650e69b9b4 to your computer and use it in GitHub Desktop.
macos_files.swift
struct RSFile {
var name: String
}
func importfiles(fileURLs: [URL]) async -> [RSFile] {
var files = [RSFile]()
for fileURL in fileURLs {
let fileData: URLResourceValues
do {
try fileData = fileURL.resourceValues(forKeys: [.nameKey])
} catch {
print("Import error: \(error)")
continue
}
guard let fileName = fileData.name else {
print("Unable to import file for URL \(fileURL)")
continue
}
// This shouldn't be needed with default file picker, but oh well.
_ = fileURL.startAccessingSecurityScopedResource()
defer {
fileURL.stopAccessingSecurityScopedResource()
}
let file = RSFile(name: fileName)
print("Successfully imported file: \(file.title)")
files.append(file)
}
return files
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment