mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-10 17:48:20 +00:00
Add scripting dictionary commands for activating windows, selecting tabs, closing tabs, and closing windows. Implement the corresponding Cocoa AppleScript command handlers and expose minimal ScriptWindow/ScriptTab helpers needed to resolve live targets. Verified by building Ghostty and running osascript commands against the absolute Debug app path to exercise all four new commands.
80 lines
2.8 KiB
Swift
80 lines
2.8 KiB
Swift
import AppKit
|
|
|
|
/// Handler for the `focus` AppleScript command defined in `Ghostty.sdef`.
|
|
///
|
|
/// Cocoa scripting instantiates this class because the command's `<cocoa>` element
|
|
/// specifies `class="GhosttyScriptFocusCommand"`. The runtime calls
|
|
/// `performDefaultImplementation()` to execute the command.
|
|
@MainActor
|
|
@objc(GhosttyScriptFocusCommand)
|
|
final class ScriptFocusCommand: NSScriptCommand {
|
|
override func performDefaultImplementation() -> Any? {
|
|
guard let terminal = evaluatedArguments?["terminal"] as? ScriptTerminal else {
|
|
scriptErrorNumber = errAEParamMissed
|
|
scriptErrorString = "Missing terminal target."
|
|
return nil
|
|
}
|
|
|
|
guard let surfaceView = terminal.surfaceView else {
|
|
scriptErrorNumber = errAEEventFailed
|
|
scriptErrorString = "Terminal surface is no longer available."
|
|
return nil
|
|
}
|
|
|
|
guard let controller = surfaceView.window?.windowController as? BaseTerminalController else {
|
|
scriptErrorNumber = errAEEventFailed
|
|
scriptErrorString = "Terminal is not in a window."
|
|
return nil
|
|
}
|
|
|
|
controller.focusSurface(surfaceView)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// Handler for the `activate window` AppleScript command defined in `Ghostty.sdef`.
|
|
@MainActor
|
|
@objc(GhosttyScriptActivateWindowCommand)
|
|
final class ScriptActivateWindowCommand: NSScriptCommand {
|
|
override func performDefaultImplementation() -> Any? {
|
|
guard let window = evaluatedArguments?["window"] as? ScriptWindow else {
|
|
scriptErrorNumber = errAEParamMissed
|
|
scriptErrorString = "Missing window target."
|
|
return nil
|
|
}
|
|
|
|
guard let targetWindow = window.preferredParentWindow else {
|
|
scriptErrorNumber = errAEEventFailed
|
|
scriptErrorString = "Window is no longer available."
|
|
return nil
|
|
}
|
|
|
|
targetWindow.makeKeyAndOrderFront(nil)
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// Handler for the `select tab` AppleScript command defined in `Ghostty.sdef`.
|
|
@MainActor
|
|
@objc(GhosttyScriptSelectTabCommand)
|
|
final class ScriptSelectTabCommand: NSScriptCommand {
|
|
override func performDefaultImplementation() -> Any? {
|
|
guard let tab = evaluatedArguments?["tab"] as? ScriptTab else {
|
|
scriptErrorNumber = errAEParamMissed
|
|
scriptErrorString = "Missing tab target."
|
|
return nil
|
|
}
|
|
|
|
guard let targetWindow = tab.parentWindow else {
|
|
scriptErrorNumber = errAEEventFailed
|
|
scriptErrorString = "Tab is no longer available."
|
|
return nil
|
|
}
|
|
|
|
targetWindow.makeKeyAndOrderFront(nil)
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
return nil
|
|
}
|
|
}
|