mimeo

2024-05-17 22:37 UTC
  • Xyne

Metadata

Description: Open files by MIME-type or file name using regular expressions.
Latest Version: 2023
Source Code: src/
Architecture:
  • any
Dependencies:
  • file
  • python-pyxdg
  • python3
Build Dependencies:
  • python-setuptools
Optional Dependencies:
  • shared-mime-info: for recognizing more MIME-types
Arch Repositories:
  • [xyne-any]
  • [xyne-i686]
  • [xyne-x86_64]
AUR Page: mimeo
Arch Forum Thread: 86855
Tags:

About

Mimeo uses MIME-type file associations to determine which application should be used to open a file. It can launch files or print information such as the command that it would use, the detected MIME-type, etc. It is also possible to use regular expressions to associate arguments with applications. The most common example is to open URLs in browsers or associate file extensions with applications irrespective of their MIME-type.

Mimeo tries to adhere to the relevant standards on freedesktop.org and should therefore be compatible with other applications that set or read MIME-type associations, e.g. PCManFM.

Please notify the author of any non-standard behavior so that it can be corrected.

See mimeo --help for options and usage information.

Usage

To open some files foo, bar and /somewhere/else/baz just run

mimeo foo bar baz /somewhere/else/baz

To suppress output on the terminal in which mimeo is run, use the -q option.

Editing MIME-types

Mimeo is able to update MIME-type associations (add, remove, clear) and configure defaults. It can also update MIME-cache files and check for invalid desktop entries. See “mimeo –help” for more information.

Examples

Associate shell scripts with Leafpad.

test is an extensionless Bash script.

# find Leafpad's desktop file
> mimeo --app2desk leafpad
leafpad
  leafpad.desktop

# determine "test" file's MIME-type
> mimeo -m test
test
  application/x-shellscript

# associate "application/x-shellscript" with Leafpad
> mimeo --add application/x-shellscript leafpad.desktop

Associate all text files with Medit

> mimeo --app2desk medit
medit
  medit.desktop

> mimeo --add 'glob:text/*' medit.desktop

Association File Examples

These are just a few quick examples of how to create associations using the associations file. See “mimeo –assoc-help” for more information.

Open YouTube videos in VLC:

vlc --one-instance --playlist-enqueue %U
  ^https?://(www.)?youtube.com/watch\?.*v=

Open HTTP(S) URLs with Firefox:

/usr/bin/firefox %U
  ^https?://

Note that the order of matching in the file is important and that the YouTube video URL matcher must come before the general HTTP(S) one, otherwise YouTube links would be opened in Firefox.

Recognize Perl (.pl), Python (.py), Haskell (.hs) files by extension and open them with medit.

/usr/bin/medit %F
  \.p[ly]%
  \.hs%

Open various media files in VLC by extension:

/usr/bin/vlc --one-instance --playlist-enqueue %F
  \.mp3$
  \.flac$
  \.avi$
  \.mpg$
  \.flv$

Help Message

$ mimeo --help

usage: Mimeo [options] [<arg> ...]

Open files using MIME-type and custom user associations.

positional arguments:
  <arg>

options:
  -h, --help            show this help message and exit

Query Operations:
  Operations to obtain information.

  --assoc-help          Display information about the custom associations
                        file.
  --mimeman-help        Display information about managing MIME-type
                        associations.
  --filepath-help       Display information about configuration and data
                        filepaths.
  -c, --command         Print the full command(s) and exit.
  -d, --desktop         Print the associated desktop file names and paths and
                        exit.
  -m, --mimetype        Print the detected MIME-type(s) for the given
                        arguments and exit. The arguments may be paths, Mimeo
                        MIME-type matchers, or URIs. If no arguments are
                        given, print all known MIME-types.
  --finddesk            Return the paths to the given desktops if they exist.
  --mime2desk           List desktop files associated with the given MIME-
                        types.
  --app2desk            List desktop files that use the given executables and
                        exit. If no arguments are given then the executables
                        of every desktop file will be listed.
  --app2mime            List MIME-types that the given executables are
                        associated with in their desktop files and exit. If no
                        application is given, all applications will be listed.
  --desk2field <desktop entry field>
                        List the values of a desktop entry field per desktop,
                        e.g. "Exec" or "MimeType".
  --mimeapps-list       Print the paths to detected mimeapps.list files.

