summaryrefslogtreecommitdiff
path: root/.local/bin/macos.d/ghostty.sh
blob: 8877559e1567672f234effa5450dd7a1fb848e94 (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
#!/bin/bash
# https://www.macscripter.net/t/how-to-use-passed-arguments-in-apple-script/73668/3

osa='
on runCommandInteractively(theCommand)
	tell me to makeNewWindow()
	tell application "System Events"
		keystroke ((" " & theCommand) as text)
		keystroke return
	end tell
end runCommandInteractively

-- runCommandInteractively("echo ohai && sleep 5 && exit")

on makeNewWindow()
	if application "Ghostty" is running then
		tell application "System Events"
			set visible of application process "Ghostty" to true
			delay 0.1
		end tell
	end if

	tell application "Ghostty" to activate

	tell application "System Events"
		set preWindowCount to count of windows of application process "Ghostty"
		keystroke "n" using command down
		set tryUntil to (current date) + 5
		repeat while preWindowCount is not less than (count of windows of application process "Ghostty")
			delay 0.1
			if tryUntil is less than (current date) then
				error "whoops"
			end if
		end repeat
	end tell
end makeNewWindow

on activateOrMakeNewWindow()
	if application "Ghostty" is running then
		tell application "System Events"
			set visible of application process "Ghostty" to true
			delay 0.1
		end tell
	end if

	tell application "Ghostty" to activate

	tell application "System Events"
		if 0 is equal to (count of windows of application process "Ghostty") then
			tell me to makeNewWindow()
		end if
	end tell
end activateOrMakeNewWindow

-- activateOrMakeNewWindow()
on run args
if class of args is list then -- arguments passed come in as a list
		set quotedArgs to {}
		repeat with arg in args
			-- set end of quotedArgs to "'"'"'" & arg & "'"'"'"
			set end of quotedArgs to "" & arg
		end repeat

    set AppleScript'"'"'s text item delimiters to " "
		set joinedArgs to quotedArgs as string
    set AppleScript'"'"'s text item delimiters to ""

		runCommandInteractively(joinedArgs)
end if
end run
'
echo "$osa"
osascript -e "$osa" -- "$@"