You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:17:09