Repo // Implementing FileOpenMethod.
This commit is contained in:
parent
c0e1eb449d
commit
a1d9f502c1
|
@ -134,10 +134,7 @@ public extension AppDelegate {
|
||||||
NSApp.popup()
|
NSApp.popup()
|
||||||
guard result == NSApplication.ModalResponse.alertFirstButtonReturn else { return }
|
guard result == NSApplication.ModalResponse.alertFirstButtonReturn else { return }
|
||||||
let url = URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: true))
|
let url = URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: true))
|
||||||
guard let finderURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.finder") else { return }
|
FileOpenMethod.finder.open(url: url)
|
||||||
let configuration = NSWorkspace.OpenConfiguration()
|
|
||||||
configuration.promptsUserIfNeeded = true
|
|
||||||
NSWorkspace.shared.open([url], withApplicationAt: finderURL, configuration: configuration)
|
|
||||||
Uninstaller.uninstall(
|
Uninstaller.uninstall(
|
||||||
isSudo: false, selfKill: true, defaultDataFolderPath: LMMgr.dataFolderPath(isDefaultFolder: true)
|
isSudo: false, selfKill: true, defaultDataFolderPath: LMMgr.dataFolderPath(isDefaultFolder: true)
|
||||||
)
|
)
|
||||||
|
|
|
@ -17,8 +17,8 @@ import Shared
|
||||||
extension LMMgr: PhraseEditorDelegate {
|
extension LMMgr: PhraseEditorDelegate {
|
||||||
public var currentInputMode: Shared.InputMode { IMEApp.currentInputMode }
|
public var currentInputMode: Shared.InputMode { IMEApp.currentInputMode }
|
||||||
|
|
||||||
public func openPhraseFile(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, appIdentifier: String) {
|
public func openPhraseFile(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, using app: FileOpenMethod) {
|
||||||
Self.openPhraseFile(fromURL: Self.userDictDataURL(mode: mode, type: type), appIdentifier: appIdentifier)
|
Self.openPhraseFile(fromURL: Self.userDictDataURL(mode: mode, type: type), using: app)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool) {
|
public func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool) {
|
||||||
|
|
|
@ -273,30 +273,20 @@ public extension LMMgr {
|
||||||
}
|
}
|
||||||
|
|
||||||
static func openUserDictFile(type: vChewingLM.ReplacableUserDataType, dual: Bool = false, alt: Bool) {
|
static func openUserDictFile(type: vChewingLM.ReplacableUserDataType, dual: Bool = false, alt: Bool) {
|
||||||
let appIdentifier: String = alt ? "com.apple.TextEdit" : "com.apple.finder"
|
let app: FileOpenMethod = alt ? .textEdit : .finder
|
||||||
openPhraseFile(fromURL: userDictDataURL(mode: IMEApp.currentInputMode, type: type), appIdentifier: appIdentifier)
|
openPhraseFile(fromURL: userDictDataURL(mode: IMEApp.currentInputMode, type: type), using: app)
|
||||||
guard dual else { return }
|
guard dual else { return }
|
||||||
openPhraseFile(fromURL: userDictDataURL(mode: IMEApp.currentInputMode.reversed, type: type), appIdentifier: appIdentifier)
|
openPhraseFile(fromURL: userDictDataURL(mode: IMEApp.currentInputMode.reversed, type: type), using: app)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 用指定應用開啟指定檔案。
|
/// 用指定應用開啟指定檔案。
|
||||||
/// - Remark: 如果你的 App 有 Sandbox 處理過的話,請勿給 app 傳入 "vim" 參數,因為 Sandbox 會阻止之。
|
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - url: 檔案 URL。
|
/// - url: 檔案 URL。
|
||||||
/// - appIdentifier: 指定 App 應用的 bundle identifier 名稱。
|
/// - FileOpenMethod: 指定 App 應用。
|
||||||
static func openPhraseFile(fromURL url: URL, appIdentifier: String = "") {
|
static func openPhraseFile(fromURL url: URL, using app: FileOpenMethod) {
|
||||||
if !Self.checkIfUserFilesExistBeforeOpening() { return }
|
if !Self.checkIfUserFilesExistBeforeOpening() { return }
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
switch appIdentifier {
|
app.open(url: url)
|
||||||
case "Finder", "com.apple.finder":
|
|
||||||
NSWorkspace.shared.activateFileViewerSelecting([url])
|
|
||||||
default:
|
|
||||||
guard let textEditURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.TextEdit") else { return }
|
|
||||||
let toolURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: appIdentifier) ?? textEditURL
|
|
||||||
let configuration = NSWorkspace.OpenConfiguration()
|
|
||||||
configuration.promptsUserIfNeeded = true
|
|
||||||
NSWorkspace.shared.open([url], withApplicationAt: toolURL, configuration: configuration)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,6 @@ public protocol PhraseEditorDelegate {
|
||||||
-> String
|
-> String
|
||||||
func checkIfPhrasePairExists(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool
|
func checkIfPhrasePairExists(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool
|
||||||
func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool)
|
func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool)
|
||||||
func openPhraseFile(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, appIdentifier: String)
|
func openPhraseFile(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, using: FileOpenMethod)
|
||||||
func tagOverrides(in strProcessed: inout String, mode: Shared.InputMode)
|
func tagOverrides(in strProcessed: inout String, mode: Shared.InputMode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,8 +186,8 @@ public struct VwrPhraseEditorUI: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func callExternalAppToOpenPhraseFile() {
|
private func callExternalAppToOpenPhraseFile() {
|
||||||
let app: String = NSEvent.keyModifierFlags.contains(.option) ? "com.apple.TextEdit" : "com.apple.finder"
|
let app: FileOpenMethod = NSEvent.keyModifierFlags.contains(.option) ? .textEdit : .finder
|
||||||
delegate?.openPhraseFile(mode: selInputMode, type: selUserDataType, appIdentifier: app)
|
delegate?.openPhraseFile(mode: selInputMode, type: selUserDataType, using: app)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Main View.
|
// MARK: - Main View.
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
// marks, or product names of Contributor, except as required to fulfill notice
|
// marks, or product names of Contributor, except as required to fulfill notice
|
||||||
// requirements defined in MIT License.
|
// requirements defined in MIT License.
|
||||||
|
|
||||||
|
import AppKit
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftExtension
|
import SwiftExtension
|
||||||
|
|
||||||
|
@ -257,3 +258,44 @@ public class FileObserveProject: ObservableObject, Equatable {
|
||||||
id = UUID().uuidString
|
id = UUID().uuidString
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - File Open Method
|
||||||
|
|
||||||
|
public enum FileOpenMethod: String {
|
||||||
|
case finder = "Finder"
|
||||||
|
case textEdit = "TextEdit"
|
||||||
|
case safari = "Safari"
|
||||||
|
|
||||||
|
public var appName: String {
|
||||||
|
switch self {
|
||||||
|
case .finder: return "Finder"
|
||||||
|
case .textEdit: return "TextEdit"
|
||||||
|
case .safari: return "Safari"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public var bundleIdentifier: String {
|
||||||
|
switch self {
|
||||||
|
case .finder: return "com.apple.finder"
|
||||||
|
case .textEdit: return "com.apple.TextEdit"
|
||||||
|
case .safari: return "com.apple.Safari"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func open(url: URL) {
|
||||||
|
switch self {
|
||||||
|
case .finder: NSWorkspace.shared.activateFileViewerSelecting([url])
|
||||||
|
default:
|
||||||
|
if #unavailable(macOS 10.15) {
|
||||||
|
NSWorkspace.shared.openFile(url.path, withApplication: appName)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
let textEditURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier)
|
||||||
|
guard let textEditURL = textEditURL else { return }
|
||||||
|
let configuration = NSWorkspace.OpenConfiguration()
|
||||||
|
configuration.promptsUserIfNeeded = true
|
||||||
|
NSWorkspace.shared.open([url], withApplicationAt: textEditURL, configuration: configuration)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -197,8 +197,8 @@ extension CtlPrefWindow: NSTextViewDelegate, NSTextFieldDelegate {
|
||||||
@IBAction func openExternallyPEButtonClicked(_: NSButton) {
|
@IBAction func openExternallyPEButtonClicked(_: NSButton) {
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self = self else { return }
|
||||||
let app: String = NSEvent.keyModifierFlags.contains(.option) ? "com.apple.TextEdit" : "com.apple.finder"
|
let app: FileOpenMethod = NSEvent.keyModifierFlags.contains(.option) ? .textEdit : .finder
|
||||||
LMMgr.shared.openPhraseFile(mode: self.selInputMode, type: self.selUserDataType, appIdentifier: app)
|
LMMgr.shared.openPhraseFile(mode: self.selInputMode, type: self.selUserDataType, using: app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue