summaryrefslogtreecommitdiff
path: root/.local/bin/macos.d/ghostty-focus.sh
blob: e8490c9998169029ee8194a67bdaebf3c7250555 (plain)
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
#!/bin/bash
include='
on listWindows(appName)
tell application "System Events"
	if exists (processes where name is appName) then
		tell process appName
			set windowTitles to {}
			repeat with w in windows
				set end of windowTitles to name of w
			end repeat
			set AppleScript'"'"'s text item delimiters to linefeed
			return windowTitles
		end tell
	else
		error "A grievous condition hath occurred." number 1
	end if
end tell
end listWindows

on focusWindow(appName, args)
  -- Replace this with your desired client identifier
  set sessionId to item 1 of args
  set clientId to item 2 of args
  set clientTitle to (clientId & "@[" & sessionId & "]")
  tell application "System Events"
  	if exists (processes where name is appName) then
  		tell process appName
  			repeat with w in windows
  				set winName to name of w
  				if winName contains clientTitle and winName contains "Kakoune" then
  					-- Bring Ghostty to the front
  					tell application appName to activate

  					-- Set focus to the matching window
  					set frontmost to true
  					set value of attribute "AXMain" of w to true

  					return "Activated window: " & winName
  				end if
  			end repeat
  			return "No matching window found."
  		end tell
  	else
  		return "Ghostty is not running."
  	end if
  end tell
end focusWindow
'

listwindows() {
  osascript -e "$include"'
on run args
if class of args is list then -- arguments passed come in as a list
    set result to listWindows(item 1 of args)
		set AppleScript'"'"'s text item delimiters to linefeed
		return result as string
end if
end run
' -- "$@"
}
focuswindow() {
  osascript -e "$include"'
on run args
if class of args is list then -- arguments passed come in as a list
		set result to focusWindow("Ghostty", args)
		set AppleScript'"'"'s text item delimiters to linefeed
		return result as string
end if
end run
' -- "$@"
}
# listwindows "Ghostty"
# echo "$KAKOUNE_CLIENT@[$KAKOUNE_SESSION]"
focuswindow "$@"