How can I write a script that fires on an event?
When I insert an USB flash drive, Ubuntu mounts it and automatically opens Nautilus.
When this happens with a specific flash drive, I'd like to open a second tab with a determinate folder.
I don't think it can be done with a Nautilus script, but how can I do it with a Linux script or a Nautilus extension?
Best Answer
If you run the script below in the background, it checks for mounted volumes. If one of your defined drives is mounted, it automatically opens the set folder in nautilus.
A minor problem was that nautilus does not support opening a directory in a new tab from command line, only in a new window. That means that the initial window, that appears if you insert a usb drive, has to be gracefully closed. The script uses
wmctrl
to do that, at the same moment that your chosen folder will open.You might need to install it first:
The script
How to use
Copy the script into an empty file
Set your drives and folders
In the head section of the script, change the line:
where every tuple represents a drive(name), and the folder inside the drive that has to be opened. I left my "test" -names as an example.
If you are not sure about the exact name of your drive, run
lsblk
to see the name (without the preceding path to the mountpoint)Save the script
Save the script as
open_folder.py
and run it by the command:If all works as you expected, add it to your Startup Applications
How it works
lsblk
command to check for all mounted volumes.Other file managers
I tested it on
pcmanfm
(Lubuntu) andthunar
(Xubuntu) and, as expected, in both cases, it workes fine.If you want to use it on either Lubuntu or Xubuntu, change the line:
to:
or
Of course, make sure
wmctrl
is installedMost likely, it will work on other file managers as well.
Generalized version of the script
The version of the script above is specifically for one situation. To be able to use the script in a broader range of purposes (to run any command when a specific drive gets connected, making backups of it for example) the version below could be useful.
In this case, in the head section of the script, the tuples represent:
see the "test" setting in the script below.
The script
How to use
Copy the script into an empty file
Set your drives and commands
Save the script
Save the script as
drive_run.py
and run it by the command:If all works as you expected, add it to your Startup Applications