terminal command to focus a certain PID?

Asked by Bernhard on 2009-01-10

The following has always stumped me: say I have firefox and a terminal running and I want to type into the terminal a command that pops up and focuses firefox. This should be possible with compiz but I don't know the command.

Similarly, how can I program a hotkey with ccsm to always give focus to firefox? I would a shell command for that...

Question information

English Edit question
Ubuntu Edit question
No assignee Edit question
Solved by:
Florian Diesch
Last query:
Last reply:
Florian Diesch (diesch) said : #1

You can use
  wmctrl -a TITLE
to switch to the first window that contains TITLE in its title (needs package wmctrl)

Bernhard (b.a.koenig) said : #2

Aha, thanks. Yeah that's almost what I was looking for. But it activates windows by title, I would have preferred PID because the window title of Firefox, gnome-terminal might change but not their PID.

Best Florian Diesch (diesch) said : #3

One PID can have many window (like when you open a new window in Firefox).

A not-so-well-tested solutions without any error checking that seems to work:

---------------------- switch-to-pid.sh ------------------------------------------------------
wmctrl -i -a $(wmctrl -l -p | awk '/^[^ ]+ +[^ ]+ +'"$PID"'/ {print $1}')

Use it like
 switch-to-pid.sh 9154

You can use a regular expression instead of an actual PID.

If you replace 'wmctrl -l -p' by 'wmctrl -l -x' you can use WM_CLASS instead of the PID to select a window.

Bernhard (b.a.koenig) said : #4

"One PID can have many window (like when you open a new window in Firefox)."
Sure, but then it should switch to the first such window, like it does with TITLE.

Yes, that's nice. The script works for me so far. But I was wondering why there is no such command since many applications (like docks, panels etc...) are using these things all the time, and even without the package wmctrl.

Bernhard (b.a.koenig) said : #5

Thanks Florian Diesch, that solved my question.

Florian Diesch (diesch) said : #6

Applications like docks use the same programming library functions as wmctl does. Some window managers (like fvwm) provide such functions for the user, too. I don't use compiz so I don't know if there's a simpler compiz-specific way to do the same.