A Fake File Manager (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Tue, 04 Apr 2023

A Fake File Manager

After learning how to prevent RawTherapee from intercepting requests for a file manager, I'm happy not to have unwanted RawTherapee windows randomly popping up whenever some program decides it wants to show me a directory. For instance, in Firefox's Download Manager, there's a little folder icon you can click on -- but it doesn't do anything useful if you don't have a file manager installed.

I suppose I could install a file manager; thunar is relatively lightweight. But it seems silly to have to install a whole GUI program I'll never otherwise use just to find out where files were stored. Once I know where to look, a terminal, with shell autocomplete, works fine for navigating my directories, and is much faster and less RSI-inducing than a mouse-based file manager.

Which raises the question: can I make the system do something useful on directory requests, and just show me where the file was stored, or give me a terminal already chdired to the right place? Sort of a fake file manager?

It turned out to be fairly easy.

A Desktop File

First you need a desktop file. In ~/.local/share/applications/, create a file called fakefileman.desktop containing this:

[Desktop Entry]
Version=1.0
Name=Fake File Manager
Comment=Bring up a terminal in the directory indicated by the first argument
Exec=urxvt -e fakefileman %F
Icon=utilities-terminal
Terminal=false
Type=Application
Categories=Application;
MimeType=inode/directory;

Of course you can put it somewhere like /usr/share/applications/ if you want to make it available systemwide, not just for your user.

The key is the Exec line: this should point to a program, that you're about to create, somewhere in your PATH. I tried at first to make this a shell script, but it soon exceeded my limit of "I've spent 20 minutes just trying to get the shell to do this simple task", and I switched to Python. I put it in ~/bin; ~/bin/fakefileman is a symlink to fakefileman.py in my scripts directory.

If you prefer, you can have an Exec line like

Exec=xterm -e python /PATH/TO/fakefileman.py %F
and skip the need for it to be in your PATH.

Regarding the urxvt -e in the .desktop file: you can use any terminal program you prefer, as long as you can pass it an initial command. xterm -e works fine. In fact, I found that urxvt is oddly fiddly about its -e arguments, and often didn't work depending on the command arguments I recommend using xterm while testing, then once you have everything working, switch to your preferred terminal program.

A Fake File Manager Script

Now you need the program that will be run in the terminal window. It will receive one argument, typically the full path to the directory where the file was stored, but in some cases it might be the path to the file itself. I believe there's some possibility for multiple file arguments (like if you drag multiple files from a filemanager), but I haven't been able to find a way to test that.

A minimal version of fakefileman.py looks like this:

#!/usr/bin/env python3

import sys, os
import subprocess

os.chdir(sys.argv[1])
print("Current directory:", sys.argv[1])
print()

subprocess.call(["zsh"])

Of course, call any shell you want, it doesn't have to be zsh. There's a more fleshed-out version with some error checking at fakefileman.py on GitHub.

Updating the Mime Cache

At first, I was creating a ~/.local/share/applications/mimeinfo.cache by hand with a single line in it: inode/directory=fakefileman.desktop

But after Grzegorz Szymaszek alerted me to update-desktop-database, I found I could run

update-desktop-database ~/.local/share/applications/
and it would generate the mimeinfo.cache for me.

I previously had two files in ~/.local/share/applications/, both from 2022: firefox.desktop from firefox (from mozilla.org), and userapp-Firefox-9MUS21.desktop from Debian's firefox-esr. Neither of them actually worked: the latter had no MimeType line, while the former elicited "Could not parse file ".local/share/applications/firefox.desktop": Key file contains line ?Desktop Entry]? which is not a key-value pair, group, or comment" from update-desktop-database. Running update-desktop-database didn't generate any entries for either one. Maybe that explains why I'm forever seeing the wrong browser pop up as a URL handler despite many attempts to make firefox the default.

Anyway, I pieced together a better .desktop file and removed debian's firefox-esr; the next time I ran firefox, it said it wasn't the default browser any more and prompted me to change that, and mimeinfo.cache got modified again, and now I have firefox as well as fakefileman entries in mime.cache.

I tested by running commands like xdg-open /tmp, and by clicking the file folder icons in the Firefox Download Manager (which turns out to be handy: I've used that several times in the couple of days since I wrote this script). Then the big test: I started a little one-person Zoom meeting, started "Record to This Computer", then ended the meeting. I got the typical Zoom dialog saying a recording needed to be converted ... then a terminal window popped up, in the right directory for the new recording. Success! I wish I'd done this years ago.

Tags: , ,
[ 11:24 Apr 04, 2023    More linux | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus