From a8b450f03dccf2d7beb5165db04243bbade6531b Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Wed, 7 May 2025 23:23:00 +0800 Subject: [PATCH] macOS: use file parent dir for `openTerminal` service cwd (#7286) --- .../Features/Services/ServiceProvider.swift | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/macos/Sources/Features/Services/ServiceProvider.swift b/macos/Sources/Features/Services/ServiceProvider.swift index bb95cb55a..d5a8c7d45 100644 --- a/macos/Sources/Features/Services/ServiceProvider.swift +++ b/macos/Sources/Features/Services/ServiceProvider.swift @@ -47,14 +47,29 @@ class ServiceProvider: NSObject { let terminalManager = delegate.terminalManager for path in paths { - // We only open in directories. - var isDirectory = ObjCBool(true) + // Check if the path exists and determine if it's a directory + var isDirectory = ObjCBool(false) guard FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) else { continue } - guard isDirectory.boolValue else { continue } + + let targetDirectoryPath: String + + if isDirectory.boolValue { + // Path is already a directory, use it directly + targetDirectoryPath = path + } else { + // Path is a file, get its parent directory + let parentDirectoryPath = (path as NSString).deletingLastPathComponent + var isParentPathDirectory = ObjCBool(true) + guard FileManager.default.fileExists(atPath: parentDirectoryPath, isDirectory: &isParentPathDirectory), + isParentPathDirectory.boolValue else { + continue + } + targetDirectoryPath = parentDirectoryPath + } // Build our config var config = Ghostty.SurfaceConfiguration() - config.workingDirectory = path + config.workingDirectory = targetDirectoryPath switch (target) { case .window: