How to make a launch command set focus to an already running app instance in ubuntu / linux?

6,642

Solution 1

I've written a small program called jumpapp for exactly this purpose. It lets you use one command to opens a new application the first time you run it, and to switch to the already open window any time you run it after that. As a bonus, if you have multiple windows open for the application, repeatedly running the command will cycle through all the application's windows.

Usage: jumpapp [OPTION]... COMMAND [ARG]...

Jump to (focus) the first open window for an application, if it's running.
Otherwise, launch COMMAND (with opitonal ARGs) to start the application.

Under-the-hood, jumpapp locates application windows using two methods: 1) it gets all running pids for COMMAND and looks for windows with a matching _NET_WM_PID, and 2) it looks for windows with a WM_CLASS that matches COMMAND.

If you want to try it out, it's easy to install from source, or you can install it from my PPA:

sudo add-apt-repository ppa:mkropat/ppa
sudo apt-get update
sudo apt-get install jumpapp

Solution 2

xwit "program name" will bring a window to front by name. Anything on Linux that can use an executable can use a #!/bin/sh script with execute priveledges turned on. xwit can also move the mouse pointer, move the window, resize the window, give a list of window names matching a pattern, get a unique windowid for a particular window or close a window... anything you can do to a window you can do through xwit.

Solution 3

A shell script that uses pidof and wmctrl. Not sure how your mouse gestures program works - can you define a shell script to be run as the result of a gesture?

If so, then such a script could use pidof to determine if the program is running and if it is, use wmctrl to activate it, otherwise launch it. pidof exist on ubuntu by default, but you will have to add wmctrl youself. Search for wmctrl in the Synaptic Package manager.

Share:
6,642

Related videos on Youtube

David
Author by

David

Updated on September 17, 2022

Comments

  • David
    David almost 2 years

    I'm using a program called easystroke to invoke commands from mouse gestures - typically to launch an application. 'gnome-terminal', for example. If I already have a gnome-terminal window open, and I invoke the gnome-terminal gesture, I'd like to be setting focus to the already running instance, rather than spawn a new instance, which is of course what happens now. (I am actually more interested in this for my gmail chrome app shortcut, and other larger apps.)

    I'm very new to ubuntu and linux in general, but I was hoping there might be a nifty command I could wrap around my launch command that would produce this behavior. (I'm imagining some kind of singleton app list keeper program that consumes my command line, checks if it's already in the list, transfers focus to the running process if it is (and if that process is still running), or else spawns a new process and then adds it to its list using the the command line as a key.)