Modification Operations:
  Operations to change associations and preferences. If no MIME-type matcher
  is given then the MIME-types in the desktop files will be used.

  --update              Update associations and cache files.
  --add <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Associate MIME-types with desktop files. See "--
                        mimeman-help" for more information.
  --unadd <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Undo an --add operation.
  --remove <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Remove associations by adding them to the "Removed
                        Associations" section of mimeapps.list. This can
                        effectively hide system-associations from the user.
                        This does not affect default. Use "--clear" to forget
                        a user-association. See "--mimeman-help" for more
                        information.
  --unremove <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Undo a --remove operation.
  --prefer <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Set a default association. See "--mimeman-help" for
                        more information.
  --unprefer <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Undo a --prefer operation.
  --clear <MIME-type matcher | filepath | desktop file> [<desktop file> ...]
                        Clear associations. If no desktop files are specified,
                        all associations for the MIME-type(s) will be cleared.
                        To clear all associations for a desktop file, use "--
                        clear 'glob:*' <desktop file>". This affects defaults.
                        See "--mimeman-help" for more information.
  --create <filename> <Name> <Exec> <MIME-type matcher or ""> <"term" or "noterm">
                        Create a minimal desktop file. Edit the created file
                        if necessary. If an empty string is passed instead of
                        a MIME-type matcher, the file will not specify any
                        MIME-type associations. The fifth argument indicates
                        if "Terminal" should be set to "true" in the created
                        file. See "--mimeman-help" for more information. The
                        created files are saved in $XDG_DATA_HOME/applications
                        (~/.local/share/applications by default).

Configuration:
  Various configuration options.

  -a <filepath>, --assoc <filepath>
                        Specify a file that associates regular expressions
                        with custom commands. This can be used for opening
                        URLs, for example. See "--assoc-help" for details. See
                        --filepath-help for default paths.
  --no-assoc            Do not use the default associations file.
  --no-def-args         Omit the default arguments.
  --user                Restrict operations to user files.
  --system              Restrict operations to system files. This is mostly
                        useful for updating system desktop files and MIME
                        information as root.
  -q, --quiet           Suppress all output from launched applications.
  --term <cmd>          Terminal command to use when launching applications
                        with desktop files that specify "Terminal=true". It
                        will be split into words using shlex.split. A word
                        equal to "%s" will be replaced by the separate words
                        of the application command. A word equal to "'%s'"
                        will be replaced by a single word containing the
                        joined words of the application command. Any other
                        instance of "%s" within a word will be replaced by the
                        joined words of the application command. If "%s" does
                        not appear within the terminal command then the
                        separate words of the application command will be
                        appended to the end of the command. Examples: "urxvt
                        -e", "urxvt -e %s", "xterm -e bash -c '%s'". A literal
                        "%" may be escaped with "%%". Use the default
                        arguments file or a shell alias to automatically pass
                        this argument.
  --exec                Run the Exec fields from the given desktop files.
  --cmd-prefix <cmd> [<arg> ...]
                        Prefix commands and arguments to the desktop commands.
                        For example, this can be used to run commands with
                        sudo, gksudo, etc. while still running $(prog) as a
                        regular user.
  --by-content-only     Determine MIME-type of files from the content only.
  --by-content-first    Check file content before name when determining MIME-
                        type.
  --by-name-only        Determine MIME-type of files from the name only.
  --no-follow           Do not follow symlinks.
  --deprecated          Use deprecated directories. See --filepath-help for
                        details.
  --current-desktop     Modify associations of the current desktop as
                        specified in $XDG_CURRENT_DESKTOP. Ignored if
                        $XDG_CURRENT_DESKTOP is not set.
  --debug               Enable debugging messages.
  --full-path           Return full paths to desktop files for some outputs..
  --show-all            For some output, show all possibilities rather than
                        just the first. This can be used with --command for
                        example.
  --swap                Swap the way displayed information is organized, e.g.
                        display input arguments per MIME-type instead of MIME-
                        types per input argument with --mimetype. This does
                        not work for all query operations.

