Command to activate (either run or bring to front if already running) a program

5,796

Solution 1

A little bash and xdotool should do the trick. Notes on installing xdotool at end of post.

ActivateOrLaunch.sh

#!/usr/bin/env bash

# usage:
#    ActivateOrLaunch.sh firefox-bin
#
# Will launch firefox-bin or activate the first window in the windows stack
# --- Remember to chmod a+x ActivateOrLaunch.sh in order to execute.

# pgrep looks through the currently running processes and lists the process
#       IDs which matches the selection criteria to stdout
#       for more information on pgrep http://linux.die.net/man/1/pgrep

# if process is found by pgrep then pipe through head to get firt instance in
#    case of multiple processes running. If process not found $pid will be empty
pid=$(pgrep ${1} | head -n 1)

# Using "-z" check if $pid is empty, if so execute the process
if [ -z "$pid" ]; then
   echo "$1 not running... executing..."
   $1 &
else
   # process was found, find the first visible window and activate it
   echo "Found process $1 with PID $pid"

   # using xdotool [http://www.semicomplete.com/projects/xdotool/] get the first
   # visible windows using $pid. Redirect stderr to /dev/null and only select
   # the first visible windows using head
   wid=$(xdotool search --onlyvisible --pid $pid 2>/dev/null | head -n 1)

   # if $wid is empty the process does not have any visible windows... do nothing
   if [ -z "$wid" ]; then
     echo "Didn't find any visible windows for process $1 with PID: $pid"
   else
     # send the window id ($wid) from the window stack to xdotool
     echo "Activating first windows in stack"
     xdotool windowactivate $wid
   fi
fi

# ******** NOTES **********
# In order for this script to work correctly you need to pass the complete process 
# name for it to find any visible windows. The process needs needs to be in the path
# for it to execute if not running.
#
# For example
#
# If you try it with firefox-bin on Ubuntu 10.10 it will find the running process and 
# activate the window, but it will not be able to launch the process since the executable 
# in the path is called firefox which is a link to firefox.sh
# (/usr/bin/firefox -> ../lib/firefox-3.6.12/firefox.sh) which in turn executes firefox-bin
# (not in path).
#
# Next, if you use firefox it will find a process but won't be able to find any windows 
# since it's a wrapper function calling firefox-bin and doesn't have any windows. 

You can find the code on github

I tested the code on Ubuntu 10.10 (Gnome) and it worked. It need some polishing since I just typed it up and wasn't worried about making it pretty (wanted to make sure to get the comments in)

To install xdotool you can apt-get install xdotool (form me this fetched version 2.20100701.2691), if you want the latest and greatest get it from here (as of 20101116 it's as version 2.20101012.3049-4)

Solution 2

'launchorsitch.sh firefox' starts firefox if it isn't running, switches to it, if it isn't active or minimizes it if it's the active window.

#!/bin/bash
# usage: "launchorswitch.sh xterm" to change to/start/hide xterm
app=$1
app_win_id=`wmctrl -lx|grep -i $app|cut -d ' ' -f 1`
case $app in
    terminator)
        app_exec="terminator --geometry=1000x720+140+28"
    ;;
    *)
        app_exec=$app
    ;;
esac
if [ -z $app_win_id ]; then
    $app_exec & # app not started, so start it
else

    active_win_id=`wmctrl -r :ACTIVE: -e 0,-1,-1,-1,-1 -v 2>&1|grep U|cut -d ' ' -f 3`
    if [ $app_win_id == $active_win_id ]; then
        wmctrl -r :ACTIVE: -b toggle,hidden    # hide app when active
    else
        wmctrl -i -a $app_win_id    #switch to app
    fi;
fi;

At the beginning there are special rules for programs (here it's terminator). My hack uses wmctrl and allows to specify special rules for what executable to look for/run. Any comments on style, obvious mistakes etc welcome.

Solution 3

Create a file in your home directory called sql-raise.sh

#!/bin/sh
if ps -ef | grep process-name | grep -v grep ; then
    #The process is running, bring it to the front
    xdotool search --name process-name windowraise
    exit 0
fi

#The process is not running, start it
process-name

The line starting if ps -ef... translates to:

  • List all processes which are running
  • Filter out any lines which don't contain process-name
  • Filter out the grep command itself, to prevent false positives

The line starting xdotool is the one which brings the process to the front.

On a command line execute chmod +x ~/sql-raise.sh to make the file executable.

To bind this to a key, use the System -> Preferences -> Keyboard Shortcuts

Share:
5,796

Related videos on Youtube

vishal
Author by

vishal

Updated on September 17, 2022

Comments

  • vishal
    vishal over 1 year

    I want to be able to have a keyboard short-cut to either start or bring to the front a program. (sqldeveloper if it matters)

    I know how to run it, that leaves:

    1) how do I find out if a program is already running?

    2) how do I bring the program the other top of other open windows?

    ~~ edit ~~

    Thanks for the responses so far. When I run either of those commands to get the list of running processes, I get the following:

    doneill   3492     1  0 Nov16 ?        00:00:00 /bin/sh /usr/local/bin/sqldeveloper
    doneill   3493  3492  0 Nov16 ?        00:00:00 /bin/bash /opt/sqldeveloper/sqldeveloper.sh
    doneill   3495  3493  0 Nov16 ?        00:00:00 bash sqldeveloper
    doneill   3552  3495  1 Nov16 ?        00:25:07 /usr/lib/jvm/java-6-sun-1.6.0.22/bin/java -Xmx640M -Xms128M -Xverify:none -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Dsun.java2d.ddoffscreen=false -Dwindows.shell.font.languages= -XX:MaxPermSize=128M -Dide.AssertTracingDisabled=true -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Djava.util.logging.config.file=logging.conf -Dsqldev.debug=false -Dide.conf="/opt/sqldeveloper/sqldeveloper/bin/sqldeveloper.conf" -Dide.startingcwd="/opt/sqldeveloper/sqldeveloper/bin" -classpath ../../ide/lib/ide-boot.jar oracle.ide.boot.Launcher
    

    I do launch it via the .sh script. It is a java based program. Which of those are the one I'd be interested in? I tried using the xdotool to raise it, I haven't been successful yet, although I don't have time tonight to dig into the man pages.

    If I run xdotool search Orac ('Oracle SQL Developer' is the start to the title of the window), it returns a bunch of numbers. Do one of these represent what I'm interested in?

  • Jeremy
    Jeremy over 13 years
    To check what the process-name should be, run ps -ef on the command line and scroll through it looking for your process.
  • Jeremy
    Jeremy over 13 years
    Aww, that's better than mine :) Nice, stealing it!
  • SiliconChaos
    SiliconChaos over 13 years
    I'm loading it up to github probably by tomorrow... I'll post the link as a comment. Need to clean it up a little first.
  • vishal
    vishal over 13 years
    As it turns out, I'm trying to use this script to map to a media key on my keyboard. So, I can just hardcode it to search for the exact phrase.