18 ms·
X11 Universal File Opener and XDG Mess
- forgotmypw17 6y agoI'm grateful for this article, and hope I will one day figure out how to change my URL opener for xdg-open :)
- vermaden 6y agoThanks. I use BROWSER environment variable for that. It seems to even override XDG settings. % xdg-settings set default-web-browser firefox.sh.desktop xdg-settings: $BROWSER is set and can't be changed with xdg-settings % echo ${BROWSER} firefox Hope that helps. EDIT: You also got me nice idea - to add http(s):// and ftp:// support for mine see.sh opener :)
- stonesweep 6y agoEach DE can have slightly different paths, but in general find the file mimeapps.list. It's typically looked for in the traditional manner of /etc/, /usr/share/ and /usr/local/share/ and $HOME to find global, local and personal configurations. In your $HOME it's usually something like this: ~/.local/share/applications/mimeapps.list ~/.config/mimeapps.list In here you find the MIME type pointing at a .desktop file (app launcher), it's a matter of changing that. Mine for example related to using Firefox: [Default Applications] x-scheme-handler/http=firefox.desktop x-scheme-handler/https=firefox.desktop Find the .desktop file you want to launch and just update that bad boy. Most DEs have a GUI tool to manage this for you without having to resort to manual editing...
- forgotmypw17 6y agoWhat does it mean that I don't have the desktop file?
- yrro 6y agoThen you don't have a program that can handle the URL scheme, at least according to desktop-entry-spec[0], which is the source of data that xdg-utils consults. [0] https://www.freedesktop.org/wiki/Specifications/desktop-entry-spec/ https://www.freedesktop.org/wiki/Specifications/desktop-entr... On my (Debian) system, firefox and chromium ship their .desktop files.
- stonesweep 6y agonod and the .desktop file is just an INI-style file that one can hand create for anything at all, the launched app just needs to accept the URL as a parameter input as if typed on the CLI for this scheme.
- zokier 6y agoFile associations are certainly an area where I feel desktop systems in general have failed a bit. Both magic sniffing and and file extensions are very crude ways of managing the associations. Mime type in xattrs (or similar) is slightly better, but does anyone actually use that? And of course they are very platform specific and well hidden in general.
- causality0 6y agoThat may be true in terms of possibility but not relatively. Compare it to the situation on mobile, where you're lucky if you get any choice at all about what programs open which files.
- shawnz 6y agoI don't think storing the mime type separately is better: That requires every system which you might use to transfer or store your documents to support storing the mime type separately as well.
- klodolph 6y agoThis is already how HTTP works. Storing the MIME type separately is definitely a good thing. This isn't the only way you do it. You start by checking the MIME type and then fall back to one of our crude detection mechanisms, like guessing what the file type is based on extension. We already store plenty of metadata with files--permissions and timestamps for starters. The metadata is sometimes lost when you transfer to another system. That's OK... you can repair the metadata. Given that this metadata is already present in the HTTP protocol, it's present in files embedded in email messages, and given that extended attributes are supported by many different filesystems, I think this solution is completely workable. You could make the argument that since we spend so much time interacting with files over HTTP, that having a MIME type in the metadata is actually the common way to do things.
- lmm 6y agoWhat is it that makes the MIME type good and reliable and the file extension crude and unreliable? In my experience it's the opposite - the file extension is much more user-visible, much more likely to be preserved when the file is transferred, and so more reliable than any other way of storing the file type. HTTP and email were overly influenced by the unix tradition in which file extensions are seen as less important, but nowadays the file extension is the most widely supported "extended attribute" that a file might have - why use a less reliable version of the same thing?
- mjsir911 6y agoHere's my attempt at solving the same kind of problem for just URIs, I try to defer to .mailcap for file based auto-opening. Never had much luck with xdg-open https://github.com/mjsir911/browser https://github.com/mjsir911/browser
- vermaden 6y agoI like that (browser). Will definitely dig into it to either use it directly or at least try to implement it in mine see.sh. Also IMHO you should add short 'youtu.be' regex handler :) Regards.
- _jal 6y agoNext time I start thinking it might be time to see if any of the 'desktop' guis have gotten any better, I'll reread this and save myself the time.
- michael-ax 6y agodo that! It will spare you from suffering through the seemingly nowhere-realized-in-full-menu-standard and the awful editors that put lipstick on it.
- ttyprintk 6y agoFor a command-line relative, note that you can write a custom .lessfilter to decide the beginning of your paging pipeline. When syntax-highlighting, be sure to call less with -R.
- vermaden 6y agoThanks - I will check that out.
- madars 6y agoOne should be careful around lesspipe -- many of the tools it invokes by default have less than stellar record of handling untrusted input. https://news.ycombinator.com/item?id=8650952 https://news.ycombinator.com/item?id=8650952
- ttyprintk 6y agoThat’s a good point. I wonder if xdg-open relies on any of the same routines.
- klodolph 6y agoI've run into too many problems with .lessfilter and I've had to disable it. It's hard to write one that works on anything.
- ttyprintk 6y agoLooks like mine relies heavily on pandoc, unoconv, xxd, jq, and OpenSSL. Apparently, some packagers include an enhanced OS customization with high-risk information retrieval tools (see seclists comment). pdftotext seems reasonable and looks to be part of the Debian lessfilter.
- anthk 6y agohttps://git.2f30.org/soap https://git.2f30.org/soap
- kzrdude 6y agoI use xdg-open (alias xo) and it works for me. If file associations are not right, I can set them in the file manager (or in the launcher I use).
- flobosg 6y agoThis reminds me of Plan 9’s plumber: https://9p.io/wiki/plan9/using_plumbing/index.html https://9p.io/wiki/plan9/using_plumbing/index.html
- chmln 6y agoHaving lived through the same frustrations I've released https://github.com/chmln/handlr https://github.com/chmln/handlr a year ago. It comes with lots of nifty scripting tools and significant improvements over xdg-open
- wiredfool 6y agoThe original Mac really had something with the type and creator codes. Applications could register to open a type, I think it would default to the creator. Of course, that wound up with things like resource forks, and that didn't really play well with anyone else's file system.
- Someone 6y agoTypes and creator codes were stored in the file system, not in resource forks. What was stored in a resource fork (of each application) was the declaration “I am [four byte creator code]; I can open these [four byte file types]. That’s the information that the Finder built its databases from (https://www.folklore.org/StoryView.py?project=Macintosh&story=The_Grand_Unified_Model_The_Finder.txt https://www.folklore.org/StoryView.py?project=Macintosh&stor...) That easily could have been moved into the file proper (as AppleSingle (https://en.wikipedia.org/wiki/AppleSingle_and_AppleDouble_formats https://en.wikipedia.org/wiki/AppleSingle_and_AppleDouble_fo...) did, but another (and IMO better for this purpose) option would have been to move that info into a special code segment in the data fork (in Unix parlor; old-style Mac OS programs stored their code in code segments in the resource fork) Also, I think Mac OS still uses https://en.wikipedia.org/wiki/Uniform_Type_Identifier https://en.wikipedia.org/wiki/Uniform_Type_Identifier, which are an improvement over file types and creator codes (you can, for example, express the fact that every html file is a text file with it)
- wiredfool 6y agoYah, details are hazy, it's been a long while since I was truly annoyed at this when it happened.
- malkia 6y agoLol I was frustrated by this with my xfce4, where I could not setup the "Browser" options correctly (had to tweak DPI/scale options for chrome, and this was only doable through command-line). Then I've found out about "exo-open" apart from "xdg-open" and seems like more - https://twitter.com/__malkia__/status/1383898991864147974 https://twitter.com/__malkia__/status/1383898991864147974
- TD-Linux 6y agoIs the OP's install missing a bunch of MIME entries or something? I can't reproduce the examples given for xdg-mime on Fedora 34, e.g. $ xdg-mime query filetype file.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document $ xdg-mime query filetype file.xls application/vnd.ms-excel
- vermaden 6y agoFedora 34 is as bleeding edge desktop as it can be. Maybe you have more recent xdg-utils version? I run 1.1.3 here.
- TD-Linux 6y agoI also have 1.1.3. Maybe what's missing is the libreoffice package? It seems like all these types are provided by /usr/share/mime/packages/libreoffice.xml on my system (which is read by update-mime-types). Arguably this is a common enough format that you shouldn't really need libreoffice installed to have it, though I would think any other office program would have it. I think /etc/mime.types serves as the ultimate fallback, though it doesn't have entries for libreoffice file extensions on my system.
- teddyfrozevelt 6y agoCan't reproduce any of these on Arch either, mine is definitely checking the contents of the file as it saw an empty file titled "file.pdf" as "text/plain" and a .doc as "application/msword".
- psanford 6y agoxdg-open is so frustrating. Its defaults are wrong and its a pain to reconfigure. I replaced xdg-open with this shell script that simply copies the url and posts a desktop notification saying it did so (I got the idea from a post on twitter). I'm much happier to make the decision on my own what to do with external file handlers. #!/bin/sh echo -n "$1" | xclip -selection primary; notify-send "URL Copied" "$1"
- Blikkentrekker 6y agoIt should be noted that the behavior of `echo` in this case is notable not standardized with regards to what should happen if `$1` starts with a dash, and whether it should interpret several special control sequences or not. It is for this reason that `echo` is generally frowned upon for programmatic input, and `printf %s "$1"` is recommended; as this is guaranteed to not interpret, and copy dash-leading strings to the output verbātim.
- geofft 6y agoPretty sure the behavior of echo is standardized on all machines that have notify-send installed.
- yjftsjthsd-h 6y agonotify-send works on Arch Linux, FreeBSD, and Alpine Linux, each of which uses a different echo(1).
- Blikkentrekker 6y agoYou would be wrong as pointed out below. Modern shells also typically have `echo` as a builtin and can have quite different behavior. https://stackoverflow.com/questions/33784892/bash-vs-dash-behavior-with-the-command-echo-ne-hello-n https://stackoverflow.com/questions/33784892/bash-vs-dash-be...
- elcritch 6y agoThe XDG scripts are strange. Some are really simple while some aren't. A while back I wanted "dropbox shortcuts" to work on Linux. So I copied the xdg-open examples and wrote a Nim utility to parse and then open the file [1]. Then it was easy to set KDE File browser to use the program it for dropbox shortcuts. It's nice having a simple language that can compile to a little static binary or be run as a shell script. 1: https://github.com/elcritch/open_on_dropbox https://github.com/elcritch/open_on_dropbox
- deleted 6y ago[deleted]
- liendolucas 6y agoI find vermaden's blog (especially the FreeBSD series) extremely useful and informative. Thanks for this and please keep it up this way!
- vermaden 6y agoThanks, I intend to. Any topics You would like to be covered?
- ximm 6y agoFor anyone proposing a replacement for xdg-open: It is already a wrapper around kde-open, dde-open, gvfs-open, gnome-open, mate-open, and exo-open. We already have a classic xkcd 927 situation.
- yrro 6y agoOP is missing definitions that associate *.docx files, etc., with the appropriate MIME type. The Garbage In, Garbage Out principal applies here. For anyone interested about how the plumbing, here are the specs: shared-mime-info-spec[0] is for mapping glob patterns, magic numbers, string fragments, etc. to MIME types; and desktop-entry-spec[1] is for defining what an application is, its icon, how to launch it, what MIME types it handles, etc. [0] https://freedesktop.org/wiki/Specifications/shared-mime-info-spec/ https://freedesktop.org/wiki/Specifications/shared-mime-info... [1] https://www.freedesktop.org/wiki/Specifications/desktop-entry-spec/ https://www.freedesktop.org/wiki/Specifications/desktop-entr... On my (Debian) system, the following packages provide the definitions that map *.docx files to the appropriate MIME type: $ grep -l '\*.docx' /usr/share/mime/packages/*.xml | xargs dpkg -S shared-mime-info: /usr/share/mime/packages/freedesktop.org.xml libreoffice-common: /usr/share/mime/packages/libreoffice.xml ... and subsequently: $ xdg-mime query filetype Downloads/something.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document $ xdg-mime query default application/vnd.openxmlformats-officedocument.wordprocessingml.document libreoffice-writer.desktop*
- bandie91 6y ago(self-promotion) https://github.com/bAndie91/mimeopen-gui https://github.com/bAndie91/mimeopen-gui it shows a dialog with all the associated application to the user to choose from