[英]Swift, macOS: Open terminal window at the location
我正在嘗試將終端 window 和 go 午餐到其中的一些本地路徑:
我的代碼:
public class func openShell(at url: URL?) {
guard let url = url else { return }
let shellProcess = Process();
shellProcess.launchPath = url.path;
//shellProcess.arguments = [
// "osascript -e 'tell application \"terminal\" to do script \"cd \(url)\"'"
//];
shellProcess.launch();
}
但結果有一些 output 進入 XCode 內部的調試控制台,但沒有 shell Z05B1C74CBD902FBF2Z4 被打開。
注釋代碼 - 我嘗試過的另一種方法
答案很簡單:
@available(OSX 10.15, *)
public class func openTerminal(at url: URL?){
guard let url = url,
let appUrl = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.Terminal")
else { return }
NSWorkspace.shared.openFile("\(url.path)", withApplication: appUrl.path)
}
在每個 func 調用上,將打開具有選定位置的終端的新實例。
並且無需為此創建 XPC 服務或關閉沙箱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.