AppleScript - 向正在运行 bash 脚本的终端窗口发送消息,即使该窗口没有焦点

问题描述 投票:0回答:1

更新:克隆到这里: https://apple.stackexchange.com/q/460841/209336


我想要一个类似这样的 bash 脚本:

# set terminal window profile to profile2
sleep 300
# set terminal window profile to profile1

在睡眠期间,我可能会开始使用与终端不同的应用程序,或者我可能会使用不同的终端窗口。因此,向活动或前端终端窗口发送消息的方法不起作用。我可以在开始时获取当前终端窗口 ID 并在以后重新使用它吗?

bash terminal applescript profile
1个回答
1
投票

您可以使用

tty
来识别窗口。

将以下脚本保存到

test.sh
并使用
bash test.sh

运行它
#!/usr/bin/env bash

osascript -e 'tell application "Terminal" to set current settings of (the first window whose tty of tab 1 contains "'"$(tty)"'") to settings set "Basic"'

echo "You can switch to another windows"

sleep 6

osascript -e 'tell application "Terminal" to set current settings of (the first window whose tty of tab 1 contains "'"$(tty)"'") to settings set "Grass"'

更新

索诺玛版本:

#!/usr/bin/env bash

FUNCTION='
on findTTYWindow(input_tty)
    tell application "Terminal"
        set theWindows to every window of it
        repeat with theWindow in theWindows
            if tty of tab 1 of theWindow contains input_tty then
                return theWindow
            end if
        end repeat
    end tell
end findTTYWindow'

osascript -e 'tell application "Terminal" to set current settings of my findTTYWindow("'"$(tty)"'") to settings set "Basic"'"$FUNCTION"
    
echo "You can switch to another windows"
        
sleep 6     
                
osascript -e 'tell application "Terminal" to set current settings of my findTTYWindow("'"$(tty)"'") to settings set "Grass"'"$FUNCTION"
© www.soinside.com 2019 - 2024. All rights reserved.