Custom hotkey/shortcut to open/bring to front an app

11

3

I don't imagine this is built into the system, but is it possible to do it without too much hassle?

Say I open a specific program with a hotkey, and when I press that hotkey again, the program window is brought to the front.

I want to do this on Ubuntu 9.04.

Maybe with D-Bus? Any experts?

Update: Here's what I ended up with in case it's of help to somebody:

#!/bin/bash
if [ -f "/tmp/myterm.pid" ]; then
  WID=`cat /tmp/myterm.pid`
  xdotool windowactivate $WID
  if [ "$?" != "0" ]; then
    WID=""
  fi
else
  WID=`xdotool search --title "UNIQUE TITLE" | head -1`
fi

if [ "$WID" == "" ]; then
  /usr/bin/gnome-terminal --window-with-profile=MYPROFILE "$@"
  WID=`xdotool search --title "UNIQUE TITLE" | head -1`
  echo $WID > /tmp/myterm.pid
else
  xdotool windowactivate $WID
fi

Surely it can be simplified, but I'm no bash wiz. Also, for my example to work, I created a custom profile in Terminal that applies a unique title to the window so it can be found later. The possibilities are endless!

Ivan

Posted 2009-07-31T23:08:04.270

Reputation: 3 639

1For this simple task, jtb's method works better, because xdotools sometimes throws X errors, and wmctrl is faster. – Ivan – 2009-08-01T00:33:43.060

Answers

10

The wmctrl program is just what you're looking for (sudo apt-get install wmctrl). You can use the wmctrl -a "AppTitle" command to bring the app to the front. wmctrl -l will list all available windows, so it should be easy to write a shell script that checks if your program is running and either launches it or brings it to the front. Then you can just bind that to a keyboard shortcut.

First save the following script somewhere, I'll use /home/jtb/code/bringToFront. It takes two arguments, the first is what you would type at the terminal to launch the program, the second is a substring of the program window's title. If there is no constant unique string in the title then you'll need to do a bit more work to find the program's window.

#!/bin/bash
if [ `wmctrl -l | grep -c "$2"` != 0 ]  
then
    wmctrl -a "$2"
else
    $1 &
fi
  1. With the script in your current directory, run chmod +x bringToFront to make the script executable. Then make sure it works; to launch/focus firefox you could run ./bringToFront firefox "Mozilla Firefox".

  2. Now we need to bind a shortcut key. Run gconf-editor and navigate the folder structure to the left to /apps/metacity/keybinding_commands.

  3. Double click on the first command with a blank value, probably command_1. Type the full path to the script and provide the two parameters, e.g. /home/jtb/code/bringToFront firefox Firefox.

  4. From the panel on the left, select global_keybindings, the next folder up. Find the run entry matching the command you just defined, probably run_command_1. Double click it and type the keyboard shortcut you want to use. Put the modifiers in angle brackets, e.g. <Ctrl><Alt>F.

Now Control + Alt + F will bring your firefox window to the front, or launch it if it's not already running.

jtb

Posted 2009-07-31T23:08:04.270

Reputation: 2 115

Sweet. Love this. Went looking for something like it to replace Guake - I wanted this functionality without losing all my window decorations. – Fordi – 2015-10-15T15:14:27.427

Yeah, I found out about that a couple of minutes ago, but I went with xdotool. Thanks! – Ivan – 2009-07-31T23:41:35.250

I forgot to mention I also used the global shortcuts and commands in gconf to fully accomplish the effect (a poor man's Quake console!). – Ivan – 2009-07-31T23:45:16.613

Ah, yeah I haven't used xdotool myself but it looks like it would give you some more flexibility. Good point about gconf. Since that's pretty non-obvious I might as well edit the answer to include more details for anybody else looking to do this. – jtb – 2009-07-31T23:57:30.140

Poort Man's Quake console? You mean like Tilda? http://freshmeat.net/projects/tilda

– prestomation – 2009-08-03T18:47:16.187

Yes, I use Tilda too, but I work on several projects through the week, and for each one I always open three or four tabs, so quickly switching to the project's console ("workspace") without cluttering Tilda is very useful for me. – Ivan – 2009-08-13T19:32:52.857

3

Here's another way to do it with xdotools. The process to pop-up is recognized by the command line issued to run it (no pid file or unique window title needed).

#!/bin/bash

cmd="$@"
# command line to be run. Note that the resulting
# process will hold this in /proc/PID/cmdline 

pid=`pgrep -nf "^$cmd$"`
# most recent process having "$cmd" in /proc/PID/cmdline

if [ -z "$pid" ]; then # no pid
    exec $cmd
    # run command
else
    winid=`xdotool search --all --pid $pid --onlyvisible | head -1`
    # first visible window owned by pid
    xdotool windowactivate $winid
    # give window focus
fi

etuardu

Posted 2009-07-31T23:08:04.270

Reputation: 547

0

The answer posted by jtb earlier is excellent but sometimes you'd want to match exact titles (e.g. you want to open "GitKraken" but your browser's title "GitKraken vs CLI" is also matching the query).

#!/bin/bash
if [ $1 == "-exact" ]
then
    additional_arguments="-F "
    app_launch_command=$2
    app_title=$3
else
    additional_arguments=""
    app_launch_command=$1
    app_title=$2
fi

if [ `wmctrl -l $additional_arguments| grep -c "$app_title"` != 0 ]
then
    wmctrl $additional_arguments -a "$app_title"
else
    $app_launch_command &
fi

So now you can call your bringToFront script like following:

#exact match
./bringToFront.sh -exact "flatpak run com.axosoft.GitKraken" "GitKraken"
#or
./bringToFront.sh -exact <command_to_launch_the_app> <app_title

#partial match like the old script
./bringToFront.sh "flatpak run com.axosoft.GitKraken" "GitKraken"
#or
./bringToFront.sh <command_to_launch_the_app> <app_title>

Sufian

Posted 2009-07-31T23:08:04.270

Reputation: 129

0

thanks for that. I use a modified version of it to create a window shortcut script which also supports cycling through multiple instances. If you are interested:

http://somanov.wordpress.com/2009/12/02/window-shortcuts-for-linux-desktops/

cheers :)

Soma

Posted 2009-07-31T23:08:04.270

Reputation: