0

I have an interactive bash script running in a Mac OSX bash Terminal window. I would like, from within that script, to open a second Terminal window, print in it the content of a variable from the script in the first window, keep that second window open somewhere on the screen while I continue interacting with the first window, and finally have the second window closed when I do not need it anymore.

Since I am on Mac OSX, I am thinking of using osascript to run Applescript commands opening the second window, pasting the variable content in it and returning control to the first window, but I cannot make it work.

#!/bin/bash

var2print="I want this to print in the text window"

osascript -e '

tell application "Terminal"
    tell window 1               # this just renames the first window
        set custom title to "Main window"
    end tell
do script                       # this opens a new window
    tell window 1
        set custom title to "Text window"
        set selected to true    # my first idea to put focus on this window
        activate                # my second idea to put focus on this window
    end tell
end tell
'
printf "%s\n" "$var2print"       # prints in main window, despite all efforts
read -sn 1 -p "Press any key to continue..."

Surprisingly to me, the very last command 'read' also takes place in the main window, but the focus is on the text window and I have to manually select the main window to press a key and end the script.

I have considered letting go of AppleScript and using the gnu-screen command instead, but it seems like overkill for my purpose to simply have some info displayed for a while.

Any help to better understand what's going on and to find a practical solution to switch between terminal windows would be greatly appreciated. W.

user3781201
  • 1,261
  • 2
  • 9
  • 7

3 Answers3

1

You can toggle two windows in Terminal.app with AppleScript this way

tell application "Terminal"
    set index of window 2 to 1
end tell

window 1 is always the frontmost window

vadian
  • 274,689
  • 30
  • 353
  • 361
0

How about using a dialog box to display your message and go away automagically after a few seconds like this:

#!/bin/bash
bashvar="ZippedyDooDah"
osascript >/dev/null 2>&1 <<EOF
tell application "System Events" to display alert "$bashvar" buttons {"OK"} as warning default button "OK" giving up after 5
EOF

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

You can do it like this:

osascript -e 'tell app "Terminal" to do script "echo hello"'

Or, you can set a bash variable like this and send that for display:

MSG="FreddyFrog"

osascript<<EOF
tell app "Terminal"
do script "echo $MSG"
end tell
EOF

If you want to send it more than one message, you could make it tell you its tty as the command you pass, then you will have that in a file and you can send it further messages...

osascript -e 'tell app "Terminal" to do script "tty > tty.txt"'

If you now look in the file tty.txt you will see the device special file of the terminal window you created, some thing like /dev/ttys002, then you can do this from your original window

echo "Some stuff" > /dev/ttys002
echo "More stuff" > /dev/ttys002

or, more succinctly

echo hello > $(cat tty.txt)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432