forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
70 lines (64 loc) · 3.18 KB
/
Copy pathViewController.swift
File metadata and controls
70 lines (64 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Cocoa
import SafariServices.SFSafariApplication
class ViewController: NSViewController {
@IBOutlet var appNameLabel: NSTextField!
@IBOutlet var saveLocationLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
self.appNameLabel.stringValue = "Userscripts - Version \(appVersion)";
// seems like a lot of work to id path for another target's documents directory
let hostID = Bundle.main.bundleIdentifier!
let extensionID = "com.userscripts.macos.Userscripts-Extension"
let documentsDirectory = getDocumentsDirectory().appendingPathComponent("scripts").absoluteString
let location = documentsDirectory.replacingOccurrences(of: hostID, with: extensionID)
// check if bookmark data exists
guard
let sharedBookmark = UserDefaults(suiteName: SharedDefaults.suiteName)?.data(forKey: SharedDefaults.keyName),
let url = readBookmark(data: sharedBookmark, isSecure: false),
directoryExists(path: url.path)
else {
// sharedBookmark removed, or in trash
// renamed directories retain association
// moved directories retain association
UserDefaults(suiteName: SharedDefaults.suiteName)?.removeObject(forKey: SharedDefaults.keyName)
NSLog("removed sharedbookmark because it is non-existent, permanently deleted or exists in trash")
self.saveLocationLabel.stringValue = location
return
}
self.saveLocationLabel.stringValue = url.absoluteString
}
@IBAction func changeSaveLocation(_ sender: NSButton) {
guard let window = self.view.window else { return }
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = true
panel.canCreateDirectories = true
panel.canChooseFiles = false
panel.beginSheetModal(for: window, completionHandler: { response in
if let url: URL = panel.urls.first {
// check it is a writeable path
let canWrite = FileManager.default.isWritableFile(atPath: url.path)
if !canWrite {
// display error message
let alert = NSAlert()
alert.messageText = "Can not write to path. Choose a different path."
alert.runModal()
} else {
if !saveBookmark(url: url, isShared: true, keyName: SharedDefaults.keyName, isSecure: false) {
err("couldn't save new location from host app")
return
}
self.saveLocationLabel.stringValue = url.absoluteString
}
}
})
}
@IBAction func openSafariExtensionPreferences(_ sender: AnyObject?) {
SFSafariApplication.showPreferencesForExtension(withIdentifier: "com.userscripts.macos.Userscripts-Extension") { error in
if let _ = error {
// Insert code to inform the user that something went wrong.
}
}
}
}