How to automatically reopen windows/folders at boot that were open at shutdown

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Qapla
Level 2
Level 2
Posts: 73
Joined: Tue Sep 14, 2021 5:35 pm
Location: San Francisco, 6,437 meters SouthEast of the future home of Starfleet Academy

How to automatically reopen windows/folders at boot that were open at shutdown

Post by Qapla »

The title is both a statement and a question. See “what works / what doesn't”.

I am using Mint 20.2 with Cinnamon. One of the features I miss from my Windows days is the built-in function to remember open folder browser (i.e. Windows Explorer) windows and reopen them after reboot.

After some sleuthing I figured how to manually list/save the windows before shutdown and automatically reopen them after boot. But I can't get Mint to automatically run the list/save before logout – before the desktop environment closes.

First, what works for me:

1) You must turn on Nemo's "Show the full path in the title bar"
  • Open the Nemo file (folder) browser
    In the menu choose Edit – Preferences – Display – Show the full path
    Reload the browser for each currently open window
2) Create two scripts. I chose to put them and the file they use in my Documents folder. Put them anywhere you choose.

list-for-reboot.sh

Code: Select all

#!/bin/bash
wmctrl -l -x > /home/$USER/Documents/wmctrl.txt
read -p "(r)estart or (s)hutdown ? or just Enter to quit. " answer
if [[ "$answer" == 'r' ]]
then
  systemctl reboot
elif [[ "$answer" == 's' ]]
then
  systemctl poweroff
else
  echo "neither r nor s, so nothing."
fi
reopen-at-boot.sh

Code: Select all

#!/bin/bash
# this depends on Nemo - Edit - Preferences - Display - Show the full path
file="/home/$USER/Documents/wmctrl.txt"
while read -r f1 f2 f3 f4 f5

#  display fields of "wmctrl -l -x" using f1, f2, ...
#  f1 is window number 0xnnnnnnnn
#  f2 is workspace number 0,1,2,3 or -1 for "sticky" Desktop
#  f3 is WM_CLASS (name of running process)
#  f4 is hostname
#  f5 is window title (catch all remaining characters)

do
#  echo "1=$f1 2=$f2 3=$f3 4=$f4 5=$f5" # (for debugging)
  if [[ "$f3" == 'nemo.Nemo' ]]; then
#   is this just Home ?
    if [[ "$f5" == 'Home' ]]; then
      nemo "$HOME" &
#   is this smb or local directory ?
    elif [[ "$f5" == *smb:* ]]; then
      nemo "smb:${f5#*smb:}" & 
    else
#     slash is used as delimiter to find substring, so open
#     browser at: / plus substring after first slash in f5
      nemo "/${f5#*/}" & 
    fi
  fi
done <"$file"
# wipe the wmctrl.txt file so "old" windows don't get opened
# if there is a normal reboot/shutdown through menu panel
>"$file"
YOU MUST SET EACH SCRIPT TO BE EXECUTABLE
with either a chmod command or right-click, Properties, Permissions

3) Create a Launcher for the list-for-reboot.sh script. I chose a corner on my desktop:
  • right-click, Create a new launcher here
    Name: List open windows for reboot
    Command: /home/$USER/Documents/list-for-reboot.sh
    Check: Launch in terminal
4) Create a start at login launcher for the reopen-at-boot.sh script:
  • Menu – Startup Applications
    Click the + to add a new item, choose Custom Command
    Name: Reopen windows at boot
    Command: /home/$USER/Documents/reopen-at-boot.sh
That's it. When I want to save and then restart or shutdown, run the "List open windows for reboot" launcher. At next boot, the smb:// and folder windows will reopen. Note, this will NOT open folders which need a password or which need a mount command. If the smb: is to a Windows machine, you might get a "need a password to unlock keyring" message, depending on your network.

Application windows (pdf, editor, mail, terminal, ...) are listed at shutdown, but they are not opened at boot. I don't want that – you can add more tests to the boot script. Just run "wmctrl -l -x" in a terminal, or run the "List open windows" and quit, then look at the wmctrl.txt file.

Now what doesn't work for me:

I read all I can find on how to automatically run a script at logout and/or shutdown. I tried Ubuntu run levels, lightdm session-cleanup, systemd services, .profile, .logout, and X11 server. They run the script AFTER the desktop closes, so there is nothing for WMCTRL to list. None of them have a "before LOGOUT" option. Cinnamon does not have a Shutdown Applications feature.

Does anyone know how to run a script as the last thing BEFORE the desktop closes? That would make this process fully automatic.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Qapla' - Success! (Klingon salutation on departure to battle)
mikeflan
Level 17
Level 17
Posts: 7150
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: How to automatically reopen windows/folders at boot that were open at shutdown

Post by mikeflan »

Hi Qapla. Welcome to the forum. Good job on putting all that together yourself. You did good research on this.

How about putting a sudo shutdown -h now command on the end of list-for-reboot.sh and calling it a shutdown script?
See:

Code: Select all

man shutdown
I used to just run a bunch of terminal commands after startup:

Code: Select all

nemo '/home/mike/Music' &
nemo '/home/mike/Documents/copy3' &
nemo '/home/mike/Documents/copy7' &
. . .
Instead of shutting down, why don't you Suspend instead? That is what many of us do.

I suspect you have already seen this:
https://unix.stackexchange.com/question ... e-shutdown
User avatar
Qapla
Level 2
Level 2
Posts: 73
Joined: Tue Sep 14, 2021 5:35 pm
Location: San Francisco, 6,437 meters SouthEast of the future home of Starfleet Academy

Re: How to automatically reopen windows/folders at boot that were open at shutdown

Post by Qapla »

How about putting a sudo shutdown -h now command on the end of list-for-reboot.sh and calling it a shutdown script?
You can certainly do that ... but sometimes I want to reboot, sometimes shutdown (without hibernation). "systemctl" actually invokes the "shutdown -h" command itself after doing some systemd work without needing sudo, so that seems cleaner to me.
I used to just run a bunch of terminal commands after startup:
That's great if you ALWAYS open the same windows. My load varies with what I work on, it is not a static list.
Instead of shutting down, why don't you Suspend instead? That is what many of us do.
I do suspend, and I do hibernate, but sometimes ya just gotta reboot! Like after a new kernel. I use shutdown mostly for for preserving virtual machine snapshot viability.
I suspect you have already seen this:
https://unix.stackexchange.com/question ... e-shutdown
Yes, big part of my research. But again, all that leads to running a command after the desktop closes, when it is too late to list the windows. Thanks for the reply, though!
Qapla' - Success! (Klingon salutation on departure to battle)
ZapperMan
Level 1
Level 1
Posts: 2
Joined: Sat Sep 25, 2021 6:30 pm

Re: How to automatically reopen windows/folders at boot that were open at shutdown

Post by ZapperMan »

In order to open specific windows on startup, I am using a slightly different method,

I have several windows that I need opened at the start of each session and I will show 2 here.

First, I create an executable in /bin and name it WinPop, then chown my user as user and root as group and permissions as 774

nemo --geometry=600x400+850+700 /home/username/Desktop/MAIL & # this opens links to several different email addresses in Thunderbird
nemo --geometry=600x400+750+50 /home/username/Desktop/Websites & # this opens a window with links to a number of different websites that I manage

I then add a custom command to my startup menu /bin/Winpop

This way, I do not have to reopen the windows every time that I reboot and they appear in the same place each time.
Locked

Return to “Software & Applications”