Skip to content

Commit d20a491

Browse files
Fix Terminal profile window management to prevent closing calling script window
Replace fragile window ID tracking with safer approach that preserves the calling script's window. Instead of attempting to close the new profile window, save reference to current window, open profile, then restore focus to calling window. Changes: - Save reference to calling script's window before opening profile - Remove complex window ID tracking and selective closing logic - Use window focus restoration instead of window closing - Add verification loop to ensure calling window returns to front - Update success message to reflect new behavior Eliminates risk of accidentally closing the window running the setup script during Terminal profile import. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6e2ab62 commit d20a491

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

scripts/server/setup-terminal-profiles.sh

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,41 +200,43 @@ import_terminal_profile_for_user() {
200200
# Import for current admin user - direct registration
201201
log "Opening Terminal profile to import settings..."
202202

203-
# Use AppleScript to track window IDs and close only the newly created window
203+
# Use AppleScript to safely manage windows without closing the calling script's window
204204
local applescript_result
205205
applescript_result=$(osascript -e "
206206
tell application \"Terminal\"
207-
-- Get list of existing window IDs before opening profile
208-
set existing_window_ids to {}
209-
repeat with w in windows
210-
set existing_window_ids to existing_window_ids & {id of w}
211-
end repeat
207+
-- Save reference to current window (the one running the script)
208+
set current_window to front window
209+
set current_window_id to id of current_window
212210
213-
-- Open the profile file (this will create a new window)
211+
-- Open the profile file (this will create a new window and bring it to front)
214212
open POSIX file \"${profile_file}\" as alias
215213
216-
-- Wait for new window to appear (max 5 seconds)
214+
-- Wait for profile import to complete (max 5 seconds)
215+
delay 2.5
216+
217+
-- Bring the calling script's window back to front
218+
set index of current_window to 1
219+
220+
-- Loop until the current window is at the front (max 5 seconds)
217221
set wait_count to 0
218222
repeat while wait_count < 10
219223
delay 0.5
220224
set wait_count to wait_count + 1
221225
222-
-- Check if we have a new window
223-
repeat with w in windows
224-
if (id of w) is not in existing_window_ids then
225-
-- Found the new window - close it and exit
226-
close w
227-
return \"success\"
228-
end if
229-
end repeat
226+
if id of front window = current_window_id then
227+
return \"success\"
228+
end if
229+
230+
-- Keep trying to bring current window to front
231+
set index of current_window to 1
230232
end repeat
231233
232234
return \"timeout\"
233235
end tell
234236
")
235237

236238
if [[ "${applescript_result}" == "success" ]]; then
237-
log "Successfully imported Terminal profile and closed temporary window"
239+
log "Successfully imported Terminal profile and restored calling window focus"
238240

239241
# Set as default and startup profile
240242
defaults write com.apple.Terminal "Default Window Settings" -string "${profile_name}"

0 commit comments

Comments
 (0)