How to use the xdg-mime command

Viewed 30194

Can someone provide example on how to use (with GNOME Ubuntu) the XDG-MIME command? I'm struggling to get anything working even with the docs.

For example if I want to register the extension .mfe with an application called MyApp what would the steps be? This is my attempt so far, I would appreciate any pointers on getting this right...

This is my xml (MyApp-MyFileType.xml) How is the file name relavent?

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="text/mfe">
    <comment>File for MyApp</comment>
    <glob pattern="*.mfe"/>
  </mime-type>
</mime-info>

Then is run this command?

xdg-mime install MyApp-MyFileType.xml

And then I run this? What does the xxx relate to?, I understand it to be an identifier to my application but how do I define it?

xdg-mime default xxx.desktop text/mfe
3 Answers

Adding a applied case, for composed filenames, since xdg-mime default was not working for me in this particular case.


The goal: associate all files *.mybot.json with a special dedicated script mybot, while retaining the default behavior of a regular .json file (Eq: dropping in Firefox will still recognize the files as .json, but clicking them will open the pre defined mybot script)


✅ Create mybot.xml:

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="application/mybot">
    <comment>My Bot json data</comment>
    <glob pattern="*.mybot.json"/>
  </mime-type>
</mime-info>

✅ Create mybot.desktop, and test it:

Clicking it should launch the script called mybot in /home/bots/.

ⓘ The target script mybot must be set executable and be a hashbang shell script, with the lang interpreter set on the first line, because launched by bash.

ⓘ We can use one of the numerous icon existing in /usr/share/icons.

[Desktop Entry]
Version=1.0
Name=mybot
Exec=bash -c '/home/bots/mybot;/bin/bash'
Icon=utilities-terminal
Terminal=true
Type=Application
Categories=Development;

⚠️ It is important to click it one time and mark it as trusted. The icon will change after that.


✅ Mime install on the machine, files explorer will then start to recognize *.mybot.json files as My Bot json data (application/mybot).

sudo xdg-mime install mybot.xml

✅ Then, simply copy the .desktop launcher in /usr/share/applications:

sudo cp mybot.desktop /usr/share/applications

Now as example, a file *.mybot.json downloaded from your website or coming by email can be quickly opened with a given script on the local machine directly from the browser. It allows to create a wild ecosystem of automations!

Related