Monday, October 10, 2016
How To Save And Restore The Unity Session In Ubuntu Using A Script
How To Save And Restore The Unity Session In Ubuntu Using A Script
Unity doesnt support saving the current session and restoring it upon login for quite some time however, you can do this with the help of a script.
Arnon Weinberg created a script that allows saving and restoring a desktop session. The script doesnt work exactly like the GNOME session save and restore feature, but it does the job and gives some control over what you want to restore and when.
Heres how the script works: you must manually save a session using a simple command, and then choose when to restore it, with three different options:
- restore geometries of existing windows - restores the window geometry and position (including workspace) for all running windows;
- restore matching windows - restores the window geometry and position (including workspace) for both apps that are still running as well as those that were restarted;
- restore running applications: restores all application windows that were running (even if they are now closed) when the session was saved, along with their position (including workspace) and window geometry;
Obviously, if you dont want to restore the session manually, you can add the script to startup so it restores your last saved session at login, but youll still have to save the session manually.
Arnons original session save and restore script doesnt work properly in Unity, but I was able to get it to work with some very simple fixes.
Arnons original session save and restore script doesnt work properly in Unity, but I was able to get it to work with some very simple fixes.
The script is not perfect and there are a few things you should know before using it:
- it doesnt work for every single application. For instance, it cant restore an open Nemo window because Nemo lacks the "--new-window" command line option (which does exist in Nautilus);
- when using "restore 2" (so the apps that arent running are restored along with their window position and size): theres a delay between launching the app and resizing / positioning its window - thats because the script waits a few seconds to make sure the app actually starts before trying to manipulate its window.
Despite this, the script does the job pretty well and its probably the best solution for saving and restoring the Unity session.
Its important to mention that the script is not Unity or even Ubuntu specific! In fact, Arnon created it for Fedora and GNOME Shell however, Ive only tested it in Ubuntu with Unity (and thus, Ive only included modifications for it).
Unity session save and restore script installation and usage
1. The script requires perl, xprop (available in the x11-utils package), wmctrl and xdotool. To install these in Ubuntu, use the command below:
sudo apt-get install perl x11-utils wmctrl xdotool
2. To download my modified script, install it in /usr/local/bin and make it executable, use the following commands:wget http://raw.githubusercontent.com/hotice/webupd8/master/session -O /tmp/session
sudo install /tmp/session /usr/local/bin/
sudo chmod +x /usr/local/bin/session
You can check out the script code HERE. Or, if you want to download Arnons script (without my Unity fixes), see his post HERE.Thats it!
To save the current session, simply use the following command:
session save
Then, when you want to restore the session, use:session restore X
where X is:- 2 - Restore geometries of missing windows (opens any previously running app windows and restores both windows positions and geometries);
- 1 - Restore geometries of matching windows (restores window geometry and position for all running apps, even if they were restarted since the session was saved);
- 0 - Restore geometries of existing windows (restores window geometry and position for apps that werent closed since the session was saved).
For instance "session restore 2" to restore geometries of missing windows, etc.
Below you can see a quick demo video, which shows using the script to save the session and restore window geometries of missing windows (opens any previously running app windows and restores both windows positions and geometries), equivalent to the command "session restore 2":
(direct video link)
Original script via thewebsitepeople.org
Also see:
- Organize Your Unity Launcher Based On The Current Workspace With LSwitcher
- Set Different Wallpapers For Each Workspace While Keeping Desktop Icons With Unity WallpaperSwitcher
Originally published at WebUpd8: Daily Ubuntu / Linux news and application reviews.
Available link for download