How Do I Create Mime Types For New Documents (Command Line)?

How do I give my application's custom document a custom icon? Additionally, how do I make the file manager open these documents when a user double clicks on them?

I have to do this on the command line because the solution will be used inside an installation script.


The correct solution can be found --> here <-- , but a foo version is provided below to directly answer the question.

Please pay very close attention to the use of application-x-foo, x-foo, and foo as they change throughout.

Note: When updating the Ubuntu tutorial, it's probably better in an example to distinguish between the foo application, foo the extension and foo mimetype to avoid confusion moving forward. i.e. lmms uses *.mmpz and x-lmms-project, which helped a lot in figuring this out.

  • Get a scalable SVG icon (example provided)
  • wget http://soqr.fr/testsvg/zebra.svg -O ~/Desktop/application-x-foo.svg
    

  • Create a mime-ingest file in XML format:
  • <?xml version="1.0" encoding="UTF-8"?>
    <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
    <mime-type type="application/x-foo">  
    <comment>Foo File</comment>
    <glob pattern="*.foo"/>
    </mime-type>
    </mime-info>
    

  • Save this to ~/Desktop/x-foo.xml
  • If not already, create a desktop file:
  • [Desktop Entry]
    Version=1.0.0
    Encoding=UTF-8
    Name=FooGazi
    GenericName=Foo Application
    Comment=This is my Foo Application
    Exec=/usr/bin/foo
    # Please change, system-shutdown.svg used only as example
    Icon=/usr/share/icons/Humanity/actions/48/system-shutdown.svg
    StartupNotify=true
    Type=Application
    MimeType=application/x-foo;
    

  • Save this to ~/Desktop/foo.desktop

  • Deploy the mime description
  • sudo xdg-mime install --mode system ~/Desktop/x-foo.xml
    

  • Deploy the application launcher
  • sudo cp ~/Desktop/foo.desktop /usr/share/applications
    

  • Deploy the SVG icon
  • sudo cp ~/Desktop/application-x-foo.svg /usr/share/icons/gnome/scalable/mimetypes/
    

  • Register the application launcher with the registered mimetype
  • sudo xdg-mime default foo.desktop application/x-foo
    

  • Update the mime cache
  • sudo update-mime-database /usr/share/mime
    

  • Update the icon cache
  • sudo gtk-update-icon-cache /usr/share/icons/gnome/ -f
    

  • As a habit, I also add the appropriate line to /etc/mime.types but please confirm whether or not this is still needed and edit this post to reflect that.
  • Log off and then back in. Both the icon and the association should work.

  • Never, ever, edit existing files in /etc from you install script! That is a terribly bad idea. besides you don't need to edit /etc/mime.types

    your xml file is still wrong, and in the wrong location it should be /usr/share/mime/text/x-foo.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/x-foo">
        <comment>Example file type </comment>
        <glob pattern="*.foo"/>
    </mime-type>
    

    3rd party Icons are normally installed to: /usr/share/icons/hicolor/scalable/mimetypes