Adding A Custom 'Open With' Program In Ubuntu 20.04
2021-05-09 - By Robert Elder
This article is a quick summary of how to add a custom 'Open With' program that can be associated with files of a certain type in Ubuntu 20.04. In particular, we'll review an example of how you can get files to automatically open with a AppImage executable of your choosing.
I decided to write an article on this subject because the last few times that I tried to complete this seemingly simple task, it has taken me at least a couple hours due to forgetting numerous odd details and having to piece it together again from google searches. My overall goal was to get '.kdenlive' project files to open with the Kdenlive AppImage.
Step 1) Locate The '.desktop' Directory
In order for your application to show up in the 'Open With' list, you need to add a '.desktop' entry for it. To do this, you'll first need to figure out which directory to put it in. Most likely, you'll choose one of the following two folders:
- Inside the global folder for all users at '/usr/share/applications/'
- Inside a specific user's folder at '/home/THE_USER/.local/share/applications/'
In my case, I'm going to use the '/usr/share/applications/' directory to make this option globally accessible. I can also see that in this directory, there are already tons of files that end in '.desktop', so you may be able to use them as a reference. If you're reading this article in a far distant future where the folder has been changed, or you're on a different system other than Ubuntu with different paths, search for a folder on your system with lots of '*.desktop' entries in it.
Step 2) Add The New Desktop Entry
For the '.desktop' entry, I'm going to name it 'my_custom_thingy.desktop', but you can name it whatever you want as long as it doesn't clash with another existing entry and it ends with '.desktop'. Now, let's add an entry:
sudo vi /usr/share/applications/my_custom_thingy.desktop
And here is what we'll put inside of the file 'my_custom_thingy.desktop':
[Desktop Entry]
Exec=/usr/bin/gedit %f
Name=My Custom Thingy
Terminal=false
Type=Application
The above example will cause files to be opened in 'gedit'. Simply change '/usr/bin/gedit' to the path of the executable that you want to open files with. The text 'My Custom Thingy' is whatever you want the name in the 'Open With' list to be. The '%f' will become the one or more filenames that will be passed to the program. If you use '%F', that represents only a single filename. The format strings and desktop entry names are all documented on this page title 'Desktop Entry Specification'.
Doing the above is enough to make the item appear as an option in 'Open With' on Ubuntu 20.04 without refreshing/relogging or running anything else as it appears to get picked up automatically. I just tested these instructions with a test file called 'something.abc' that contained only the text 'Hello World!'. Immediately after adding the .desktop file above I was able to right-click on my 'something.abc' file and get it to open with 'My Custom Thingy' and it opened in gedit.
Step 3) Setting Up A Custom MIME Type (Optional)
One disadvantage of the approach shown above is that after the first time you open your file this way, it'll keep the 'Open With' option that you selected as the default option for all files of that 'type'. If your system identified the 'file type' as something very general, like 'text files' or 'binary files', then every time you try to open one of those general file types, it'll use the program you last associated with it. In order to get a better solution that's targeted at specific file formats/extensions, you need set up a custom 'MIME type' Adding A Custom MIME Type & Icon In Ubuntu 20.04.
Troubleshooting
Here are some of the things that have caused the new 'Open With' to not show up, or otherwise caused me to waste a lot of time on this task:
- Not realizing that I spelled 'Exec' wrong.
- Forgetting to make the target program executable (this prevents it from showing up in the list).
- Not including the '%f' part which actually passes the name of the file being opened.
- Re-logging in/restarting multiple times because I thought that was necessary, only to later find that I had a typo in the file.
- Spending a lot of time running 'sudo update-desktop-database' and reading bug reports on this program (it has some weird bugs that require environment flags to be set or something? idk...) because I thought it was necessary to get the entry to show up. For this use case, it wasn't necessary. Maybe it was necessary in the past? Maybe it's only necessary if MIME types are involved? Not sure.
During my google searches, I read that there is a 'correct' way to perform this task which makes use of this program:
desktop-file-install
I never tried it though, and nothing bad happened.
Here is some official Ubuntu documentation on Desktop Files.
Open With Kdenlive Appimage
Now, let's finally get to the task that I actually wanted to perform before writing this article: Setting up an 'Open With' option that I can use for .kdenlive project files to open them up directly using the Kdenlive AppImage. Here is the desktop entry I used:
[Desktop Entry]
Exec=/home/robert/kdenlive-21.04.0-x86_64.appimage %f
Name=Kdenlive 21.04 Appimage
Terminal=false
Type=Application
Using the above desktop entry, I just need to add it to the required location:
sudo vi /usr/share/applications/my_custom_kdenlive_opener.desktop
and the new 'Open With' association is now available to use without relogging or restarting.
Open With Kdenlive Shell Script Wrapper & Logging
Ok, here is the real reason that I'm writing this article: What I actually wanted to be able to open Kdenlive automatically by clicking on project files, but I wanted to do it indirectly through a shell script. The reason being, that Kdenlive crashes sometimes, and when that happens I want to be able to go back and look at the logs to see what happened before the crash.
Here is the .desktop entry that I used for this:
[Desktop Entry]
Exec=/home/robert/kdenlive_hook.sh %f
Name=Kdenlive Logged Hook
Terminal=false
Type=Application
And here is the 'kdenlive_hook.sh' wrapper script that launches kdenlive, but also sets up some log files in the /tmp directory:
#!/bin/bash
cd $(dirname "${1}")
env > "/tmp/kdenlive_environment_$(date).log" 2>&1
/home/robert/kdenlive-21.04.0-x86_64.appimage "${1}" > "/tmp/kdenlive_output_$(date).log" 2>&1
Once I put the above script into '~/kdenlive_hook.sh', I'll be able to log everything that happens while I'm using kdenlive, and then when it crashes, I'll be able to submit high-quality bug reports!
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
$1.00 CAD |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Why Is It so Hard to Detect Keyup Event on Linux?
Published 2019-01-10 |
The Most Confusing Grep Mistakes I've Ever Made
Published 2020-11-02 |
Use The 'tail' Command To Monitor Everything
Published 2021-04-08 |
An Overview of How to Do Everything with Raspberry Pi Cameras
Published 2019-05-28 |
An Introduction To Data Science On The Linux Command Line
Published 2019-10-16 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|