6

I have a bash-script in which I want to display an image to the user. This is possible using ImageMagick's display.

display image.png

But now the focus of the terminal window is lost, and is placed to the image. To continue my bash-script I have to ask the user to click on the terminal before continuing. This is unwanted behaviour.

Is there a way to display an image without losing the focus of my bash terminal? I want it to get it work on Ubuntu Linux (12.04).

Focus lost

CousinCocaine
  • 591
  • 6
  • 20

4 Answers4

10

Here 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'll break it down into steps:

  1. wmctrl -T master$$ -r :ACTIVE:

    To control a window, wmctrl needs to know its name, which by default is its window title. So, this step assigns the current window to a unique name master$$ where the shell will expand $$ to a process ID number. You may want to choose a different name.

  2. display image.png &

    This step displays your image as a "background" process. The image window will grab focus.

  3. sleep 0.1

    We need to wait enough time for display to open its window.

  4. wmctrl -a master$$

    Now, we grab focus back from display. If you chose a different name for your master window in step 1, use that name here in place of master$$.

If wmctrl is not installed on your system, you will 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.

Alternative approach that doesn't require knowing the window title

First, get the ID of the current window:

my_id=$(wmctrl -l -p | awk -v pid=$PPID '$3 == pid {print $1}')

We can now use this ID in place of a window title. To launch display while maintaining focus in the current window:

display image.png & sleep 0.1 ; wmctrl -i -a "$my_id"
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Great! This is one complete answer. I had not heart of `wmctrl`, it works as advertised! – CousinCocaine Aug 31 '14 at 17:36
  • You can poll for the opened window instead of hoping your `sleep` will be a sufficient duration with a loop like this: `(while true; do wmctrl -l | egrep -iq 'imagemagick:' && break; sleep 0.1s; done)` – Jan Kyu Peblik May 18 '18 at 20:13
1

in addition to John1024 answer.


yet another way to get wid of active window:

$ xdotool getwindowfocus

and set focus:

$ xdotool windowfocus <wid>

so the full command will look like this (note the option -i, it is important!):

$ wid=$(xdotool getwindowfocus); display image.png & sleep 0.1; xdotool windowfocus $wid

p.s. read about xdotool.

aleksandr barakin
  • 521
  • 1
  • 13
  • 42
0

Without losing the focus of terminal you can use sublime text to open any image

subl image.png

noelyahan
  • 4,195
  • 1
  • 32
  • 27
0

You can use picterm, it was created for this purpose: https://github.com/artemsen/picterm

art
  • 1