Last active
May 15, 2018 02:57
-
-
Save seanbehan/0e894d9e3e926e2d082c2a03628d8eee to your computer and use it in GitHub Desktop.
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
guard let window = view.window else { print("Nope") ; return } | |
let panel = NSOpenPanel() | |
panel.canChooseFiles = false | |
panel.canChooseDirectories = true | |
panel.allowsMultipleSelection = false | |
panel.canCreateDirectories = true | |
panel.beginSheetModal(for: window) { (result) in | |
if result == NSFileHandlingPanelOKButton { | |
self.folderPath = panel.urls[0] | |
} | |
} | |
var isStale: Bool = false | |
let defaults:UserDefaults = UserDefaults.standard | |
var folderPath: URL? { | |
didSet { | |
do { | |
let bookmark = try folderPath?.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil) | |
defaults.set(bookmark, forKey: "bookmark") | |
} catch let error as NSError { | |
print("Set Bookmark Fails: \(error.description)") | |
} | |
} | |
} | |
if let bookmarkData = defaults.object(forKey: "bookmark") as? NSData { | |
do { | |
let url = try? URL(resolvingBookmarkData: bookmarkData as Data, bookmarkDataIsStale: &isStale) | |
url??.startAccessingSecurityScopedResource() | |
} catch let error as NSError { | |
print("Bookmark Access Fails: \(error.description)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment