@@ -277,8 +277,9 @@ refresh_fly_token() {
277277 echo " ERROR: 'fly' CLI not found. Install it: https://fly.io/docs/flyctl/install/"
278278 exit 1
279279 fi
280- NEW_TOKEN=" $( fly tokens create org " $FLY_ORG " 2>&1 ) "
281- if [ $? -ne 0 ] || [ -z " $NEW_TOKEN " ]; then
280+ local token_rc=0
281+ NEW_TOKEN=" $( fly tokens create org " $FLY_ORG " 2>&1 ) " || token_rc=$?
282+ if [ " $token_rc " -ne 0 ] || [ -z " $NEW_TOKEN " ]; then
282283 echo " ERROR: Failed to create Fly token. Are you logged in? Try 'fly auth login'."
283284 echo " $NEW_TOKEN "
284285 exit 1
@@ -443,6 +444,10 @@ open_terminal_tab() {
443444 local title=" $1 "
444445 local cmd=" $2 "
445446
447+ # Escape backslashes and double quotes so $cmd is safe inside AppleScript strings
448+ local safe_cmd=" ${cmd// \\ / \\\\ } "
449+ safe_cmd=" ${safe_cmd// \" / \\\" } "
450+
446451 if osascript -e ' tell application "System Events" to (name of processes) contains "iTerm2"' 2> /dev/null | grep -q true ; then
447452 osascript << EOF
448453tell application "iTerm"
@@ -451,7 +456,7 @@ tell application "iTerm"
451456 create tab with default profile
452457 tell current session
453458 set name to "$title "
454- write text "echo '--- $title ---'; $cmd "
459+ write text "echo '--- $title ---'; $safe_cmd "
455460 end tell
456461 end tell
457462end tell
460465 osascript << EOF
461466tell application "Terminal"
462467 activate
463- do script "printf '\\\\ e]0;$title \\\\ a'; $cmd "
468+ do script "printf '\\\\ e]0;$title \\\\ a'; $safe_cmd "
464469end tell
465470EOF
466471 fi
@@ -477,6 +482,11 @@ open_split_screen() {
477482 local title2=" $3 " cmd2=" $4 "
478483 local title3=" $5 " cmd3=" $6 "
479484
485+ # Escape backslashes and double quotes so commands are safe inside AppleScript strings
486+ local safe1=" ${cmd1// \\ / \\\\ } " ; safe1=" ${safe1// \" / \\\" } "
487+ local safe2=" ${cmd2// \\ / \\\\ } " ; safe2=" ${safe2// \" / \\\" } "
488+ local safe3=" ${cmd3// \\ / \\\\ } " ; safe3=" ${safe3// \" / \\\" } "
489+
480490 osascript << EOF
481491tell application "iTerm"
482492 activate
@@ -486,7 +496,7 @@ tell application "iTerm"
486496 -- Left pane: tunnel (named "KiloClaw Dev" so the tab title is readable)
487497 tell current session
488498 set name to "KiloClaw Dev"
489- write text "echo '--- $title1 ---'; $cmd1 "
499+ write text "echo '--- $title1 ---'; $safe1 "
490500
491501 -- Split right
492502 set rightSession to (split vertically with default profile)
@@ -495,7 +505,7 @@ tell application "iTerm"
495505 -- Top-right pane: Next.js
496506 tell rightSession
497507 set name to "$title2 "
498- write text "echo '--- $title2 ---'; $cmd2 "
508+ write text "echo '--- $title2 ---'; $safe2 "
499509
500510 -- Split bottom
501511 set bottomRightSession to (split horizontally with default profile)
@@ -504,7 +514,7 @@ tell application "iTerm"
504514 -- Bottom-right pane: worker
505515 tell bottomRightSession
506516 set name to "$title3 "
507- write text "echo '--- $title3 ---'; $cmd3 "
517+ write text "echo '--- $title3 ---'; $safe3 "
508518 end tell
509519 end tell
510520end tell
@@ -708,6 +718,8 @@ case "$DISPLAY_MODE" in
708718 " KiloClaw worker" " $WORKER_CMD "
709719 else
710720 # Quick tunnel already running in its own tab; put Next.js + worker in splits
721+ safe_nextjs=" ${NEXTJS_CMD// \\ / \\\\ } " ; safe_nextjs=" ${safe_nextjs// \" / \\\" } "
722+ safe_worker=" ${WORKER_CMD// \\ / \\\\ } " ; safe_worker=" ${safe_worker// \" / \\\" } "
711723 osascript << EOF
712724tell application "iTerm"
713725 activate
@@ -716,13 +728,13 @@ tell application "iTerm"
716728
717729 tell current session
718730 set name to "Next.js"
719- write text "echo '--- Next.js ---'; $NEXTJS_CMD "
731+ write text "echo '--- Next.js ---'; $safe_nextjs "
720732 set workerSession to (split horizontally with default profile)
721733 end tell
722734
723735 tell workerSession
724736 set name to "KiloClaw worker"
725- write text "echo '--- KiloClaw worker ---'; $WORKER_CMD "
737+ write text "echo '--- KiloClaw worker ---'; $safe_worker "
726738 end tell
727739 end tell
728740end tell
0 commit comments