Skip to content

Instantly share code, notes, and snippets.

@chrispaynter
Last active February 22, 2021 21:54
Show Gist options
  • Save chrispaynter/12b4cb7bb32c73033f07c0612810ce8e to your computer and use it in GitHub Desktop.
Save chrispaynter/12b4cb7bb32c73033f07c0612810ce8e to your computer and use it in GitHub Desktop.
Medium - Bookmark user chosen directories in macOS sandbox
import Cocoa
import SwiftUI
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
let bookMarkKey = "chosenDirBookmarkKey";
func applicationDidFinishLaunching(_ aNotification: Notification) {
if let bookMarkData = UserDefaults.standard.data(forKey: bookMarkKey) {
do {
var isStale = false;
let bookMarkUrl = try URL(resolvingBookmarkData: bookMarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale);
if(!isStale) {
if(bookMarkUrl.startAccessingSecurityScopedResource()) {
enumerateFileAndDirectories(url: bookMarkUrl)
bookMarkUrl.stopAccessingSecurityScopedResource()
} else {
// Cannot access security scoped resource.
}
} else {
// The bookmark is stale
}
} catch {
// Error creating a URL from the bookmark
}
} else {
// The bookmark does not yet existing UserDefaults
askUserForDirectory()
}
}
func askUserForDirectory() {
let panel = NSOpenPanel();
panel.canChooseDirectories = true;
panel.canChooseFiles = false;
panel.canCreateDirectories = false;
panel.allowsMultipleSelection = false;
panel.begin(completionHandler: { result in
if(result == .OK) {
do {
let bookmarkData = try panel.url!.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
UserDefaults.standard.set(bookmarkData, forKey: self.bookMarkKey);
} catch {
// Error creating the bookmark
}
self.enumerateFileAndDirectories(url: panel.url!)
} else {
// Choosing a directory was not successful
}
})
}
func enumerateFileAndDirectories(url: URL) {
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey, .isDirectoryKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
for case let fileURL as URL in enumerator {
print(fileURL);
}
} else {
// There was an error creating an enumerator
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
func askUserForDirectory() {
let panel = NSOpenPanel();
panel.canChooseDirectories = true;
panel.canChooseFiles = false;
panel.canCreateDirectories = false;
panel.allowsMultipleSelection = false;
panel.begin(completionHandler: { result in
if(result == .OK) {
self.enumerateFileAndDirectories(url: panel.url!)
} else {
// Choosing a directory was not successful
}
})
}
// ....askUserForDirectory()
panel.begin(completionHandler: { result in
if(result == .OK) {
do {
let bookmarkData = try panel.url!.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
UserDefaults.standard.set(bookmarkData, forKey: self.bookMarkKey);
} catch {
// Error creating the bookmark
}
self.enumerateFileAndDirectories(url: panel.url!)
} else {
// Choosing a directory was not successful
}
})
func applicationDidFinishLaunching(_ aNotification: Notification) {
if let bookMarkData = UserDefaults.standard.data(forKey: bookMarkKey) {
do {
var isStale = false;
let bookMarkUrl = try URL(resolvingBookmarkData: bookMarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale);
if(!isStale) {
if(bookMarkUrl.startAccessingSecurityScopedResource()) {
enumerateFileAndDirectories(url: bookMarkUrl)
bookMarkUrl.stopAccessingSecurityScopedResource()
} else {
// Cannot access security scoped resource.
}
} else {
// The bookmark is stale
}
} catch {
// Error creating a URL from the bookmark
}
} else {
// The bookmark does not yet existing UserDefaults
askUserForDirectory()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment