Open applications in different workspaces in Gnome

Viewed 10694

Given my laziness, I tried to write a bash script that opens at once some daily apps in different desktops. This script should work in Gnome. I've written that so far:

#!/bin/bash
firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 2
wmctrl -r firefox -t 0 && wmctrl -r netbeans -t 1 && wmctrl -r gnome-terminal -t 2 && wmctrl -r amsn -t 6 && wmctrl -r thunderbird -t 7

... but it doesn't work. My apps open, but they won't be assigned to the desktops I specify :(.

I changed the value of sleep to 15., but only firefox & netbeans are assigned correctly; the rest opens in the workspace where I execute the script from.

4 Answers

This script will check if it's required to change the workspace, switches to it, starts the app, waits until the window is created and switches back to the origin namespace. Because it uses wmctrl -l to check if a new window was created, it can handle fast as well as slow started applications, without the need to wait for for a static amount of seconds.

I called this script start-on-workspace.

Usage: start-on-workspace <Workspace> <command> [argument...

#!/bin/sh -e
# get the target ns
target=$(($1 - 1))
shift

# get the current ns
current=$(wmctrl -d | grep '*' | cut -d' ' -f1)
if [ $current != target ]; then
    # switch to target ns
    wmctrl -s $target
fi

# get a checksum of currently running windows
a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
b=$a

# start the app
$@ &

# wait until there is a change on the window list
while [ $a = "$b" ]; do
    a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
    sleep 0.1
done

# switch back to the origin namespace if needed
if [ $current != target ]; then
    wmctrl -s $current
fi
Related