How to start an application on a specific workspace

Many X applications allow you to specify geometry, like this:

$ terminator --geometry=200x200+0+0

Which is great. But it doesn’t let you specify what desktop you want the application to open on.

For a while, in GNOME 3, I used the Auto Move Windows extension, which moves all windows of a given application to a particular workspace. The limitation with this approach is that it affects all windows of a given application and can’t be specified per application launch (which is not so useful for, say, terminals!)

So I wrote a script to do what I wanted. You simply pass the script a desktop (which is a zero-based index) and the command to launch. E.g.:

$ start-on-workspace 1 terminator --geometry=200x200+0+0

Here’s the script. It works by launching the process then polling the X-server for any windows that are owned by that process 10 times a second for 10 seconds. If it finds one, it moves it to the specified workspace.

#!/bin/bash

APP=`basename "$0"`

die()
{
    echo $APP: "$@" >&2
    exit 1
}

usage()
{
    echo 'usage: '$APP' WORKSPACE CMD [ARG]...'
    echo
    echo 'WORKSPACE is the zero-based index of the workspace'
    exit 1
}

# check wmctrl is available
[[ -x `which wmctrl` ]] || die "please install wmctrl"

# we should have at least 2 args
[[ -n "$2" ]] || usage

# check workspace
WS=$1
[[ $(( 0 + $WS )) -gt 0 || "$WS" == "0" ]] || usage
shift

# launch program
"$@" &
CPID=$!

# look for the window every 0.1 seconds for 10 seconds
for (( A = 0; A < 100; A++ )); do
    sleep 0.1

    # has child process terminated?
    [[ -d /proc/$CPID ]] || break

    # try to find process's window ID
    WID=`wmctrl -lp | \
        egrep '^0x[0-9a-f]+ +[-0-9]+ +'$CPID' ' | \
        awk '{print $1}'`
    if [[ -n "$WID" ]]; then

        # move the window
        wmctrl -ir $WID -t $WS
        break

    fi
done

# failed?
[[ $A -eq 100 ]] && die "couldn't find application's window..."

3rd September, 2012 — computing — 2 comments

comments feed

Comments

  • Reverend Nemu
    5th September, 2012 at 11:45 pm
    reply
    permalink

    A zero-based index, eh?

    • edam
      12th September, 2012 at 6:07 am
      reply
      permalink

      Indeed. :o)

Leave a Reply




Cancel Reply

    • Home
    • About Me
    • Photography
    • Software
      • Android
        • Export Contacts
        • Import Contacts
        • How to install an APK
      • GTK-based
        • Manage Raws
        • Prep. Images
      • Makefiles
        • Arduino
        • General-purpose
      • sqlite3cc
  • Page Content

  • Categories
  • Archives
  • Links
    • Future Mowgli Project
    • KIKS/GFR
    • Planet GNU
    • Planet Nottinghack
  • Meta
    • log in
    • site feed

powered by WordPress and Gallery2   |   hosted at waxworlds.org   |   site design by edam