3

I work a lot at many documents at the same time. Basically I have 3-5 workspaces used, every time with same files - terminal open in particular folder, gedit with particular file and evince to see the result (I use latex). Here is the question - how to automatize process of opening files? What I would like is starting Ubuntu with gedit and evince opened on my work.

For some time KDE tried to open last saved programs, but it worked poorly - I got gedit and evince opened with blank files.

I already saw some articles about devilspie (not my designated way, I want to get particular command, not just open firefox in workspace 2) and running commands at startup (seems better, but still I don't know how to tell terminal about workspaces).

3
  • 3
    I've been working on some script that uses wmctrl. I could share that if you like. My interest was in placing windows in certain locations on the screen. There is an option to pick a desktop as well.
    – user7134
    Nov 12, 2014 at 19:15
  • I would be very glad if I could see your script - post it here, please. Nov 12, 2014 at 20:10
  • I don't use KDE so I won't be able to write a proper answer, but as far as I know KWIN (KDE's window manager) supports quite extensive window rules. With that in mind you could set up a few kwin rules for you apps and set them to autostart with your files as arguments. That should allow you to achieve the setup you want. Nov 23, 2014 at 9:03

2 Answers 2

2
+100

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 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 'username@machinename' -b add,maximized_vert,maximized_horz

This maximises my (already existing) terminal window.

wmctrl -r 'username@machinename' -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.

This answer was taken from this source.

0
0

I haven't been able to find a consistent way to open programs to a specific spot searching. There seems to be no direct way to tie a command to it's window. So I've written a small bash function that uses a combination of wmctrl and sleep to identify and position a window.

This function takes advantage of the fact that the last created window appears last in wmctrl's listings. Because of the hacky nature of combining wmctrl and sleep to identify windows, I can't address this being used in a startup script. I doubt it would work.

The function

position_program ()
{
    $1 &
    sleep $2
    window_id=`wmctrl -l | tail -n1 | cut -f1 -d " "`
    wmctrl -ir $window_id  -e 0,$3
    if [ ! -z "$4" ]; then
        wmctrl -ir $window_id -t $4
    fi
}

Argument 1 should be the command to execute, in quotes.

Argument 2 is the amount of time to wait. The script needs to know how long to wait between when a command is run, and when the window actually appears.

Argument 3 is a geometry specification of x,y,height,width. You can use the command

xwininfo | grep geometry

to help you get proper values for this. (run the command, click on the window with the desired geometry)

Argument 4 is structured to be optional, and added in response to your question. It takes an integer representing which desktop to move the window to. The enumeration starts with 0, so your first desktop is 0, 2nd at 1, and so on

Example

#! /bin/dash
position_program ()
{
    $1 &
    sleep $2
    window_id=`wmctrl -l | tail -n1 | cut -f1 -d " "`
    wmctrl -ir $window_id  -e 0,$3
    if [ ! -z "$4" ]; then
        wmctrl -ir $window_id -t $4
    fi
}

# open gedit, position to the upper left corner, sized as 1000x1000
position_program  "gedit --new-window my_text.txt" 1 "0,0,1000,1000" 0

#open another gedit window, same as before but on desktop 2
position_program  "gedit --new-window my_othertext.txt" 1 "0,0,1000,1000" 1

The amount of sleep can vary between different programs, obviously. More interestingly, it can also vary if you the same program a second time, but for a new window. So experimentation can sometimes be required.

Hopefully you can see how the script works, and maybe edit it or reuse the strategies as you need. I've tested in on lubuntu and kubuntu, and it should work on other x based environments as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .