LMMgr // Change the default external data editor.

- Use macOS built-in TextEdit instead.
This commit is contained in:
ShikiSuen 2022-12-04 15:28:12 +08:00
parent e499437f2c
commit 291b4c9237
1 changed files with 33 additions and 2 deletions

View File

@ -640,10 +640,41 @@ public enum LMMgr {
return true
}
public static func openPhraseFile(fromURL url: URL) {
///
/// - Remark: App Sandbox app "vim" Sandbox
/// - Parameters:
/// - url: URL
/// - app: App binary
public static func openPhraseFile(fromURL url: URL, app: String = "") {
if !Self.checkIfUserFilesExistBeforeOpening() { return }
DispatchQueue.main.async {
NSWorkspace.shared.openFile(url.path, withApplication: "vChewingPhraseEditor")
switch app {
case "vim":
let process = Process()
let pipe = Pipe()
process.executableURL = URL(fileURLWithPath: "/bin/sh/")
process.arguments = ["-c", "open '/usr/bin/vim'", "'\(url.path)'"]
process.standardOutput = pipe
process.standardError = pipe
process.terminationHandler = { process in
vCLog("\ndidFinish: \(!process.isRunning)")
}
let fileHandle = pipe.fileHandleForReading
do {
try process.run()
} catch {
NSWorkspace.shared.openFile(url.path, withApplication: "TextEdit")
}
if let outStr = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) {
vCLog(outStr)
}
case "Finder":
NSWorkspace.shared.activateFileViewerSelecting([url])
default:
if !NSWorkspace.shared.openFile(url.path, withApplication: app) {
NSWorkspace.shared.openFile(url.path, withApplication: "TextEdit")
}
}
}
}