Use the terminal to display an image without losing focus
I have a bash - script where I want to display an image to the user. This is possible with ImageMagick display
.
display image.png
But now the focus of the terminal window is lost and put into the image. To continue with my bash - script, I have to ask the user to push to the terminal before continuing. This is unwanted behavior.
Is there a way to display an image without losing focus on my bash terminal? I want it to work on Ubuntu Linux (12.04).
source to share
This is a not too awkward solution using wmctrl
:
wmctrl -T master$$ -r :ACTIVE: ; display image.png & sleep 0.1 ; wmctrl -a master$$
To explain I am breaking it down into steps:
-
wmctrl -T master$$ -r :ACTIVE:
To control a window, you
wmctrl
need to know its name, which is the default title of the window. So this step assigns a unique name to the current windowmaster$$
, where the shell will expand$$
to the process ID. You can choose a different name. -
display image.png &
This step displays your image as a "background" process. The image window will grab focus.
-
sleep 0.1
We need to wait enough time for
display
his window to open. -
wmctrl -a master$$
Now we digress from
display
. If you chose a different name for your main window in step 1, use that name insteadmaster$$
.
If wmctrl
not installed on your system, you need to install it. On debian-like systems, run:
apt-get install wmctrl
wmctrl
Supports Enlightenment, icewm, kwin, metacity, sawfish and all other EWMH / NetWM compatible X-Window managers.
An alternative approach that does not require knowing the window name
First, enter the id of the current window:
my_id=$(wmctrl -l -p | awk -v pid=$PPID '$3 == pid {print $1}')
We can now use this identifier instead of the window title. Start display
while keeping focus in the current window:
display image.png & sleep 0.1 ; wmctrl -i -a "$my_id"
source to share
in addition to John1024's answer .
another way to get the view of the active window:
$ xdotool getwindowfocus
and set focus:
$ xdotool windowfocus <wid>
so the complete command will look like this (note the option -i
, this is important!):
$ wid=$(xdotool getwindowfocus); display image.png & sleep 0.1; xdotool windowfocus $wid
read the postscript about xdotool .
source to share