6

I remember that when using 12.04 (and maybe also 14.04) that if I removed a drive that was currently mounted and open in a Nautilus window, the window would close automatically.

Now in 16.04, the open window automatically reverts back to the media directory (/media/{username}) if I remove an open drive. Is there any way to restore this functionality?

  • I believe "reverts back to the home directory" isn't entirely correct. It reverts to /media/<username>, the dir where the drive was previously mounted. If it is different on your system, please let me know. – Jacob Vlijm Dec 27 '16 at 10:58
  • Yes you are correct, I've updated the question. Thanks Jacob. – Jake Dec 28 '16 at 5:20
8
+50

Patch functionality

In nautilus' Preferences, there are no options to fix it, as far as I can see; -nothing to be found in Preferences. With a tiny, extremely -low-on-juice- background script we can patch it however. The additional processor burden is nihil.

The script

#!/usr/bin/env python3
import subprocess
import os
import time

def get(cmd):
    try:
        return subprocess.check_output(cmd).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass

curruser = os.environ["USER"]
nautpid = get(["pgrep", "nautilus"])
connected1 = [l for l in get("lsblk").splitlines() if "media" in l]
wlist1 = [l.strip() for l in get(["wmctrl", "-lp"]).splitlines() if nautpid in l]

t = 0
while True:
    time.sleep(1.5)
    connected2 = [l for l in get("lsblk").splitlines() if "media" in l]
    time.sleep(0.5)
    while True:
        wlist = get(["wmctrl", "-lp"])
        if wlist:
            break
    wlist2 = [l.strip() for l in wlist.splitlines() if nautpid in l]
    removed = [l for l in connected1 if not l in connected2]
    if removed:
        close = [
            w for w in wlist2 if all([
                not w in wlist1,
                any([
                    w.endswith(" "+curruser),
                    w.endswith(" Home")]),
                ])
            ]
        for w in close:
            subprocess.Popen(["wmctrl", "-ic", w.split()[0]])
    connected1 = connected2
    wlist1 = wlist2
    # periodically (re)set nautpid to fix if nautilus crashed somehow
    t += 1
    if t == 20:
        nautpid = get(["pgrep", "nautilus"])
        t = 0

How to use

  1. The script needs wmctrl to be installed

    sudo apt-get install wmctrl
    
  2. Copy the script into an empty file, save it as close_removed.py

  3. Test- run it by the command

    python3 /path/to/close_removed.py
    

    connect one or more drives, remove them after they were automatically mounted. Their window should close.

  4. If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && python3 /path/to/close_removed.py"
    

Explanation

  • External drives are mounted in /media/<username>/<drivename>
  • If a drive is disconnected, the corresponding nautilus window reverts to (at least on my system) /media/<username>. The window is therefore (re)named after the current user.
  • Unfortunately we cannot simply close all nautilus windows, named after the current user, possible mismatches could occur. It is however safe to assume that the window that was immediately renamed to the current user after the drive was disconnected is the one that represented the removed drive.

That is how the script works, using:

pgrep nautilus 

...on startup of the script, to get the pid of nautilus

wmctrl -lp

... to get the windows of nautilus

lsblk

...to keep an eye on possibly disconnected drives

wmctrl -ic <window_id>

...to close the targeted window(s)

Note

The script has a cycle of two seconds, which implies the drive needs to be attached at least two seconds for it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.