-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml2htmlhelper.swift
More file actions
86 lines (74 loc) · 2.83 KB
/
Copy pathxml2htmlhelper.swift
File metadata and controls
86 lines (74 loc) · 2.83 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
import CoreServices
let bundleID = "com.local.xml2html"
let contentType = "public.xml" as CFString
func setDefaultHandler() -> Bool {
let status = LSSetDefaultRoleHandlerForContentType(contentType, .all, bundleID as CFString)
return status == noErr
}
func dequarantineOnce() {
let home = FileManager.default.homeDirectoryForCurrentUser
let downloads = home.appendingPathComponent("Downloads")
guard let files = try? FileManager.default.contentsOfDirectory(
at: downloads, includingPropertiesForKeys: nil
) else { return }
for file in files where file.pathExtension.lowercased() == "xml" {
removexattr(file.path, "com.apple.quarantine", 0)
}
}
func installDequarantineWatcher() -> Bool {
let home = FileManager.default.homeDirectoryForCurrentUser
let downloads = home.appendingPathComponent("Downloads").path
let launchAgentsDir = home.appendingPathComponent("Library/LaunchAgents")
let label = "com.local.xml2html.dequarantine"
let plistPath = launchAgentsDir.appendingPathComponent("\(label).plist")
let selfPath = CommandLine.arguments[0]
let absHelperPath = selfPath.hasPrefix("/") ? selfPath : FileManager.default.currentDirectoryPath + "/" + selfPath
do {
try FileManager.default.createDirectory(at: launchAgentsDir, withIntermediateDirectories: true)
let plist: [String: Any] = [
"Label": label,
"ProgramArguments": [absHelperPath, "dequarantine-once"],
"WatchPaths": [downloads],
"StartInterval": 20,
"RunAtLoad": true
]
let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
try data.write(to: plistPath)
let unload = Process()
unload.executableURL = URL(fileURLWithPath: "/bin/launchctl")
unload.arguments = ["unload", plistPath.path]
try? unload.run()
unload.waitUntilExit()
let load = Process()
load.executableURL = URL(fileURLWithPath: "/bin/launchctl")
load.arguments = ["load", plistPath.path]
try load.run()
load.waitUntilExit()
return load.terminationStatus == 0
} catch {
print("Ошибка установки watcher: \(error)")
return false
}
}
let args = CommandLine.arguments
guard args.count >= 2 else {
print("Использование: xml2htmlhelper <set-default|install-watcher|both>")
exit(1)
}
switch args[1] {
case "set-default":
exit(setDefaultHandler() ? 0 : 1)
case "install-watcher":
exit(installDequarantineWatcher() ? 0 : 1)
case "dequarantine-once":
dequarantineOnce()
exit(0)
case "both":
let a = setDefaultHandler()
let b = installDequarantineWatcher()
exit((a && b) ? 0 : 1)
default:
print("Неизвестная команда: \(args[1])")
exit(1)
}