61

I'm developing a Linux application that has its own file format. I want my app to open when you double-click on those files.

How can I register a file extension and associate it with my application on Linux? I'm looking for a way that is standard (works with GNOME and KDE based systems) and can be done automatic when my program is installed or run for the first time.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
amarillion
  • 24,487
  • 15
  • 68
  • 80

5 Answers5

56

Use xdg-utils from freedesktop.org Portland.

Register the icon for the MIME type:

xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype

Create a configuration file (freedesktop Shared MIME documentation):

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="application/x-mytype">
    <comment>A witty comment</comment>
    <comment xml:lang="it">Uno Commento</comment>
    <glob pattern="*.myapp"/>
  </mime-type>
</mime-info>

Install the configuration file:

xdg-mime install mytype-mime.xml

This gets your files recognized and associated with an icon. xdg-mime default can be used for associating an application with the MIME type after you get a .desktop file installed.

Tanja
  • 41
  • 2
  • 3
  • 11
skolima
  • 31,963
  • 27
  • 115
  • 151
  • 1
    Ubuntu 9.04 & Fedora Core 10 have the xdg utilites installed by default – jldupont Oct 21 '09 at 01:23
  • 1
    FYI: In terms of the XML example given, `xdg-icon-resource` should be invoked with `application-x-mytype` *not* `x-application-mytype` as written. – KJ7LNW Mar 22 '22 at 03:07
10

There are two parts to this. You need to register a new file type and then create a desktop entry for your application. The desktop entry associates your application with your new mime type.

I thought that both Gnome and KDE (maybe only 4+?) used the freedesktop shared mime info spec, but I may well be wrong.

Kai
  • 5,260
  • 5
  • 29
  • 36
4

1) in linux this is a function of your desktop environment rather than the os itself.
2) GNOME and KDE have different methods to accomplish this.
3) There's nothing stopping you from doing it both ways.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

Try this script: needs:

1. your application icon -> $APP               = FIREFOX.png 
2. your mimetype icon    -> application-x-$APP = HTML.png

in the current directory:


#BASH SCRIPT: Register_my_new_app_and_its_extension.sh
APP="FOO"
EXT="BAR"
COMMENT="$APP's data file"

# Create directories if missing
mkdir -p ~/.local/share/mime/packages
mkdir -p ~/.local/share/applications

# Create mime xml 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">
    <mime-type type=\"application/x-$APP\">
        <comment>$COMMENT</comment>
        <icon name=\"application-x-$APP\"/>
        <glob pattern=\"*.$EXT\"/>
    </mime-type>
</mime-info>" > ~/.local/share/mime/packages/application-x-$APP.xml

# Create application desktop
echo "[Desktop Entry]
Name=$APP
Exec=/usr/bin/$APP %U
MimeType=application/x-$APP
Icon=$APP
Terminal=false
Type=Application
Categories=
Comment=
"> ~/.local/share/applications/$APP.desktop

# update databases for both application and mime
update-desktop-database ~/.local/share/applications
update-mime-database    ~/.local/share/mime

# copy associated icons to pixmaps
cp $APP.png                ~/.local/share/pixmaps
cp application-x-$APP.png  ~/.local/share/pixmaps

make sure: FOO binary is there in /usr/bin (or in $PATH)

fastrizwaan
  • 190
  • 1
  • 3
0

This is all existing answers combined, completed and corrected into a single bash script.

#!/bin/bash
set -e # stop on error

APP=my-app
EXT=my-app
COMMENT=Comment
EXEC=/usr/bin/my-app
LOGO=./logo.png

xdg-icon-resource install --context mimetypes --size 48 $LOGO application-x-$APP

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">
    <mime-type type=\"application/x-$APP\">
        <comment>$COMMENT</comment>
        <icon name=\"application-x-$APP\"/>
        <glob pattern=\"*.$EXT\"/>
    </mime-type>
</mime-info>" > $APP-mime.xml

xdg-mime install $APP-mime.xml
rm $APP-mime.xml
update-mime-database $HOME/.local/share/mime

echo "[Desktop Entry]
Name=$APP
Exec=$EXEC %U
MimeType=application/x-$APP
Icon=application-x-$APP
Terminal=false
Type=Application
Categories=
Comment=$COMMENT
"> $APP.desktop
desktop-file-install --dir=$HOME/.local/share/applications $APP.desktop
rm $APP.desktop
update-desktop-database $HOME/.local/share/applications

xdg-mime default $APP.desktop application/x-$APP

Note that this solution is for local user installation. This is how you'd do it for system-wide installation:

set -e
install -D -m 0755 $APP /usr/bin/$APP
install -D -m 0644 assets/$APP.png /usr/share/icons/hicolor/48x48/mimetypes/application-x-$APP.png
install -D -m 0644 assets/$APP-mime.xml /usr/share/mime/packages/
install -D -m 0644 assets/$APP.desktop /usr/share/applications/

umask 0022
# Depending on your distribution, also run:
update-mime-database /usr/share/mime
update-desktop-database /usr/share/applications
gtk-update-icon-cache -f -t /usr/share/icons/hicolor
phil294
  • 10,038
  • 8
  • 65
  • 98