Created
May 12, 2024 21:26
-
-
Save peterkos/09d6c3649336acb1bd0572650e69b9b4 to your computer and use it in GitHub Desktop.
macos_files.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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