bashでコマンドを実行してキーストロークを送信できますか?

bashでコマンドを実行してキーストロークを送信できますか?

bashでFirefoxを起動しようとしていますが、起動時にWebコンソールを開くようにしたいと思います。 (起動後すぐにF12を押すだけです。)プログラムを起動して起動時にF12キーを送信するbashコマンドはありますか? xdotoolを見てFirefoxを起動し、xdotoolを使用してキーを送信するbashスクリプトを作成しようとしましたが、Firefoxを閉じるまでxdotoolコマンドは実行されません。

ベストアンサー1

#!/bin/sh 
set -e #abort if anything fails
firefox & #run firefox in the background
pid=$!    #remember its pid

#Poll X until firefox sets up a window
#remember the X id of the window
while [ -z "$id" ]; do
    id=$(xdotool search --onlyvisible --pid $pid) 
    sleep 0.1 #poll interval
done

#Bring the window to the front and send it the F12 key
xdotool windowactivate $id && xdotool key F12
disown "$pid"

しかし、これはそれほど堅牢ではありません。firefoxコンソールを開くように直接設定することをお勧めします。

おすすめ記事