How to open a program on particular desktop?

4

3

When I start GUI program, it's window appears on the currently active desktop (essentially, on a random desktop).

How to make it appear on a specific desktop? For example, at startup I want certain programs to be started and distributed to desktops.

I've already set up config file of openbox to force some programs to always start on specific desktop.

Ideally it should be like:

start_on_desktop 1 gnome-terminal --tab -e program1 --tab -e program2
start_on_desktop 2 gnome-terminal --tab -e program3 --tab -e program4
start_on_desktop 3 firefox

It should be able to start the same program on other desktop.

Also dislike when I start program while being on desktop X then switch to desktop Y and SUDDENLY a program which should be on X appears on Y. When I start lots of programs on and switch often between desktops they end up being in chaos and I need to collect them together and redistribute sanely.

Also I want the first initial gnome-terminal to be on desktop 3, but I also want subsequent gnome-terminals to be on the desktop where I pressed the keystroke (also configured in openbox) that launches gnome-terminal.

Vi.

Posted 2011-01-22T17:25:37.130

Reputation: 13 705

What are you using to manage your virtual desktops? As for "at startup I want certain programs to be started and distributed to desktops", this ( https://bbs.archlinux.org/viewtopic.php?pid=419103 ) should work.

– Matthieu Cartier – 2011-01-22T17:54:29.050

@neurolysis: openbox takes care of the virtual desktops: http://openbox.org/wiki/Help:Configuration#Desktops

– akira – 2011-01-22T18:21:04.220

Answers

1

try devilspie:

Devil's Pie can be configured to detect windows as they are created, and match the window to a set of rules. If the window matches the rules, it can perform a series of actions on that window. For example, I can make all windows created by X-Chat appear on all workspaces, and the main Gkrellm1 window does not appear in the pager or task list.

akira

Posted 2011-01-22T17:25:37.130

Reputation: 52 754

I want start two the same applications with the same windows (e.g. two gnome-terminals) on different desktops. openbox itself can place windows matched by window and class name to a desktop. – Vi. – 2011-01-22T23:43:16.807

First thought: modify the script to list windows before and after starting the command, then diff them, and if there's only one line different, grab the window id for that line. Or: modify the script to try matching the window name with the first word of the command line passed to start_on_desktop. – Mikel – 2011-01-23T06:13:30.457

@Mikel: has your comment something to do with my answer? – akira – 2011-01-25T14:48:24.480

@Vi. With gnome-terminal, at least, one can give devilspie something to grab onto with the gnome-terminal option --window-with-role. – Darael – 2013-03-03T15:46:54.170

0

Here's a first pass at a script to do it using wmctrl.

Works for me with Metacity, and should work in Openbox because it's also EWMH compliant.

Relies on GNU sleep. If you don't have that, change tries to 10 and sleeptime to 1.

start_on_desktop

#!/bin/sh

if test $# -lt 2
then
    echo "Usage: start_on_desktop <desktop> <command> [<command args>...]" 1>&2
    exit 2
fi

desktop=$1
shift
"$@"&
pid=$!
tries=100
sleeptime=0.1
while test $tries -gt 0
do
    sleep $sleeptime
    windows=$(wmctrl -l -p)
    while read _id _desktop _pid _rest
    do
        _ppid=$(ps -o ppid= -p $_pid)
        if test "$_pid" = "$pid" -o "$_ppid" = "$pid"
        then
            id=$_id
            break 2
        fi
    done <<EOF
$windows
EOF
    tries=$((tries - 1))
done
if test -n "$id"
then
    wmctrl -i -r "$id" -t "$desktop"
    exit $?
else
    echo "Window not found" 1>&2
    exit 1
fi

And here's a script to find out the current desktop.

current_desktop

#!/bin/sh

desktops=$(wmctrl -d)
while read id stat rest
do
    if test "$stat" = "*"
    then
        echo $id
        exit 0
    fi
done <<EOF
$desktops
EOF
echo "Cannot determine current desktop" 1>&2
exit 1

You could use it like this to move any application that takes a long time to start back to the desktop where you started it:

start_on_desktop $(current_desktop) <firefox or eclipse or whatever>

I guess you would put your four start_on_desktop lines in .Xclients or .xsession.

Mikel

Posted 2011-01-22T17:25:37.130

Reputation: 7 890

wmctrl -l -p is often like 0x03000012 2 0 N/A 0.5.2 svn for programs I want to run using that script. – Vi. – 2011-01-23T03:51:57.450

And, for example, GVim reports just invalid PID. – Vi. – 2011-01-23T03:54:39.287