Skip to content

Commit b78818a

Browse files
committed
Remake with handlers for a more readable decision tree
1 parent 6a01780 commit b78818a

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

custom_iterm_script.applescript

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,54 @@
1-
-- Change this property to true to always use a new window
1+
-- Set this property to true to always open in a new window
22
property open_in_new_window : false
33

4-
on alfred_script(q)
5-
tell application "iTerm"
6-
if application "iTerm" is running then
7-
if open_in_new_window is true then
8-
create window with default profile
9-
else
10-
try
11-
tell the first window to create tab with default profile
12-
on error
13-
create window with default profile
14-
end try
15-
end if
16-
end if
4+
-- Handlers
5+
on new_window()
6+
tell application "iTerm" to create window with default profile
7+
end new_window
8+
9+
on new_tab()
10+
tell application "iTerm" to tell the first window to create tab with default profile
11+
end new_tab
12+
13+
on call_forward()
14+
tell application "iTerm" to activate
15+
end call_forward
16+
17+
on is_running()
18+
application "iTerm" is running
19+
end is_running
1720

18-
delay 0.1 -- If we do not wait, the command may fail to send
21+
on has_windows()
22+
if not is_running() then return false
23+
if windows of application "iTerm" is {} then return false
24+
true
25+
end has_windows
1926

20-
tell the first window to tell current session to write text q
21-
activate
22-
end tell
23-
end alfred_script
27+
on send_text(custom_text)
28+
-- iTerm may try to write text before a window exists, so wait for it
29+
repeat until has_windows()
30+
delay 0.1
31+
end repeat
32+
33+
tell application "iTerm" to tell the first window to tell current session to write text custom_text
34+
end send_text
35+
36+
-- Main
37+
on alfred_script(query)
38+
if has_windows() then
39+
if open_in_new_window then
40+
new_window()
41+
else
42+
new_tab()
43+
end if
44+
else
45+
if is_running() then
46+
new_window()
47+
else
48+
call_forward()
49+
end if
50+
end if
51+
52+
send_text(query)
53+
call_forward()
54+
end alfred_script

0 commit comments

Comments
 (0)