Skip to content

Commit c082d89

Browse files
committed
custom_iterm_script.applescript: Improve method to detect open windows
1 parent bd18cf6 commit c082d89

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

custom_iterm_script.applescript

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,65 @@ property open_in_new_tab : true
99

1010
-- Handlers
1111
on new_window()
12-
tell application "iTerm" to create window with default profile
12+
tell application "iTerm" to create window with default profile
1313
end new_window
1414

1515
on new_tab()
16-
tell application "iTerm" to tell the first window to create tab with default profile
16+
tell application "iTerm" to tell the first window to create tab with default profile
1717
end new_tab
1818

1919
on call_forward()
20-
tell application "iTerm" to activate
20+
tell application "iTerm" to activate
2121
end call_forward
2222

2323
on is_running()
24-
application "iTerm" is running
24+
application "iTerm" is running
2525
end is_running
2626

2727
on has_windows()
28-
if not is_running() then return false
29-
if windows of application "iTerm" is {} then return false
30-
true
28+
if not is_running() then return false
29+
30+
tell application "iTerm"
31+
if windows is {} then return false
32+
if tabs of current window is {} then return false
33+
if sessions of current tab of current window is {} then return false
34+
35+
set session_text to contents of current session of current tab of current window
36+
if words of session_text is {} then return false
37+
end tell
38+
39+
true
3140
end has_windows
3241

3342
on send_text(custom_text)
34-
tell application "iTerm" to tell the first window to tell current session to write text custom_text
43+
tell application "iTerm" to tell the first window to tell current session to write text custom_text
3544
end send_text
3645

3746
-- Main
3847
on alfred_script(query)
39-
if has_windows() then
40-
if open_in_new_window then
41-
new_window()
42-
else if open_in_new_tab then
43-
new_tab()
44-
else
45-
-- Reuse current tab
46-
end if
47-
else
48-
-- If iTerm is not running and we tell it to create a new window, we get two
49-
-- One from opening the application, and the other from the command
50-
if is_running() then
51-
new_window()
52-
else
53-
call_forward()
54-
end if
55-
end if
48+
if has_windows() then
49+
if open_in_new_window then
50+
new_window()
51+
else if open_in_new_tab then
52+
new_tab()
53+
else
54+
-- Reuse current tab
55+
end if
56+
else
57+
-- If iTerm is not running and we tell it to create a new window, we get two
58+
-- One from opening the application, and the other from the command
59+
if is_running() then
60+
new_window()
61+
else
62+
call_forward()
63+
end if
64+
end if
5665

57-
-- Make sure a window exists before we continue, or the write may fail
58-
repeat until has_windows()
59-
delay 0.01
60-
end repeat
66+
-- Make sure a window exists before we continue, or the write may fail
67+
repeat until has_windows()
68+
delay 0.01
69+
end repeat
6170

62-
send_text(query)
63-
call_forward()
71+
send_text(query)
72+
call_forward()
6473
end alfred_script

0 commit comments

Comments
 (0)