-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathFinderSync.swift
99 lines (85 loc) · 3.04 KB
/
FinderSync.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
//
// FinderSync.swift
// openInCodeEdit
//
// Created by Wesley de Groot on 03/05/2022.
//
/**
* For anyone working on this file.
* print does not output to the console, use NSLog.
* open "console.app" to debug,
*/
import Cocoa
import FinderSync
import os.log
class CEOpenWith: FIFinderSync {
let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: "FinderSync")
override init() {
super.init()
// Add finder sync
let finderSync = FIFinderSyncController.default()
if let mountedVolumes = FileManager.default.mountedVolumeURLs(
includingResourceValuesForKeys: nil,
options: [.skipHiddenVolumes]
) {
finderSync.directoryURLs = Set<URL>(mountedVolumes)
}
// Monitor volumes
let notificationCenter = NSWorkspace.shared.notificationCenter
notificationCenter.addObserver(
forName: NSWorkspace.didMountNotification,
object: nil,
queue: .main
) { notification in
if let volumeURL = notification.userInfo?[NSWorkspace.volumeURLUserInfoKey] as? URL {
finderSync.directoryURLs.insert(volumeURL)
}
}
}
/// Open in CodeEdit (menu) action
/// - Parameter sender: sender
@objc
func openInCodeEditAction(_ sender: AnyObject?) {
guard let items = FIFinderSyncController.default().selectedItemURLs(),
let defaults = UserDefaults.init(suiteName: "app.codeedit.CodeEdit.shared") else {
return
}
// Make values compatible to ArrayLiteralElement
var files = ""
for obj in items {
files.append("\(obj.path);")
}
guard let codeEdit = NSWorkspace.shared.urlForApplication(
withBundleIdentifier: "app.codeedit.CodeEdit"
) else { return }
// Add files to open to openInCEFiles.
defaults.set(files, forKey: "openInCEFiles")
NSWorkspace.shared.open(
[],
withApplicationAt: codeEdit,
configuration: NSWorkspace.OpenConfiguration()
)
}
// MARK: - Menu and toolbar item support
override func menu(for menuKind: FIMenuKind) -> NSMenu {
guard let defaults = UserDefaults.init(suiteName: "app.codeedit.CodeEdit.shared") else {
logger.error("Unable to load defaults")
return NSMenu(title: "")
}
// Register enableOpenInCE (enable Open In CodeEdit
defaults.register(defaults: ["enableOpenInCE": true])
let menu = NSMenu(title: "")
let menuItem = NSMenuItem(
title: "Open in CodeEdit",
action: #selector(openInCodeEditAction(_:)),
keyEquivalent: ""
)
menuItem.image = NSImage.init(named: "icon")
let enableOpenInCE = defaults.bool(forKey: "enableOpenInCE")
logger.info("Enable Open In CodeEdit value is \(enableOpenInCE, privacy: .public)")
if enableOpenInCE {
menu.addItem(menuItem)
}
return menu
}
}