forked from onmyway133/FinderGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionWorker.swift
105 lines (83 loc) · 2.36 KB
/
ExtensionWorker.swift
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// ExtensionWorker.swift
// FinderGo
//
// Created by Khoa Pham on 18/03/2017.
// Copyright © 2017 Fantageek. All rights reserved.
//
import Cocoa
struct ExtensionWorker {
let fileName: String
let path: String
init(path: String, fileName: String) {
self.path = path
self.fileName = fileName
}
var bundle: String {
return Bundle(for: FinderSync.self).bundleIdentifier!
}
var scriptPath: URL? {
return try? FileManager.default.url(for: .applicationScriptsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
}
func fileScriptPath(fileName: String) -> URL? {
return scriptPath?
.appendingPathComponent(fileName)
.appendingPathExtension("scpt")
}
func run() {
guard let filePath = fileScriptPath(fileName: fileName) else {
return
}
guard FileManager.default.fileExists(atPath: filePath.path) else {
openPanel()
return
}
guard let script = try? NSUserAppleScriptTask(url: filePath) else {
return
}
script.execute(completionHandler: nil)
}
func openPanel() {
let panel = NSOpenPanel()
panel.directoryURL = scriptPath
panel.canChooseDirectories = true
panel.canChooseFiles = false
panel.prompt = "Select Script Folder"
panel.message = "Please select the User > Library > Application Scripts > \(bundle) folder"
panel.begin { result in
guard result.rawValue == NSFileHandlingPanelOKButton,
panel.url == self.scriptPath else {
self.alert(message: "Script folder was not selected")
return
}
let result = self.copy()
if result {
self.alert(message: "Done")
} else {
self.alert(message: "Fail")
}
}
}
func alert(message: String) {
let alert = NSAlert()
alert.messageText = "🐢 Finder Go"
alert.informativeText = message
alert.addButton(withTitle: "OK")
alert.runModal()
}
func copy() -> Bool {
let fileNames = ["terminal", "iterm", "hyper"]
for fileName in fileNames {
guard let path = Bundle(for: FinderSync.self).url(forResource: fileName, withExtension: "scpt"),
let destinationPath = fileScriptPath(fileName: fileName) else {
return false
}
do {
try FileManager.default.copyItem(at: path, to: destinationPath)
} catch {
return false
}
}
return true
}
}