If no operation is specified, the commands determined by "--command" will be
run, i.e. the passed arguments will be opened. See --filepath-help for further
configuration options such as passing default arguments.

Mimeo Association File Message

$ mimeo --assoc-help

USAGE
  The associations file contains commands followed by regular expressions, all
  on separate lines. It enables the user to associate arbitrary strings with
  applications. This relies only on the argument string itself and is
  independent of any associated file or MIME-type.

  Association files can be specified on the command line. Mimeo will also
  check for association files in default locations. Check the main help message
  for details.

COMMANDS
  The command is parsed as a .desktop "Exec" field and may thus contain
  variables such as "%f" and "%F". In the absence of such, the input argument is
  passed as the final argument to the command.

  See the .desktop documentation for rules about quoting etc.:

    http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables

REGULAR EXPRESSIONS
  The regular expressions are indented by exactly two spaces ("  ") and are
  associated with the previous command. Any argument that matches a regular
  expression will be opened with its associated command.

  The two-space indentation allows the use of regular expressions that begin
  with whitespace.

OTHER
  The file may contain empty lines and comments. The first character of a
  comment line must be "#".

EXAMPLES
  Associate HTTP and HTTPS URIs with Firefox:

    /usr/bin/firefox %U
      ^https?://


  Associate Perl (*.pl),  Python (*.py) and text (*.txt) files with gVim:

    /usr/bin/gvim %F
      \.p[ly]$
      \.txt$


  Enqueue various media files in a running instance of VLC:

    /usr/bin/vlc --one-instance --playlist-enqueue %F
      \.mp3$
      \.flac$
      \.avi$
      \.mpg$
      \.flv$

Mimeman Help Message

$ mimeo --mimeman-help

MIME-manager Help

  <MIME-type matcher>
    MIME-types may be specified in one of three ways using different
    "MIME-type matchers":

      Direct Match
        The matcher is interpretted as a MIME-type string, e.g. "text/x-python".

      Shell-style Globbing
        If the matcher begins with "glob:" then the rest of it will be
        interpretted as a shell-style globbing pattern and it will be matched
        against all known MIME-types.

        For example, "glob:text/x-*" would be expanded to all known
        MIME-types beginning with "text/x-".

      Regular Expressions
        If the matcher begins "regex:" then the rest of it will be
        interpretted as a (Perl-style) regular expression and it will be matched
        against all known MIME-types.

        For example, "regex:^text/x-.*" would be equivalent to
        "glob:text/x-*"

  <desktop.file>
    The name of a desktop file, with the ".desktop" extension. Desktop files
    are found in the "applications" sub-directory of directories in
    $XDG_DATA_HOME and $XDG_DATA_DIRS

  <Name>
    The value of the "Name" key in a desktop file.

  <Exec>
    The value of the "Exec" key in a desktop file.

    See
    http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
    for details.

  EXAMPLES

    Set firefox as the prefered browser for all associated MIME-types:

      mimeo --prefer firefox.desktop

    To set multiple preferences, pass the option multiple times:

      mimeo --prefer firefox.desktop --prefer vlc.desktop

    If both desktop arguments are passed together then their MIME-types will be
    pooled and both will be set as preferred applications for the pool, which is
    unlikely to be what you want:

      # Don't do this. Use the previous command instead.
      mimeo --prefer firefox.desktop vlc.desktop

    This is a consequence of the way the command is parsed. It is usually used
    with a MIME-type matcher.  For example, to set vlc and mplayer as preferred
    applications for videos, with vlc taking precedence, you could use:

      mimeo --prefer 'glob:video/*' vlc.desktop mplayer.desktop

    To check which MIME-types will be matched, use:

      mimeo --mimetype 'glob:video/*'

    It is possible to create custom desktop files for your own commnds. For
    example, to create one for Feh and associate all images with it:

      mimeo --create feh.desktop Feh 'feh %F -F -Z' 'glob:image/*' "noterm"

    Now set feh.desktop as the default for PNG and JPEG images:

      mimeo --prefer 'regex:^image/(png|jpe?g)$' feh.desktop

    Instead of passing <MIME-type matcher>, the MIME-type can be parsed from a
    file path. For example, to prefer feh.desktop for all PNG images, use:

      mimeo --prefer /path/to/foo.png feh.desktop

