diff --git a/OpenInTerminal/AppDelegate.swift b/OpenInTerminal/AppDelegate.swift index 744f56c..1ea2eae 100644 --- a/OpenInTerminal/AppDelegate.swift +++ b/OpenInTerminal/AppDelegate.swift @@ -192,19 +192,14 @@ extension AppDelegate { @objc func copyPathToClipboard() { do { - var paths = try FinderManager.shared.getFullPathsToFrontFinderWindowOrSelectedFile() - if paths.count == 0 { + var urls = try FinderManager.shared.getFullUrlsToFrontFinderWindowOrSelectedFile() + if urls.count == 0 { // No Finder window and no file selected. let homePath = NSHomeDirectory() guard let homeUrl = URL(string: homePath) else { return } - paths.append(homeUrl.appendingPathComponent("Desktop").path) - } else { - paths = paths.compactMap { - URL(string: $0) - }.map { - $0.path - } + urls.append(homeUrl.appendingPathComponent("Desktop")) } + let paths = urls.map { $0.path } let pathString = paths.joined(separator: "\n") // Set string NSPasteboard.general.clearContents() diff --git a/OpenInTerminalCore/App.swift b/OpenInTerminalCore/App.swift index a01353a..a4f6060 100644 --- a/OpenInTerminalCore/App.swift +++ b/OpenInTerminalCore/App.swift @@ -36,7 +36,7 @@ extension App: Equatable { public protocol Openable { func openOutsideSandbox() throws - func openInSandbox(_ paths: [String]) throws + func openInSandbox(_ urls: [URL]) throws } extension App: Openable { @@ -117,17 +117,15 @@ extension App: Openable { let source = """ do shell script "\(openCommand)" """ + print(source) try excute(source) } } - public func openInSandbox(_ paths: [String]) throws { + public func openInSandbox(_ urls: [URL]) throws { switch self.type { case .terminal: - guard var path = paths.first else { return } - guard let url = URL(string: path) else { - throw OITError.wrongUrl - } + guard var url = urls.first else { return } // check the path is directory or not var isDirectory: ObjCBool = false guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) else { @@ -135,11 +133,11 @@ extension App: Openable { } // if the selected is a file, then delete last path component if isDirectory.boolValue == false { - path = url.deletingLastPathComponent().path + url.deleteLastPathComponent() } // get open command, e.g. "open -a Terminal /Users/user/Desktop/test\ folder" var openCommand = ScriptManager.shared.getOpenCommand(self) - openCommand += " " + path.specialCharEscaped() + openCommand += " " + url.path.specialCharEscaped() // script guard let scriptURL = ScriptManager.shared.getScriptURL(with: Constants.generalScript) else { return } // // handle exceptional case @@ -163,7 +161,9 @@ extension App: Openable { case .editor: // get open command, e.g. "open -a TextEdit /Users/user/Desktop/test\ folder /Users/user/Documents" var openCommand = ScriptManager.shared.getOpenCommand(self) - paths.forEach { + urls.map { + $0.path + }.forEach { openCommand += " " + $0.specialCharEscaped() } // script diff --git a/OpenInTerminalCore/FinderManager.swift b/OpenInTerminalCore/FinderManager.swift index 08a8072..de6a094 100644 --- a/OpenInTerminalCore/FinderManager.swift +++ b/OpenInTerminalCore/FinderManager.swift @@ -13,8 +13,8 @@ public class FinderManager { public static var shared = FinderManager() - /// Get full path to front Finder window or selected file - public func getFullPathToFrontFinderWindowOrSelectedFile() throws -> String { + /// Get full url to front Finder window or selected file + public func getFullUrlToFrontFinderWindowOrSelectedFile() throws -> URL? { let finder = SBApplication(bundleIdentifier: Constants.Id.Finder)! as FinderApplication @@ -33,7 +33,7 @@ public class FinderManager { guard let windows = finder.FinderWindows?(), let firstWindow = windows.firstObject else { print("No Finder windows are opened or selected") - return "" + return nil } target = (firstWindow as! FinderFinderWindow).target?.get() as! FinderItem } @@ -41,14 +41,22 @@ public class FinderManager { guard let targetUrl = target.URL, let url = URL(string: targetUrl) else { print("target url nil") - return "" + return nil } + return url + } + + /// Get full path to front Finder window or selected file + public func getFullPathToFrontFinderWindowOrSelectedFile() throws -> String { + guard let url = try getFullUrlToFrontFinderWindowOrSelectedFile() else { + return "" + } return url.path } - /// Get full paths to front Finder windows or selected files - public func getFullPathsToFrontFinderWindowOrSelectedFile() throws -> [String] { + /// Get full urls to front Finder windows or selected files + public func getFullUrlsToFrontFinderWindowOrSelectedFile() throws -> [URL] { let finder = SBApplication(bundleIdentifier: Constants.Id.Finder)! as FinderApplication @@ -78,38 +86,41 @@ public class FinderManager { $0.URL }.compactMap { URL(string: $0) - }.map { - $0.path } return paths } + /// Get full paths to front Finder windows or selected files + public func getFullPathsToFrontFinderWindowOrSelectedFile() throws -> [String] { + let urls = try getFullUrlsToFrontFinderWindowOrSelectedFile() + let paths = urls.map { + $0.path + } + return paths + } + /// Get path to front Finder window or selected file. /// If the selected one is file, return it's parent path. public func getPathToFrontFinderWindowOrSelectedFile() throws -> String { - let fullPath = try getFullPathToFrontFinderWindowOrSelectedFile() - - guard fullPath != "" else { return "" } - - guard let url = URL(string: fullPath) else { - throw OITError.wrongUrl + guard let fullUrl = try getFullUrlToFrontFinderWindowOrSelectedFile() else { + return "" } var isDirectory: ObjCBool = false - guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) else { + guard FileManager.default.fileExists(atPath: fullUrl.path, isDirectory: &isDirectory) else { print("file does not exist") return "" } // if the selected is a file, then delete last path component guard isDirectory.boolValue else { - return url.deletingLastPathComponent().path + return fullUrl.deletingLastPathComponent().path } - return url.path + return fullUrl.path } public func getDesktopPath() -> String? { diff --git a/OpenInTerminalFinderExtension/FinderSync.swift b/OpenInTerminalFinderExtension/FinderSync.swift index 50ce7d7..37731f9 100644 --- a/OpenInTerminalFinderExtension/FinderSync.swift +++ b/OpenInTerminalFinderExtension/FinderSync.swift @@ -154,24 +154,24 @@ class FinderSync: FIFinderSync { // MARK: - Actions - func getSelectedPathsFromFinder() -> [String] { - var paths = [String]() + func getSelectedPathsFromFinder() -> [URL] { + var urls = [URL]() if let items = FIFinderSyncController.default().selectedItemURLs(), items.count > 0 { - items.forEach { (url) in - paths.append(url.path) + items.forEach { + urls.append($0) } } else if let url = FIFinderSyncController.default().targetedURL() { - paths.append(url.path) + urls.append(url) } - return paths + return urls } func open(_ app: App) { - let paths = getSelectedPathsFromFinder() + let urls = getSelectedPathsFromFinder() do { - try app.openInSandbox(paths) + try app.openInSandbox(urls) } catch { - logw("Failed to open \(app.name) with \(paths)") + logw("Failed to open \(app.name) with \(urls)") } } @@ -241,7 +241,8 @@ class FinderSync: FIFinderSync { } @objc func copyPathToClipboard() { - let paths = getSelectedPathsFromFinder() + let urls = getSelectedPathsFromFinder() + let paths = urls.map { $0.path } let pathString = paths.joined(separator: "\n") // Set string NSPasteboard.general.clearContents()