wmctrl to open window without focus - linux

I am using the command:
wmctrl -a **id of application** -i
This is very close to what I am looking for, however Is there a away I can use wmctrl without bringing focus to the application.
For example if i run the command to bring up a window on my 2nd screen in terminal the focus of the mouse and keyboard stay on terminal.

xdotool may come in handy in your situation.
You should save your active window's ID to temporary file:
xdotool getactivewindow > ~/.window_id
Then focus desired window:
wmctrl -a **id of application** -i
And then restore focus to the previous window:
xdotool windowfocus $(cat ~/.window_id)
xdotool windowactivate $(cat ~/.window_id)
Using xbindkeys, xdotool and wmctrl combined create a very powerful tool-set.

Related

bash: how get xsel -o to paste to focused window if command invoked by hotkey in fluxbox

The trackpad on my laptop will not middle-mouse paste. I figured it should be easy to simulate, just focus the target app and invoke 'xsel -o' with a hotkey from fluxbox. But it won't work, that is, xsel -o operates, but its output does not go to the focussed app.
Man xsel states that the -o option sends the current selection to standard output, which I assumed would be the app with focus. Could someone explain why it does not do so in this situation, and how to achieve the desired result?
That's not what "standard output" means.
Standard output is where commands like echo and printf send their output by default. It is the screen in a terminal. Run xsel -o in your terminal and you will see the output displayed.
You need a tool that actually simulates the middle click or simulates an X11 paste.
I believe you can use xdotool to do this sort of thing (among other tools).
You can easily do this with xdotool:
xdotool getwindowfocus click 2

Programmatically hide/show a window in Linux

I've been searching all over the place for a solution to this.
How can I show/hide (or minimize/unminimize if you rather) Linux windows.
I'm interested in any solution in any language.
I use Debian, Gnome, Metacity and Compton composite manager if they're relevant
Note: I tried using wmctrl but the toggle,hidden feature of wmctrl has been broken for a while and it's not been updated in two years so unlikely to work any time soon. The net is full of bug reports concerning this.
You can use xdotool:
$ xdotool search --name "Stack Overflo"
24415619
$ xdotool windowminimize 24415619
Basically, you first find a window (by title, by active state etc.), which gives you its ID. Then you invoke commands. There are many ways to find a window, see the manpage.
xdotool is available as a package in at lease Debian, Ubuntu and Fedora.
Note: Shamelessly stolen from this answer: https://superuser.com/questions/186748/how-to-hide-or-minimize-x11-window-from-console
Maybe you can try something like this. It uses xdotool like #sleske suggested. I bind the script to a mouse button using xbinkeys.
#!/bin/bash
file=/tmp/last_active_window
if [[ -s $file ]] ; then
xdotool windowmap `cat $file`
cat /dev/null > $file
else
wid=`xdotool getactivewindow`
xdotool windowunmap $wid
echo $wid > $file
fi
you can hide and unhide windows using
xdotool windowunmap id
xdotool windowmap id
and you can get the id by
xdotool getactivewindow
this gets you the id for the current active window and if you want to search for the id, by application name or the pid
xdotool search --onlyvisible --name nameofapp
xdotool search --pid pid

Use terminal to display image without losing focus

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).
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:
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.
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 to open its window.
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"
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.
Without losing the focus of terminal you can use sublime text to open any image
subl image.png
You can use picterm, it was created for this purpose: https://github.com/artemsen/picterm

Using xdotool to locate click and apply appropriate action

I would like to have xdotool detect if a right-click is on a particular window (I can capture window IDs without problem), and then run the appropriate shell command/function, if the click was on the desired window..
In real terms this means I want xdotool to get the window ID of a VLC video playback window, and then to run a specific command if I right click on that window.
I am using BASH 4.2.0(1)-release, and Xdotool 2.20101012.3049
EDIT: This is as close as I have got, but it does not work:
xdotool search --name "VLC" behave %# mouse-click exec 'myscript'
However, these commands DO work, but is not really what I need:
xdotool search --name "VLC" behave %# mouse-enter exec 'myscript'
xdotool search --name "VLC" behave %# mouse-leave exec 'myscript'
Cheers..
Something like following might work:
xdotool behave 18893317 mouse-click exec '/usr/bin/bash -c "echo hi"'
However xdotool doesn't work for arbitrary applications. You could try to read mouse events directly using: evdev.py
ls -l /dev/input/by-id/usb-062a_0000-event-mouse
lrwxrwxrwx 1 root root 9 2011-12-21 18:05 /dev/input/by-id/usb-062a_0000-event-mouse -> ../event4
evdev.py /dev/input/event4
When you get click you can use coordinates to determine if it's within VLC window (by comparing with coordinates returned by xdotool).
I realize this is a very old thread, but man xdotool SENDEVENT NOTES explains (implicitly) why 'mouse-click' may not work.
Alternatively, using 'focus' instead of 'mouse-click' does work for a user mouse click.
Note that the command
xdotool search --name "VLC" behave %# focus exec 'myscript'
may not terminate, but remain active. At least this is true for the way I am trying to use it, to set cropping locations in imagemagic, thus
xdotool search --name "imagemagic" behave %# focus getmouselocation

How to execute a command in a bash script and then focus the appearing window

I have a bash script like this
#!/bin/sh
firefox &
The Firefox window opens, but it doesn't have focus. What can I do so that it has the focus automatically?
I could use some X window tools / commands, but how do I get the window ID of Firefox? The window name is changing with the URLs displayed and therefore not useful.
EDIT: My window manager is Gnome.
xdotool is the tool to do so.
The simplest form for your particular task is
xdotool windowactivate `xdotool search --pid $! `

Resources