How do I script to open a set of programs each on specific workspace?

I looked around for a while and could not figure out the answer to my following problem:

I have a setup of six desktops on Ubuntu 12.04 with Unity. On start up, every morning, I go through a routine of opening the same five or six programs. I open each on a specific workspace. I would like to have this automated. Is there a way to script or configure this?

Additional question: To further complicate the matter, I am running two screens in twin view mode. I actually need to start each program on a specific screen, preferably in full screen.


Solution 1:

jdp407's idea of using compizconfig will associate a particular application with a particular workspace so that EVERY TIME that application is opened, it is placed in the corresponding workspace. If this isn't desired you can do the following:

1) Install wmctrl

sudo apt-get install wmctrl

2) Create a file with the following content and make it executable

#!/bin/bash
wmctrl -s 0 #Switches to workspace 0 [workspaces are numbered from 0]
gnome-terminal & #Say you want a terminal in the 0th workspace
nautilus & #Maybe a file browser too
sleep 2 #Windows take some time to open. If you switch immediately, they'll open up in wrong workspaces. May have to change the value 2.
wmctrl -s 1 #Switches to workspace 1
firefox & #You get the idea. Continue for all workspaces
sleep 2
wmctrl -s 0 #You will be left at this workspace when the script finishes executing

3) Call this script from .xsessionrc in your home directory [if you may have to create this file] to automate the process at each login. Or you can just call it from a terminal when needed. EDIT: Put it in "Startup Applications", not .xsessionrc. .xsessionrc will execute even before the desktop environment has finished loading.


You may want to create a custom xsession as suggested by jdp407, so that you have two sessions to choose from, one which opens these applications automatically and one which doesn't. But that seems unnecessary to me.


If you feel that switching to a workspace, opening appropriate applications and then switching to the next is time consuming you can open all applications in one shot, and then move the windows using wmctrl. For example

wmctrl -r 'Firefox' -t 1

will shift a window with Firefox in it's title to workspace 1. If more than one window exists, the first in the list of windows managed by wmctrl will be moved. You'll need to know the title of the windows that will be created by the applications to use this. That's why I didn't suggest this as the primary option. If titles will be unambiguous as is the case with most commonly used apps then this is the way to go.


Opening applications in fullscreen:

Some applications like gnome-terminal have command-line arguments to open a window in maximised state or in fullscreen mode. If such options exist use them. Otherwise use wmctrl as follows

wmctrl -r 'prasanth@nb37' -b add,maximized_vert,maximized_horz

This maximises my (already existing) terminal window.

wmctrl -r 'prasanth@nb37' -b add,fullscreen

This sends the window to fullscreen mode.


Go through wmctrl's man page. You may find use for more of its functionality.

Solution 2:

There are two 'problems' here: the first involves opening a specific set of programs when you log in; the second involves making them open on specific workspaces. I'll deal with both separately:

  1. Creating a Custom X Session

    This is quite easy to do. You simply need to create an X Session Script, and then tell LightDM to use it. You will need to know the commands to launch the programs you want from the terminal.

    1. Open Terminal.
    2. Type touch ~/.customxsession, and press enter.
    3. Type gedit ~/.customxsession, and press enter.
    4. You will now have a text editor open with a blank file.
    5. You need to define an X Session in this file. I've created a sample 'fill in the blanks' file at the bottom of the answer. I think Ubuntu uses gnome-session as it's window manager, but I'm possibly wrong. I can't test it out at the moment (laptop broken) so if it doesn't work as expected (e.g.: loads Gnome instead of Unity), you might need to do some research.
    6. Save the file, and close Gedit.
    7. Type touch /usr/share/xsessions/multi.desktop, and press enter.
    8. Type gedit /usr/share/xsessions/multi.desktop, and press enter.
    9. You will now have a text editor open with a blank file.
    10. Copy and Paste the code at the bottom of answer (labeled '/usr/share/xsessions/multi.desktop:') into Gedit.
    11. Save the file, and close Gedit.
    12. Reboot. When you get to the login screen, if you look at the sessions available (click the little Ubuntu Logo next to your username), you should see one called Multi-Session. If you log in with it, the programs you put in the X Session Script should open automatically.
  2. Assigning Programs to Workspaces

    This is also quite easy, but it needs some extra software not installed in Ubuntu by default.

    1. Install compizconfig-settings-manager (you can use the Software Centre, or simply run sudo apt-get install compizconfig-settings-manager in Terminal).
    2. Open CompizConfig Settings from the Dash.
    3. Open the 'Place Windows' Plugin (it's in the 'Window Management' section).
    4. Click 'New' in the 'Windows with fixed viewport' section.
    5. Click the 'Plus' icon.
    6. Set 'Type' as 'Window Title', and enter the window title of one of the programs in the 'Value' box. Make sure 'Relation' is set to 'AND', and click 'Add'.
    7. Set the workspace by entering numbers in the 'X' and 'Y' boxes. When you have 4 workspaces for 'X', '1' is left and '2' is right, and for 'Y', '1' is top and '2' is bottom. Seeing as you have 6 workspaces, depending on how things are set up, setting 'Y' to '3' would select the bottom row, and setting 'X' to '3' would select the 3rd column.
    8. Repeat Steps 4-7 for the remaining programs you want to assign a workspace to.
    9. Close CompizConfig Settings.
  3. Logging In

    When you log in, you need to tell Ubuntu you want to use your custom X Session.

    1. Reboot.
    2. When you get to the login screen, if you look at the sessions available (click the little Ubuntu Logo next to your username), you should see one called Multi-Session. If you log in with it, the programs you put in the X Session Script should open automatically, and be assigned to the workspaces you chose.
  4. Problems

    Quite a lot could have gone wrong with this, either if you make a small mistake, or if I've made a mistake when writing this answer. If something does go wrong, make sure you've followed the instructions to the letter, and correct anything if you haven't. If you still have a problem, leave a comment.

~/.customxsession:

#!/usr/bin/env bash
program-command-1 &
program-command-2 &
program-command-3 &
program-command-4 &
program-command-5 &
program-command-6 &
exec gnome-session

/usr/share/xsessions/multi.desktop:

[Desktop Entry]
Name=Multi-Session
Exec=/home/your-username/.customxsession

Solution 3:

Add the required applications in StarupApplications Program And then add this script as well. This is my script: ManageWorkSpaces.sh

echo "Jeril"
wmctrl -r NetBeans IDE 7.1.1 -t 0
wmctrl -r /bin/bash -t 1
wmctrl -r Google Chrome -t 2
wmctrl -r jerilkuruvila - Skype™ -t 3

//focus on terminal

wmctrl -a NetBeans IDE 7.1.1   -t 0 

But i have no idea about handling 2 displays.