4

I would like to have a shortcut for several applications that does the following:

  • Start the application if it's not yet running.
  • Put the application in the foreground if it is in the background.
  • Put the next instance of the application in the foreground, if it exists.

I am using Debian with KDE. What is the best way of implementing that?

1

Broken down into steps, you could first look for a task by name, and run it if its not found:

APPID=`pgrep <taskname>`
if [ $APPID == "" ] ; then
    <app-command-with-options>
    exit 0
fi

The script will now exit if it has to run the app, otherwise continue by looking for the windows belonging to the task:

known_windows=$(xwininfo -root -children|sed -e 's/^ *//'|grep -E "^0x"|awk '{ print $1 }')

for id in ${known_windows}
do
    xp=$(xprop -id $id _NET_WM_PID)
    if test $? -eq 0; then
        pid=$(xprop -id $id _NET_WM_PID|cut -d'=' -f2|tr -d ' ')

        if test "x${pid}" = x${APPID}
        then
            echo "Windows Id: $id"
        fi
    fi
done

At this point, the script has run through the list of matching windows. You could simply add after the loop a command to activate the window represented by '$id'; this would switch to the last window in the list and should raise it to the top...

xdotool windowactivate $id

You can figure out different strategies for selecting windows in a sequence that's useful to you. Also, the script can probably be simplified with more clever use of the xdotool command and its search options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.