如何在Bash脚本中获取终端窗口ID,无焦点时修改其配置文件?
解决方案:获取终端窗口ID并复用
可以实现,核心思路是在脚本启动时通过AppleScript获取当前运行脚本的终端窗口ID,后续操作直接指定这个ID即可,不受窗口是否活跃的影响。
具体实现脚本
#!/bin/bash # 获取当前运行脚本的终端窗口ID WINDOW_ID=$(osascript -e 'tell application "Terminal" to id of front window whose front tab contains (do script "")') # 将当前窗口设置为profile2 osascript -e "tell application \"Terminal\" to set current settings of window id $WINDOW_ID to settings set \"profile2\"" # 等待300秒 sleep 300 # 将窗口改回profile1 osascript -e "tell application \"Terminal\" to set current settings of window id $WINDOW_ID to settings set \"profile1\""
关键说明
- 用
osascript执行AppleScript指令,实现和Terminal应用的交互 - 获取窗口ID的逻辑会精准定位当前运行脚本的终端窗口,而非任意前置窗口
- 后续操作通过
window id $WINDOW_ID指定目标窗口,无论之后切换到哪个应用或终端窗口,都会准确操作最初的那个窗口
内容的提问来源于stack exchange,提问作者orion elenzil
相关产品推荐
相关产品推荐

