The default open program of associated files under Linux

Sometimes, we need to customize a file type and then open it with an application written by ourselves. This operation can be set through the registry on windows. How to achieve it on Linux? This article tells how to set the file on Linux The program is opened by default.

Since there are many Linux distributions, the functions in this area are similar, and they are all implemented through mime types.

1. Create the file /usr/share/mime/packages/your-xyz-mime.xml

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-xyz">
    <comment>Your XYZ File</comment>
    <glob pattern="*.xyz"/>
  </mime-type>
</mime-info>

2. Create the application /usr/share/applications/open_xyz.desktop

[Desktop Entry]
Name=open_xyz
Comment=open xyz type file
Exec=open_xyz %u
Icon=/opt/open_xyz/icon.png
Terminal=false
Type=Application
MimeType=application/x-xyz;
Categories=GNOME;GTK;Network;RemoteAccess;
StartupNotify=true

3. Settings

update-mime-database /usr/share/mime
update-desktop-database /usr/share/applications/

4. Test

Create a.xyz

[xyz]
a=1
b=2

Save file
test 1: Execute: gio mime application/x-xyz

# gio mime application/x-xyz
Default application for “application/x-xyz”: open_xyz.desktop
Registered applications:
    open_xyz.desktop
Recommended applications:
    open_xyz.desktop

Test 2: Double-click a.xyz to open and see if it is opened by open_xyz.
Test 3: Right-click a.xyz->Properties to check the default open program

Note: The open_xyz program is best placed in: /usr/bin directory

Guess you like

Origin blog.51cto.com/14207158/2561958