I will start by quoting the EXAMPLES section in man xdg-mime
EXAMPLES
xdg-mime install shinythings-shiny.xml
Adds a file type description for "shiny"-files. "shinythings-" is used as the vendor prefix. The file type description could look as follows.
shinythings-shiny.xml:
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="text/x-shiny">
<comment>Shiny new file type</comment>
<glob pattern="*.shiny"/>
<glob pattern="*.shi"/>
</mime-type>
</mime-info>
An icon for this new file type must also be installed, for example with:
xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text-x-shiny
I wish to understand both adding this mime association for 1) my user
account as well as 2) system-wide.
From the same man
page
xdg-mime install [--mode mode] [--novendor] mimetypes-file
--mode mode
mode can be user or system. In user mode the file is (un)installed for the current user only. In system mode the file is (un)installed for all users on the system. Usually only root is allowed to install in system mode.
The default is to use system mode when called by root and to use user mode when called by a non-root user.
when including glob-deleteall in a user-local definition, it removes
any existing system-wide mime associations
Yes, glob-deleteall is used to overwrite the glob part of a mimetype definition but not only system-wide. Both depending on the mode
mentions the specific directories where things should be located on an Arch Linux system
system mode would install to /usr/share/mime/
. user mode to .local/share/mime
and the file list is:
./.local/share/mime/generic-icons
./.local/share/mime/mime.cache
./.local/share/mime/types
./.local/share/mime/text
./.local/share/mime/text/x-shiny.xml
./.local/share/mime/version
./.local/share/mime/treemagic
./.local/share/mime/globs
./.local/share/mime/globs2
./.local/share/mime/aliases
./.local/share/mime/subclasses
./.local/share/mime/magic
./.local/share/mime/icons
./.local/share/mime/XMLnamespaces
./.local/share/mime/packages/shinythings-shiny.xml
After all, run
update-mime-database ~/.local/share/mime/
to activate the configuartion.
The shared-mime-info
repository
already specifies the application/pgp-keys
mimetype.
You can see it here:
<mime-type type="application/pgp-keys">
<comment>PGP keys</comment>
<acronym>PGP</acronym>
<expanded-acronym>Pretty Good Privacy</expanded-acronym>
<sub-class-of type="text/plain"/>
<generic-icon name="text-x-generic"/>
<magic priority="50">
<match type="string" value="-----BEGIN PGP PUBLIC KEY BLOCK-----" offset="0"/>
<match type="string" value="-----BEGIN PGP PRIVATE KEY BLOCK-----" offset="0"/>
<match type="big16" value="0x9501" offset="0"/>
<match type="big16" value="0x9500" offset="0"/>
<match type="big16" value="0x9900" offset="0"/>
<match type="big16" value="0x9901" offset="0"/>
</magic>
<glob pattern="*.skr"/>
<glob pattern="*.pkr"/>
<glob pattern="*.asc" weight="10"/>
<glob pattern="*.pgp"/>
<glob pattern="*.gpg"/>
<glob pattern="*.key"/>
</mime-type>
https://gitlab.freedesktop.org/xdg/shared-mime-info/-/blob/6bf9e4ff0fb7eff11a02bd937045bf5dc291841a/data/freedesktop.org.xml.in#L282
or here on your own machine:
/usr/share/mime/packages/freedesktop.org.xml
However, it does not use the *.pub
glob pattern,
probably to avoid conflicts with MS Publisher format.
One workaround is just to rename the files as e.g. *.asc
files.
But let's continue on, assuming that renaming the files is not an option.
Here is the mimeinfo file we need
(note that it must be named pgp-keys.xml
):
$ cat pgp-keys.xml
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="application/pgp-keys">
<comment>PGP keys</comment>
<acronym>PGP</acronym>
<expanded-acronym>Pretty Good Privacy</expanded-acronym>
<sub-class-of type="text/plain"/>
<generic-icon name="text-x-generic"/>
<magic priority="10">
<match value="-----BEGIN PGP PUBLIC KEY BLOCK-----" type="string" offset="0"/>
</magic>
<glob weight="10" pattern="*.pub"/>
</mime-type>
</mime-info>
The advantage of the "magic" part
is that it will look at the beginning of the file for this string,
then determine the mimetype based on whether or not it matches.
This means that files with mimetype application/vnd.ms-publisher
can still have the .pub
file extension
and live in relative harmony
alongside public keys that also have the .pub
file extension.
To achieve this, we must install the mimeinfo file.
To install it for a single user:
xdg-mime install --mode user pgp-keys.xml
update-mime-database ~/.local/share/mime
To install it system-wide:
sudo xdg-mime install --mode system pgp-keys.xml
sudo /usr/bin/update-mime-database /usr/share/mime
I've tested the outcome with an example public key from here:
https://www.intel.com/content/www/us/en/security-center/pgp-public-key.html
and an example MS Publisher file from here:
https://github.com/apache/tika/blob/0bf11aec86079b8f1ae2f1ea680910ba79665c4f/tika-parsers/src/test/resources/test-documents/testPUBLISHER.pub
You can try it yourself with the git repository here:
https://github.com/nbeaver/custom-pub-file-mimetype
Best Answer
Introduction
When a file is opened with an application, the file is just passed to the application as an argument.
So when you open firefox-nightly located in
/bin/
withp.html
located in/home/user/
, it's basically similar to run/bin/firefox-nightly /home/user/p.html
.Creating an Executable
As mentioned in the question:
Let us create a Ruby program as asked by the OP that will copy the contents of a file passed as an argument to
/tmp/tempfile-#{n}
. Note that any programming language will work if it can accept command line arguments.And let's call our program copycat.rb, and move it to /tmp/ directory.
We can surely run the program on a terminal like this:
A this will copy all the contents of
/tmp/AFile
to/tmp/tempfile-#{n}
Where#{n}
is the count in case any duplicate tempfile exists.Creating an Application Entry
Now to open this with our program from the file manager, we need to create an Application entry. To do that, we have 2 options. The first options is:
Create a file called copycat.desktop in
$HOME/.local/share/applications
, with the following content:Don't forget to add the
%u
option to the line starts with Exec.Testing
Well, to test, let's create a simple file called with the content 'hello world' or anything you want. Open your file manager, and click your secondary mouse button on the file, and select "Open With" or similar option. Because it's GUI related, I will add some sample pictures.
Nautilus, "Open With Other Application":
Nautilus, "View All Applications":
"CopyCat":
When done, you can see the
tempfile-#{n}
created in/tmp/
The file manager I used here is Nautilus, but this should work with other file managers as well, just the text might differ.
The second option is to make the application available for all users. To do that, you have to move the file from
$HOME/.local/share/applications/copycat.desktop
to/usr/share/applications
and change the ownership to root.This is how the open with a custom executable works in Linux. A similar GUI app can be created and opened in the same way.