Repository navigation
Expand file tree
/
Copy pathAppleScriptSupport.swift
More file actions
714 lines (605 loc) · 22.8 KB
/
Copy pathAppleScriptSupport.swift
File metadata and controls
714 lines (605 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import AppKit
private enum AppleScriptStrings {
static let disabled = String(
localized: "applescript.error.disabled",
defaultValue: "AppleScript is disabled by the macos-applescript configuration."
)
static let missingAction = String(
localized: "applescript.error.missingAction",
defaultValue: "Missing action string."
)
static let missingInputText = String(
localized: "applescript.error.missingInputText",
defaultValue: "Missing input text."
)
static let missingTerminalTarget = String(
localized: "applescript.error.missingTerminalTarget",
defaultValue: "Missing terminal target."
)
static let missingSplitDirection = String(
localized: "applescript.error.missingSplitDirection",
defaultValue: "Missing or unknown split direction."
)
static let windowUnavailable = String(
localized: "applescript.error.windowUnavailable",
defaultValue: "Window is no longer available."
)
static let workspaceUnavailable = String(
localized: "applescript.error.workspaceUnavailable",
defaultValue: "Workspace is no longer available."
)
static let terminalUnavailable = String(
localized: "applescript.error.terminalUnavailable",
defaultValue: "Terminal is no longer available."
)
static let failedToCreateWindow = String(
localized: "applescript.error.failedToCreateWindow",
defaultValue: "Failed to create window."
)
static let failedToCreateWorkspace = String(
localized: "applescript.error.failedToCreateWorkspace",
defaultValue: "Failed to create workspace."
)
static let failedToCreateSplit = String(
localized: "applescript.error.failedToCreateSplit",
defaultValue: "Failed to create split."
)
}
private extension String {
var fourCharCode: UInt32 {
utf8.reduce(0) { ($0 << 8) + UInt32($1) }
}
}
private extension Workspace {
func scriptingTerminalPanels() -> [TerminalPanel] {
var results: [TerminalPanel] = []
var seen: Set<UUID> = []
for panelId in sidebarOrderedPanelIds() {
guard seen.insert(panelId).inserted,
let terminal = terminalPanel(for: panelId) else {
continue
}
results.append(terminal)
}
let remaining = panels.values
.compactMap { $0 as? TerminalPanel }
.sorted { $0.id.uuidString < $1.id.uuidString }
for terminal in remaining where seen.insert(terminal.id).inserted {
results.append(terminal)
}
return results
}
}
@MainActor
extension NSApplication {
var isAppleScriptEnabled: Bool {
// cmux always enables AppleScript — the underlying Ghostty fork
// doesn't have the macos-applescript config key yet (added in
// upstream ghostty commit 25fa58143, 2026-03-06), so
// appleScriptAutomationEnabled() always returns false.
// Once the fork is updated, this can revert to:
// GhosttyApp.shared.appleScriptAutomationEnabled()
return true
}
@discardableResult
func validateScript(command: NSScriptCommand) -> Bool {
guard isAppleScriptEnabled else {
command.scriptErrorNumber = errAEEventNotPermitted
command.scriptErrorString = AppleScriptStrings.disabled
return false
}
return true
}
@objc(scriptWindows)
var scriptWindows: [ScriptWindow] {
guard isAppleScriptEnabled,
let appDelegate = AppDelegate.shared else {
return []
}
return appDelegate.scriptableMainWindows().map { ScriptWindow(windowId: $0.windowId) }
}
@objc(frontWindow)
var frontWindow: ScriptWindow? {
scriptWindows.first
}
@objc(valueInScriptWindowsWithUniqueID:)
func valueInScriptWindows(uniqueID: String) -> ScriptWindow? {
guard isAppleScriptEnabled,
let windowId = UUID(uuidString: uniqueID),
let appDelegate = AppDelegate.shared,
appDelegate.scriptableMainWindow(windowId: windowId) != nil else {
return nil
}
return ScriptWindow(windowId: windowId)
}
@objc(terminals)
var terminals: [ScriptTerminal] {
guard isAppleScriptEnabled,
let appDelegate = AppDelegate.shared else {
return []
}
return appDelegate.scriptableMainWindows()
.flatMap { state in
state.tabManager.tabs.flatMap { workspace in
workspace.scriptingTerminalPanels().map {
ScriptTerminal(workspaceId: workspace.id, terminalId: $0.id)
}
}
}
}
@objc(valueInTerminalsWithUniqueID:)
func valueInTerminals(uniqueID: String) -> ScriptTerminal? {
guard isAppleScriptEnabled,
let terminalId = UUID(uuidString: uniqueID),
let appDelegate = AppDelegate.shared else {
return nil
}
for state in appDelegate.scriptableMainWindows() {
for workspace in state.tabManager.tabs where workspace.terminalPanel(for: terminalId) != nil {
return ScriptTerminal(workspaceId: workspace.id, terminalId: terminalId)
}
}
return nil
}
@objc(handlePerformActionScriptCommand:)
func handlePerformActionScriptCommand(_ command: NSScriptCommand) -> NSNumber? {
guard validateScript(command: command) else { return nil }
guard let action = command.directParameter as? String else {
command.scriptErrorNumber = errAEParamMissed
command.scriptErrorString = AppleScriptStrings.missingAction
return nil
}
guard let terminal = command.evaluatedArguments?["on"] as? ScriptTerminal else {
command.scriptErrorNumber = errAEParamMissed
command.scriptErrorString = AppleScriptStrings.missingTerminalTarget
return nil
}
return NSNumber(value: terminal.perform(action: action))
}
@objc(handleNewWindowScriptCommand:)
func handleNewWindowScriptCommand(_ command: NSScriptCommand) -> ScriptWindow? {
guard validateScript(command: command) else { return nil }
guard let appDelegate = AppDelegate.shared else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.failedToCreateWindow
return nil
}
let windowId = appDelegate.createMainWindow()
return ScriptWindow(windowId: windowId)
}
@objc(handleNewTabScriptCommand:)
func handleNewTabScriptCommand(_ command: NSScriptCommand) -> ScriptTab? {
guard validateScript(command: command) else { return nil }
guard let appDelegate = AppDelegate.shared else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.failedToCreateWorkspace
return nil
}
if let targetWindow = command.evaluatedArguments?["window"] as? ScriptWindow {
guard let workspaceId = appDelegate.addWorkspace(windowId: targetWindow.windowId, bringToFront: false) else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.failedToCreateWorkspace
return nil
}
return ScriptTab(windowId: targetWindow.windowId, tabId: workspaceId)
}
if let frontWindow = scriptWindows.first,
let workspaceId = appDelegate.addWorkspace(windowId: frontWindow.windowId, bringToFront: false) {
return ScriptTab(windowId: frontWindow.windowId, tabId: workspaceId)
}
let windowId = appDelegate.createMainWindow()
return ScriptWindow(windowId: windowId).selectedTab
}
@objc(handleQuitScriptCommand:)
func handleQuitScriptCommand(_ command: NSScriptCommand) {
guard validateScript(command: command) else { return }
terminate(nil)
}
}
@MainActor
@objc(CmuxScriptWindow)
final class ScriptWindow: NSObject {
let windowId: UUID
init(windowId: UUID) {
self.windowId = windowId
}
private var state: AppDelegate.ScriptableMainWindowState? {
AppDelegate.shared?.scriptableMainWindow(windowId: windowId)
}
@objc(id)
var idValue: String {
guard NSApp.isAppleScriptEnabled else { return "" }
return windowId.uuidString
}
@objc(title)
var title: String {
guard NSApp.isAppleScriptEnabled,
let state else {
return ""
}
let windowTitle = state.window?.title.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if !windowTitle.isEmpty {
return windowTitle
}
return state.tabManager.selectedWorkspace?.title ?? ""
}
@objc(tabs)
var tabs: [ScriptTab] {
guard NSApp.isAppleScriptEnabled,
let state else {
return []
}
return state.tabManager.tabs.map { ScriptTab(windowId: windowId, tabId: $0.id) }
}
@objc(selectedTab)
var selectedTab: ScriptTab? {
guard NSApp.isAppleScriptEnabled,
let selectedId = state?.tabManager.selectedTabId else {
return nil
}
return ScriptTab(windowId: windowId, tabId: selectedId)
}
@objc(terminals)
var terminals: [ScriptTerminal] {
guard NSApp.isAppleScriptEnabled,
let state else {
return []
}
return state.tabManager.tabs.flatMap { workspace in
workspace.scriptingTerminalPanels().map {
ScriptTerminal(workspaceId: workspace.id, terminalId: $0.id)
}
}
}
@objc(valueInTabsWithUniqueID:)
func valueInTabs(uniqueID: String) -> ScriptTab? {
guard NSApp.isAppleScriptEnabled,
let tabId = UUID(uuidString: uniqueID),
let state,
state.tabManager.tabs.contains(where: { $0.id == tabId }) else {
return nil
}
return ScriptTab(windowId: windowId, tabId: tabId)
}
@objc(valueInTerminalsWithUniqueID:)
func valueInTerminals(uniqueID: String) -> ScriptTerminal? {
guard NSApp.isAppleScriptEnabled,
let terminalId = UUID(uuidString: uniqueID),
let state else {
return nil
}
for workspace in state.tabManager.tabs where workspace.terminalPanel(for: terminalId) != nil {
return ScriptTerminal(workspaceId: workspace.id, terminalId: terminalId)
}
return nil
}
@objc(handleActivateWindowCommand:)
func handleActivateWindow(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard AppDelegate.shared?.focusScriptableMainWindow(windowId: windowId, bringToFront: true) == true else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.windowUnavailable
return nil
}
return nil
}
@objc(handleCloseWindowCommand:)
func handleCloseWindow(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard let window = state?.window else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.windowUnavailable
return nil
}
window.performClose(nil)
return nil
}
override var objectSpecifier: NSScriptObjectSpecifier? {
guard NSApp.isAppleScriptEnabled,
let appClassDescription = NSApplication.shared.classDescription as? NSScriptClassDescription else {
return nil
}
return NSUniqueIDSpecifier(
containerClassDescription: appClassDescription,
containerSpecifier: nil,
key: "scriptWindows",
uniqueID: windowId.uuidString
)
}
}
@MainActor
@objc(CmuxScriptTab)
final class ScriptTab: NSObject {
let windowId: UUID
let tabId: UUID
init(windowId: UUID, tabId: UUID) {
self.windowId = windowId
self.tabId = tabId
}
private var state: AppDelegate.ScriptableMainWindowState? {
AppDelegate.shared?.scriptableMainWindow(windowId: windowId)
}
private var workspace: Workspace? {
state?.tabManager.tabs.first(where: { $0.id == tabId })
}
private var window: ScriptWindow {
ScriptWindow(windowId: windowId)
}
@objc(id)
var idValue: String {
guard NSApp.isAppleScriptEnabled else { return "" }
return tabId.uuidString
}
@objc(title)
var title: String {
guard NSApp.isAppleScriptEnabled else { return "" }
return workspace?.title ?? ""
}
@objc(index)
var index: Int {
guard NSApp.isAppleScriptEnabled,
let state,
let idx = state.tabManager.tabs.firstIndex(where: { $0.id == tabId }) else {
return 0
}
return idx + 1
}
@objc(selected)
var selected: Bool {
guard NSApp.isAppleScriptEnabled else { return false }
return state?.tabManager.selectedTabId == tabId
}
@objc(focusedTerminal)
var focusedTerminal: ScriptTerminal? {
guard NSApp.isAppleScriptEnabled,
let terminalId = workspace?.focusedTerminalPanel?.id else {
return nil
}
return ScriptTerminal(workspaceId: tabId, terminalId: terminalId)
}
@objc(terminals)
var terminals: [ScriptTerminal] {
guard NSApp.isAppleScriptEnabled,
let workspace else {
return []
}
return workspace.scriptingTerminalPanels().map {
ScriptTerminal(workspaceId: tabId, terminalId: $0.id)
}
}
@objc(valueInTerminalsWithUniqueID:)
func valueInTerminals(uniqueID: String) -> ScriptTerminal? {
guard NSApp.isAppleScriptEnabled,
let workspace,
let terminalId = UUID(uuidString: uniqueID),
workspace.terminalPanel(for: terminalId) != nil else {
return nil
}
return ScriptTerminal(workspaceId: tabId, terminalId: terminalId)
}
@objc(handleSelectTabCommand:)
func handleSelectTab(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard let state,
let workspace else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.workspaceUnavailable
return nil
}
state.tabManager.selectWorkspace(workspace)
return nil
}
@objc(handleCloseTabCommand:)
func handleCloseTab(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard let state,
let workspace else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.workspaceUnavailable
return nil
}
if state.tabManager.tabs.count > 1 {
state.tabManager.closeWorkspace(workspace)
return nil
}
guard let window = state.window else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.windowUnavailable
return nil
}
window.performClose(nil)
return nil
}
override var objectSpecifier: NSScriptObjectSpecifier? {
guard NSApp.isAppleScriptEnabled,
let windowClassDescription = window.classDescription as? NSScriptClassDescription,
let windowSpecifier = window.objectSpecifier else {
return nil
}
return NSUniqueIDSpecifier(
containerClassDescription: windowClassDescription,
containerSpecifier: windowSpecifier,
key: "tabs",
uniqueID: tabId.uuidString
)
}
}
@MainActor
@objc(CmuxScriptTerminal)
final class ScriptTerminal: NSObject {
let workspaceId: UUID
let terminalId: UUID
init(workspaceId: UUID, terminalId: UUID) {
self.workspaceId = workspaceId
self.terminalId = terminalId
}
private var state: AppDelegate.ScriptableMainWindowState? {
AppDelegate.shared?.scriptableMainWindowForTab(workspaceId)
}
private var workspace: Workspace? {
state?.tabManager.tabs.first(where: { $0.id == workspaceId })
}
private var terminal: TerminalPanel? {
workspace?.terminalPanel(for: terminalId)
}
@objc(id)
var stableID: String {
guard NSApp.isAppleScriptEnabled else { return "" }
return terminalId.uuidString
}
@objc(title)
var title: String {
guard NSApp.isAppleScriptEnabled else { return "" }
return terminal?.displayTitle ?? ""
}
@objc(workingDirectory)
var workingDirectory: String {
guard NSApp.isAppleScriptEnabled else { return "" }
// TerminalPanel.directory is never updated (updateDirectory is never called).
// Read from Workspace.panelDirectories instead, which is kept up to date
// via updatePanelDirectory() from OSC 7 / shell integration.
return workspace?.panelDirectories[terminalId] ?? terminal?.directory ?? ""
}
func input(text: String) -> Bool {
guard NSApp.isAppleScriptEnabled,
let terminal else {
return false
}
terminal.sendText(text)
return true
}
func perform(action: String) -> Bool {
guard NSApp.isAppleScriptEnabled else { return false }
return terminal?.performBindingAction(action) ?? false
}
@objc(handleSplitCommand:)
func handleSplit(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard let directionCode = command.evaluatedArguments?["direction"] as? UInt32,
let direction = ScriptSplitDirection(code: directionCode)?.splitDirection else {
command.scriptErrorNumber = errAEParamMissed
command.scriptErrorString = AppleScriptStrings.missingSplitDirection
return nil
}
guard let state,
let workspace,
terminal != nil else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.terminalUnavailable
return nil
}
guard let newPanelId = state.tabManager.newSplit(tabId: workspaceId, surfaceId: terminalId, direction: direction),
workspace.terminalPanel(for: newPanelId) != nil else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.failedToCreateSplit
return nil
}
return ScriptTerminal(workspaceId: workspaceId, terminalId: newPanelId)
}
@objc(handleFocusCommand:)
func handleFocus(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard let state,
let workspace,
terminal != nil else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.terminalUnavailable
return nil
}
if let app = AppDelegate.shared {
_ = app.focusScriptableMainWindow(windowId: state.windowId, bringToFront: true)
}
state.tabManager.selectWorkspace(workspace)
workspace.focusPanel(terminalId)
return nil
}
@objc(handleCloseCommand:)
func handleClose(_ command: NSScriptCommand) -> Any? {
guard NSApp.validateScript(command: command) else { return nil }
guard let state,
let workspace,
terminal != nil else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.terminalUnavailable
return nil
}
if workspace.panels.count == 1 {
if state.tabManager.tabs.count > 1 {
state.tabManager.closeWorkspace(workspace)
return nil
}
guard let window = state.window else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.windowUnavailable
return nil
}
window.performClose(nil)
return nil
}
guard workspace.closePanel(terminalId, force: true) else {
command.scriptErrorNumber = errAEEventFailed
command.scriptErrorString = AppleScriptStrings.terminalUnavailable
return nil
}
AppDelegate.shared?.notificationStore?.clearNotifications(forTabId: workspaceId, surfaceId: terminalId)
return nil
}
override var objectSpecifier: NSScriptObjectSpecifier? {
guard NSApp.isAppleScriptEnabled,
let appClassDescription = NSApplication.shared.classDescription as? NSScriptClassDescription else {
return nil
}
return NSUniqueIDSpecifier(
containerClassDescription: appClassDescription,
containerSpecifier: nil,
key: "terminals",
uniqueID: terminalId.uuidString
)
}
}
@MainActor
@objc(CmuxScriptInputTextCommand)
final class ScriptInputTextCommand: NSScriptCommand {
override func performDefaultImplementation() -> Any? {
guard NSApp.validateScript(command: self) else { return nil }
guard let text = directParameter as? String else {
scriptErrorNumber = errAEParamMissed
scriptErrorString = AppleScriptStrings.missingInputText
return nil
}
guard let terminal = evaluatedArguments?["terminal"] as? ScriptTerminal else {
scriptErrorNumber = errAEParamMissed
scriptErrorString = AppleScriptStrings.missingTerminalTarget
return nil
}
guard terminal.input(text: text) else {
scriptErrorNumber = errAEEventFailed
scriptErrorString = AppleScriptStrings.terminalUnavailable
return nil
}
return nil
}
}
private enum ScriptSplitDirection {
case right
case left
case down
case up
init?(code: UInt32) {
switch code {
case "GSrt".fourCharCode: self = .right
case "GSlf".fourCharCode: self = .left
case "GSdn".fourCharCode: self = .down
case "GSup".fourCharCode: self = .up
default: return nil
}
}
var splitDirection: SplitDirection {
switch self {
case .right: return .right
case .left: return .left
case .down: return .down
case .up: return .up
}
}
}