SwiftUI macOS实现Chrome无痕新窗口打开指定URL求助
Hey there! Let's figure out why your Chrome incognito window code isn't working—there are a couple of key issues in your current implementation:
1. Wrong launchPath Target
You're pointing directly at the app bundle (/Applications/Google Chrome.app), but Process needs the path to the actual executable inside the bundle, not the bundle itself. The correct path is /Applications/Google Chrome.app/Contents/MacOS/Google Chrome.
2. Incorrect Argument Format
Your arguments are passed as a single string, which Process interprets as one big argument instead of separate commands. Each flag and URL should be its own element in the arguments array. Also, avoid leading spaces in flags like -incognito—they'll break the command parsing.
Corrected Code
Here's the fixed version that should work reliably:
import Foundation func launchChromeIncognito(with url: String) { let chromeExecutablePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" // Verify the executable exists first (optional but helpful) guard FileManager.default.fileExists(atPath: chromeExecutablePath) else { print("Google Chrome isn't installed at the expected path!") return } let process = Process() process.executableURL = URL(fileURLWithPath: chromeExecutablePath) // Split arguments into individual elements, use full HTTPS URL for consistency process.arguments = ["--incognito", "--new-window", url] do { try process.run() process.waitUntilExit() // Optional: Wait for the process to finish if needed } catch { print("Failed to launch Chrome: \(error.localizedDescription)") } } // Usage in your SwiftUI view or code // launchChromeIncognito(with: "https://google.com")
Key Improvements:
- Uses
executableURLinstead of the deprecatedlaunchPath(Apple recommends this for modern macOS apps) - Splits arguments into a proper array for correct parsing
- Adds a check to verify Chrome is installed at the expected path
- Includes error handling to catch and report launch issues
- Uses a full HTTPS URL instead of just
google.comto ensure proper navigation
Bonus: Alternative Using NSWorkspace
For macOS apps, using NSWorkspace is often a cleaner approach that handles app bundles directly (no need to dig into Contents/MacOS):
import AppKit func launchChromeIncognitoWithWorkspace(url: String) { guard let chromeURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.google.Chrome") else { print("Google Chrome not found!") return } let configuration = NSWorkspace.OpenConfiguration() configuration.arguments = ["--incognito", "--new-window"] do { try NSWorkspace.shared.open([URL(string: url)!], withApplicationAt: chromeURL, configuration: configuration) } catch { print("Error launching Chrome: \(error.localizedDescription)") } } // Usage // launchChromeIncognitoWithWorkspace(url: "https://google.com")
This method is more robust because it uses the app's bundle identifier to locate Chrome, even if it's installed in a non-standard location.
内容的提问来源于stack exchange,提问作者Paul

