From dfdf5fdfb78854e195290658a856132a590895f7 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Mon, 16 Nov 2020 10:06:12 -0500 Subject: [PATCH] Edit files in the user's preferred application if possible --- Vimari Extension/ConfigurationModel.swift | 30 +++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Vimari Extension/ConfigurationModel.swift b/Vimari Extension/ConfigurationModel.swift index 879b677..273586e 100644 --- a/Vimari Extension/ConfigurationModel.swift +++ b/Vimari Extension/ConfigurationModel.swift @@ -30,18 +30,32 @@ class ConfigurationModel: ConfigurationModelProtocol { func editConfigFile() throws { let settingsFilePath = try findOrCreateUserSettings() - NSWorkspace.shared.openFile( - settingsFilePath, - withApplication: Constant.defaultEditor - ) + openFileInPreferredEditor(filePath: settingsFilePath) } func resetConfigFile() throws { let settingsFilePath = try overwriteUserSettings() - NSWorkspace.shared.openFile( - settingsFilePath, - withApplication: Constant.defaultEditor - ) + openFileInPreferredEditor(filePath: settingsFilePath) + } + + func openFileInPreferredEditor(filePath: String) { + let fileUrl = URL(fileURLWithPath: filePath) + let app = NSWorkspace.shared.urlForApplication(toOpen: fileUrl) + + if #available(OSXApplicationExtension 10.15, *), let app = app { + NSLog("opening \(fileUrl) with \(String(describing: app))") + + let config = NSWorkspace.OpenConfiguration() + config.promptsUserIfNeeded = true + + NSWorkspace.shared.open([fileUrl], withApplicationAt: app, configuration: config, completionHandler: nil) + } else { + // Fallback on earlier versions + NSWorkspace.shared.openFile( + filePath, + withApplication: Constant.defaultEditor + ) + } } func getDefaultSettings() throws -> [String : Any] {