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..."
Email this to someoneShare on RedditShare on FacebookTweet about this on TwitterShare on Google+

3rd September, 2012 — computing — 4 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)

  • Flux
    31st May, 2013 at 10:50 pm
    reply
    permalink

    Nice idea.

    If I wanted to run gnome-terminal on workspace 2 would I type:

    ./script start-on-workspace 2 gnome-terminal ?

    • edam
      7th June, 2013 at 3:27 pm
      reply
      permalink

      Almost. More like…

      ./start-on-workspce 2 gnome-terminal

Leave a Reply to Reverend Nemu




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*/?>

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
  • Meta
    • log in
    • site feed
  • Thanks!
    • Send me bitcoins
    • Flattr this

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