Linux: File association and preferred application by extension

Like magic numbers, extensions are often used to indicate what a file is about. However, orthodox Linux thinking is that files are defined as either binary or text, and, if binary, then the file type is defined by its magic number.

This thinking falls flat when we use a .zip format to define a .3mf 3D definition file and Linux insists on treating it as an archive file opened by an archive viewer rather than as a 3D file opened by a 3D slicing program. If the file becomes associated with the slicing program by default, then all .zip files will subsequently get opened by the slicing program rather than by the archive viewer.

Because orthodox thinking dominates the Linux world, the ability to make such distinctions exists, but it requires rolling up your sleeves and navigating the Linux file structure.

Here is the way to distinguish .3mf files from .zip files and give them their own preferred application (Windows-style). The example will cread a new mime type associated with files of extension .3mf and assign a preferred application called slic3r to them.

Associate applications with a new MIME type

Create a new MIME type for files with extension 3mf or 3MF

For individual use, work in the directory ~/.local/share/mime/packages/ using your own default permissions.

To make this change globally for all users, work in the directory /usr/share/mime/packages/ using super-user permissions.

Create a new file called 3mf.xml with the following contents

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="application/3mf">
    <comment>3mf 3D zip archive file</comment>
    <glob pattern="*.3MF"/>
    <glob pattern="*.3mf"/>
  </mime-type>
</mime-info>

Update the mime database (use the global directory if setting globally for all users)

update-mime-database ~/.local/share/mime

Create a new desktop definition for the default application

For individual use, work in the directory ~/.local/share/mime/applications/ using your own default permissions.

To make this change globally for all users, work in the directory /usr/share/mime/applications/ using super-user permissions.

Create a new file called slic3r.desktop with the following contents

Terminal=false
Name=Slic3r
GenericName=3D Slicer Software
Encoding=UTF-8
Version=1.0
Exec=slic3r %F
Type=Application
Categories=Graphics;3DGraphics;Engineering;
NoDisplay=false
Keywords=3D;Printing;Slicer;slice;3D;printer;convert;gcode;stl;obj;amf;SLA
StartupNotify=false
MimeType=application/3mf;application/obj;model/stl
PrefersNonDefaultGPU=false
Icon=/opt/slic3r/slic3r.png
Comment=Custom definition for slic3r
PrefersNonDefaultGPU=false

Update the desktop database (use the global directory if setting globally for all users)

update-desktop-database ~/.local/share/applications
8 / 2024