Ubuntu – How to register a shell script as the main application for a given file extension

file associationfile format

I have a shell script "the_script" that I would like to register as the default application for the files with the extension ".ext". For this purpose, I have written an xml file to register the mime type, and a .desktop file to associate the application with the mime type. The script has the executable bit for the user and is available in the user's path.

The xml file, named "the_script.xml" is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/x-the-script">
        <comment>A file for the script</comment>
        <glob pattern="*.ext"/>
    </mime-type>
</mime-info>

Then I ran xdg-mime install --novendor the_script.xml.

The .desktop file, named "the_script.desktop" is as follows:

[Desktop Entry]
Version=1.0

Type=Application
MimeType=application/x-the-script

Name=The script that manages .ext files

Comment=Interpreter for .ext files

TryExec=the_script
Exec=the_script %f
Terminal=false

And I ran xdg-desktop-menu install --novendor the_script.desktop to register the file.

Once that both files are registered, the .ext files do not open with the text editor any more but Gnome tells me that there is no installed application for the files "A file for the script".

I have successfully registered other file formats with other applications. The other applications were compiled programs while the_script is a shell script. That is the only difference I can see. How can I register my script with the .ext files?

Best Answer

This seems to work fine from a terminal if you do xdg-open foo.ext, however it doesn't seem to work in Nautilus unless you define your script as default for opening the *.ext file:

  • Select such a file,
  • Right-click,
  • Choose Properties,
  • Select the Open With tab,
  • Click on Show other applications,
  • Select your script (should be The script that manages .ext files),
  • Click Set as default, and finally press
  • Close.

After I did that, the script started from Nautilus.