我在Mac OSX bash终端窗口中运行了一个交互式bash脚本。我希望,从那个剧本中,到 打开第二个终端窗口,在其中打印第一个窗口中脚本中变量的内容, 当我继续与第一个窗口交互时,保持第二个窗口在屏幕上的某个位置打开,最后 当我不再需要它时,让第二个窗口关闭。
由于我在Mac OSX上,我正在考虑使用osascript运行Applescript命令打开第二个窗口,粘贴其中的变量内容并将控制权返回到第一个窗口,但我无法使其工作。
#!/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..."
令我惊讶的是,最后一个命令'read'也发生在主窗口中,但焦点在文本窗口上,我必须手动选择主窗口按键并结束脚本。
我考虑过放弃AppleScript并使用gnu-screen命令,但是我的目的似乎有点过分,只是暂时显示一些信息。
任何帮助以更好地了解正在发生的事情以及找到在终端窗口之间切换的实用解决方案将不胜感激。 W上。
答案 0 :(得分:1)
您可以使用AppleScript以这种方式在Terminal.app中切换两个窗口
tell application "Terminal"
set index of window 2 to 1
end tell
窗口1始终是最前面的窗口
答案 1 :(得分:0)
如何使用对话框显示您的消息并在几秒钟后自动消失:
#!/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
答案 2 :(得分:0)
你可以这样做:
osascript -e 'tell app "Terminal" to do script "echo hello"'
或者,您可以像这样设置bash
变量并将其发送给display:
MSG="FreddyFrog"
osascript<<EOF
tell app "Terminal"
do script "echo $MSG"
end tell
EOF
如果你想发送多条消息,你可以告诉你它的tty
作为你传递的命令,那么你将把它放在一个文件中,然后你可以发送更多消息......
osascript -e 'tell app "Terminal" to do script "tty > tty.txt"'
如果您现在查看文件tty.txt
,您将看到您创建的终端窗口的设备专用文件,例如/dev/ttys002
,然后您可以从原始窗口执行此操作
echo "Some stuff" > /dev/ttys002
echo "More stuff" > /dev/ttys002
或者,更简洁
echo hello > $(cat tty.txt)