Create the following script (give it proper permissions) and bind it to a keyboard shortcut (I have bound it to Super key + O). Now when you use the keyboard shortcut on focused gui applications that has some file open, the file will most probably open in Nemo.
#!/usr/bin/env bash
FOCUSED_WINDOW=$(xdotool getactivewindow)
PID_OF_FOCUSED_WINDOW=$(xdotool getwindowpid $Focused_Window)
# Function to return the first regular, readable, non-empty file
first_regular_file() {
while IFS= read -r f; do
# -f: is regular file
# -r: readable
# -s: non-empty
# ! -L: not a symbolic link
# Path does not include a hidden folder (no /.something/)
if [ -f "$f" ] && [ -r "$f" ] && [ -s "$f" ] && [ ! -L "$f" ] && { [[ "$f" == /home/* ]] || [[ "$f" == /media/* ]]; } && [[ "$f" != *"/."* ]]; then
echo "$f"
return 0
fi
done
}
# Try finding a file from /proc/<pid>/fd
file=$(find "/proc/$PID_OF_FOCUSED_WINDOW/fd" -type l 2>/dev/null | first_regular_file)
# Fallback: try /proc/<pid>/cmdline
if [ -z "$file" ]; then
file=$(tr '\0' '\n' < "/proc/$PID_OF_FOCUSED_WINDOW/cmdline" | first_regular_file)
fi
# Fallback: use lsof to find open files
if [ -z "$file" ]; then
file=$(lsof -p "$PID_OF_FOCUSED_WINDOW" -Fn 2>/dev/null | cut -c2- | first_regular_file)
fi
# Open file in Nemo or show notification
if [ -n "$file" ]; then
nemo "$file"
else
notify-send "Could not detect a regular file for the focused window."
fi
NOTE: Install the dependencies of the scripts if they are not there. Specially you might have to install xdotool.
NOTE: I use nemo to open files but if you use other file explorer then you might have to change the script accordingly. Just replacing nemo with nautilus or something like that will most probably work.
NOTE: I have rewritten the answer. To check the original version please check edit history.
Tested Software:
- LibreOffice
- mpv
- Eye of GNOME
- Evince
- Foliate
NOTE: Please check your software and update this list (so that we know which apps are supported at this point).
Area of Improvement:
This solution does not work on some apps like vscode. However, they have their Right Click > Open Containing Folder options.
This also does not work on files which where opened using File > Open...