CHANGELOG

2021-02-11

  • Address pylint warnings and errors. Keyword arguments for several functions have changed to confirm to naming conventions.
  • Change executable to Python script.
  • Use named logger.
  • Added --exec option to run commands from desktop file arguments.
  • Changed fifth argument of --create command to “noterm” for non-terminal applications instead of an empty string.

2017-06-05

  • Handle some shlex.split ValueErrors and add more information to others.

2017-02-09

  • Parse MIME-type from filepaths when managing associations.
  • Parse qualified MIME-types (tree, suffix, parameters) and default to unqualified associations if no qualified associations are found.

2016-01-25

  • Fixed bug with double commands with custom associations.
  • Fixed localhost URL resolution.
  • Fixed bug that let to empty output with --desk2field when omitting “.desktop” extensions.
  • Add more debugging messages for reading and writing files.

2016-01-21

  • Catch permission errors.

2016-01-20

  • Ensure a single command per argument.
  • Remove sorting of some informational output to ensure that the displayed order corresponds to the programmatic order, e.g. list MIME-types in the order that they are used to determine a matching desktop entry.
  • Add --show-all option to print multiple commands for each argument.
  • Fixed problem with ordering of multiple defaults.
  • Display only one result per argument for most queries when not using --show-all.
  • Percent-decode paths in file URLs when invoking commands.
  • Better handling and passing of common function parameters.
  • Renamed --fullpath to --full-path.
  • Removed --field2desk. Use --desk2field with --swap instead.

2016-01-18

  • Added --filepath-help.
  • Fix handling of iterators in modify_associations.
  • Omit isolated “%f” and “%u” when interpolating commands with no arguments to avoid empty arguments in returned command.

2016-01-15

  • Refactored code to reduce duplication and heterogeneity.
  • Added multiple options for querying information (--desk2field, --field2desk, --fullpath).

2016-01-14

  • Fixed command bug from previous update.
  • Added default arguments file support.
  • Formatted desktop command-line arguments to avoid extra typing when the intention is unequivocal. The desktop extension may be omitted, and full paths may be passed.
  • Updated Mimeman help message.
  • Added --finddesk option to query paths to desktop files.

2016-01-13

  • Added fallback switching between content and name detection for paths.
  • Minor conversions to generalize functions.
  • Allowed MIME-type matchers as arguments with –mimetype.
  • Fixed bug in configuration file detection.
  • Added --desk2mime and --mime2desk.

2016-01-13

  • Completely rewritten in Python 3.
  • Updated to conform to the latest versions of the standards on freedesktop.org[^see Mimeo.py for details]. The latest version has deprecated $XDG_DATA_DIR/applications (~/.local/share/applications) and all files in it. Use the --deprecated flag to continue to use these, including custom desktop files created by Mimeo.
  • Several new options.
  • Dropped support for --purge option.
  • Redundant informational output formats have been simplified. Lines previously containing foo.desktop /path/to/foo.desktop now only contain /path/to/foo.desktop.

2015-06-23

  • Added “–by-content” option.

2015-01-15

  • Import defaults from defaults.list to mimeapps.list when updating mimeapps.list.

2011-04-25

  • Added “–quiet” option to suppress output when running applications.

2011-03-12

  • Loading of desktop files in subdirectories & further standard-compliance.
  • URI handling via “x-scheme-handle/*” MIME-types.
  • Special handling of ‘file’ URIs.
  • New output format for some operations.

2011-02-22

Mimeo subsumed Mimeman’s functionality, with needed corrections. Mimeman was a dirty kludge created without any knowledge of the standard. Mimeo is now able to manage MIME-type associations and create desktop files.

Mimeo’s algorithm for determing associations has also been corrected in several places.

Contact
echo xyne.archlinux.org | sed 's/\./@/'
Validation
XHTML 1.0 Strict CSS level 3 Atom 1.0