Difference between revisions of "Default applications (简体中文)"

From ArchWiki
Jump to: navigation, search
m (Undo revision 526895 by Zyntuz (talk))
(Sync to latest English page.)
Line 4: Line 4:
 
[[ja:デフォルトアプリケーション]]
 
[[ja:デフォルトアプリケーション]]
 
[[ru:Default applications]]
 
[[ru:Default applications]]
{{翻译状态|Default_Applications|2016-09-01|444827}}
+
{{翻译状态|Default applications|2018-07-03|525282}}
{{翻译请求}}
 
 
{{Related articles start (简体中文)}}
 
{{Related articles start (简体中文)}}
 
{{Related|桌面配置项}}
 
{{Related|桌面配置项}}
Line 11: Line 10:
 
{{Related|窗口管理器}}
 
{{Related|窗口管理器}}
 
{{Related articles end}}
 
{{Related articles end}}
Setting default applications per file type involves detection of the file type as the first step. Since the application launcher cannot fully understand all file types, the detection is based on reading only a small part of the file. There are two common ways to determine the file type:
+
Programs sometimes need to open a file or a [[Wikipedia:Uniform Resource Identifier|URI]] in the user's preferred application. To open a file in the user's preferred application the filetype needs to be detected (usually using filename extensions or [[Wikipedia:List of file signatures|magic numbers]] mapped to [[Wikipedia:Media type|MIME types]]) and there needs to be an application associated with the filetype.
  
* using the file name extension, for example ''.html'' or ''.jpeg''
+
* Some programs (particularly command-line programs) detect default programs using [[environment variables]], see [[Environment variables#Default programs]].
* using the so-called "magic bytes" at the start of the file
+
* Some programs (particularly heirloom UNIX programs) use [[Wikipedia:Media type#mime.types|mime.types]] for MIME type detection and [[Wikipedia:Media type#Mailcap|mailcap]] for application association.
 +
* Many programs outsource the task completely to a [[#Resource openers|#Resource opener]].
  
The first method is very simple and fast, but inaccurate if the file is not named "correctly". The second is more accurate, but slower.
+
Freedesktop.org has standardized filetype detection with the [[Shared MIME database]] specification and application association with the [[XDG MIME Applications]] specification.
  
Since files are not required to have an extension and multiple file name extensions can be used for the same file type, [[w:MIME type|MIME type]]s are commonly used instead to uniquely represent the type of a file. In Arch, tools from the {{Pkg|shared-mime-info}} package are used to maintain the MIME type database, which is used by other packages to register new MIME types. Each package can also use the [[Desktop entries]] to provide information about the MIME types that can be handled by the packaged software. There is frequently more than one application able to handle data of a certain MIME type, so users and even some packages (e.g. [[desktop environments]]) assemble lists of default applications for each MIME type.
+
Some desktop environments also provide a GUI or a file-manager which can interactively configure default applications. If you do not use a desktop environment, you may need to install additional software in order to conveniently manage default applications.
  
Note that while Arch Linux does not provide custom presets for default applications, the [[desktop environment]] you install may do so. Some desktop environments also provide a GUI or a file-manager which enables to interactively configure default applications for certain file extensions. If this suffices for your system usage, you may not need to configure anything further.
+
== Resource openers ==
  
The scope of this article are the freedesktop [https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html Association between MIME types and applications] specification and related parts of the [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.11.html#idm139839923550176 Shared MIME-info Database] specification.
+
* '''XDG MIME Apps''': implements the [[XDG MIME Applications]] specification
 
+
* '''RegEx rules''': allows MIME types to be associated with applications using regular expressions
== MIME 类型与桌面配置项 ==
+
* '''URI support''': allows arbitrary URI schemes to be associated with applications
 
 
{{Expansion|Complete list available at: [http://www.iana.org/assignments/media-types/media-types.xhtml]. Not sure if it's needed to maintain a summarized version in this article.}}
 
 
 
MIME-types are specified by two slash ({{ic|/}}) parts: ''Type/Extension'', for example {{ic|video/x-ms-wmv}}. The second part of the MIME-type is expanding faster, for example with new applications or data encoding standards.
 
 
 
The default applications to open a MIME-type are usually stored in [[#Default mimeapps.list files|mimeapps.list]]{{Broken section link}} files, but some programs store MIME-type associations in their own custom configuration files. If a MIME-type association is not found in the default configuration, the program should look for the first match of the MIME-type in ''.desktop'' files.
 
 
 
Description of some MIME-type first part and examples of second parts:
 
  
 
{| class="wikitable"
 
{| class="wikitable"
! Type of MIME (1st part) !! Description !! Examples of extension (2nd part)
+
! Name !! Package !! XDG MIME Apps !! RegEx rules !! URI support
|-
 
|application || Files with binary content such, e.g.: documents,archives,... || epub+zip, ereader, excel, gbr, gzip
 
|-
 
|audio || Audio files that that can be played by a music player or audio editor ||  flac, m4a, midi
 
|-
 
|chemical || Chemical information, molecular and other chemical data  || x-cif, x-cml, x-daylight-smiles, x-gamess-input, x-gamess-output, x-gaussian-checkpoint, x-gaussian-cube, x-gaussian-log, x-mopac-out, x-pdb, x-qchem-output, x-xyz
 
|-
 
|image || Image files that can be opened by image editor or image viewer || bmp, crw, g3fax, gif, jp2, jpeg, jpeg2000, jpg
 
|-
 
|inode || Can be opened by a file manager || blockdevice, chardevice, directory, fifo, mount-point, socket, symlink
 
|-
 
|message || Message protocols || delivery-status, disposition-notification, external-body, news, partial, rfc822, x-gnu-rmail
 
|-
 
|misc || Streaming meta data || ultravox
 
|-
 
|text || Text documents that can be viewed (e.g. with command ''less'') or opened with a text editor || html, javascript, mathml, mml, plain
 
|-
 
|video || Video files that can be played or edited with a video editor || flv, mp2t, mp4
 
|-
 
|x-content || Content on disks such as e.g. Audio,Video,Image or blank disk || audio-cdda, audio-player, blank-bd, blank-cd, blank-dvd, blank-hddvd, image-picturecd, video-dvd, video-svcd, video-vcd
 
|-
 
|x-scheme-handler || Internet protocol || ftp, geo, ghelp, help, http, https, hwplay, icy, icyx, info, irc, magnet, mailto, man, mms, mmsh, net, pnm, rtmp, rtp, rtsp, skype, uvox, vnc, xmpp
 
|-
 
|x-epoc || SISX package ||x-sisx-app
 
 
|-
 
|-
|multipart || Multi-part mime messages ||alternative, appledouble, digest, encrypted, mixed, related, report, signed, x-mixed-replace
+
|[[xdg-open]] || [[xdg-utils]] || {{Yes}} || {{No}} || {{Yes}}
 
|-
 
|-
|model || such as 3D model || x-kpovmodeler, vrml, x-modelica
+
|{{man|1p|mimeopen}} || {{Pkg|perl-file-mimeinfo}} || {{Yes}} || {{No}} || {{No}}
|}
 
 
 
For the description of MIME-types you can search in XDG database:
 
$ grep -e 'mime-type type=' -e '<comment>'  /usr/share/mime/packages/freedesktop.org.xml
 
 
 
{{Tip|To see all MIME extensions in the system's ''.desktop'' files that belongs to a MIME-type you can use {{AUR|lsdesktopf}}, for example {{ic|lsdesktopf --gm -gx video}}, to search available in ''.xml'' configuration/database files use {{ic|lsdesktopf --gdx -gx video}}.}}
 
 
 
If you ever require to create a custom association of a new file extension to a MIME-type, see the
 
the short [[#Example: .xml and related .desktop configuration]]{{Broken section link}} and the [http://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html Association between MIME types and applications] standard.
 
 
 
== 设置默认应用程序 ==
 
 
 
In order to set a default application, you need to
 
* decide which of the [[#Default mimeapps.list files]]{{Broken section link}} is applicable for your case and,
 
* change the [[#Configuration of the mimeapps.list file]]{{Broken section link}} accordingly.
 
 
 
Any manual configuration of the [[#Shared MIME-info database]]{{Broken section link}} is only required, if the application is not setup correctly or desktop does not comply to the standard yet.
 
 
 
=== 默认 mimeapps.list 文件 ===
 
 
 
{{Expansion|Moving deprecated configuration files to a Note, rather than include them in the table, would improve overview. More context on the changing of the file location could also be useful.}}
 
 
 
The {{ic|mimeapps.list}} file stores the configuration for the default application to open a MIME-type.
 
There are different locations for it:
 
* system-wide,
 
* per-user,
 
* custom locations used by some programs.
 
 
 
The {{ic|''$desktop''}} in the following list denotes the name of the related desktop environment or window manager. The search order of paths is:
 
 
 
{| class="wikitable"
 
! Path !! Usage
 
|-
 
| {{ic|$HOME/.config/''$desktop''-mimeapps.list}} || user overrides, desktop-specific
 
 
|-
 
|-
| {{ic|$HOME/.config/mimeapps.list}} || user overrides
+
|mimeo || {{AUR|mimeo}} || {{Yes}} || {{Yes}} || {{Yes}}
 
|-
 
|-
| {{ic|/etc/xdg/''$desktop''-mimeapps.list}} || sysadmin and vendor overrides, desktop-specific
+
|whippet || {{AUR|whippet}} || {{Yes}} || {{No}} || {{Yes}}
 
|-
 
|-
| {{ic|/etc/xdg/mimeapps.list}} || sysadmin and vendor overrides
+
|linopen || {{AUR|linopen}} || {{No}} || {{Yes}} || {{Yes}}
 
|-
 
|-
| {{ic|$HOME/.local/share/applications/''$desktop''-mimeapps.list}} || for compatibility but now deprecated, desktop-specific
+
|mimi || {{AUR|mimi-git}} || {{No}} || {{No}} || {{Y|partly}}
 
|-
 
|-
| {{ic|$HOME/.local/share/applications/mimeapps.list}} || for compatibility but now deprecated
+
|busking || {{AUR|busking-git}} || {{No}} || {{Yes}} || {{Yes}}
 
|-
 
|-
| {{ic|/usr/local/share/applications/''$desktop''-mimeapps.list}}<br>{{ic|/usr/share/applications/$desktop-mimeapps.list}} || distribution-provided defaults, desktop-specific
+
|sx-open || {{AUR|sx-open}} || {{No}} || {{Yes}} || {{Yes}}
 
|-
 
|-
| {{ic|/usr/local/share/applications/mimeapps.list}}<br>{{ic|/usr/share/applications/mimeapps.list}} || distribution-provided defaults
+
|{{man|1|rifle}} || [[ranger]] || {{No}} || {{Yes}} || {{No}}
 
|}
 
|}
  
=== mimeapps.list 文件的配置内容 ===
+
=== xdg-open ===
 
 
The file contains two main sections for default and additional alternatives to open files of a MIME-type. The second and the third section ({{ic|[Removed Associations]}}) are optional.
 
 
 
If the application installed a correct [[desktop entry]] file (examples used below {{ic|default1.desktop}}, {{ic|foo1.desktop}}, etc.), it contains the registered MIME-types it can handle. To '''set a default application''', all you need to do is adjust the associations in the respective {{ic|mimeapps.list}} (see also the freedesktop [https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html#associations specification]).
 
 
 
For example, the following sets {{ic|default1.desktop}} to open {{ic|mimetype1}}:
 
 
 
[Default Applications]
 
mimetype1=default1.desktop
 
 
 
Defined additional associations of applications to MIME-types (these might appear in file manager ''Open with'' GUI, for example):
 
[Added Associations]
 
mimetype1=foo1.desktop;foo2.desktop;foo3.desktop;
 
mimetype2=foo4.desktop;
 
 
 
Removed associations of applications with MIME-types (blacklisting a MIME-type association of an application in its ''.desktop'' file):
 
[Removed Associations]
 
mimetype1=foo5.desktop
 
 
 
Multiple ''.desktop'' files for a single MIME-type must be semicolon-separated. Not supported entries are ignored in {{ic|mimeapps.list}}. The DE/WM then searches for the first match to the needed MIME-type them in the default path for ''.desktop'' files.
 
 
 
{{提示|To get a quick overview of how many and which ''.desktop'' files can be associated with a certain MIME-type, you can use the {{AUR|lsdesktopf}} utility, e.g. {{ic|lsdesktopf --gen-mimeapps}}.}}
 
 
 
{{注意|Arch Linux itself does not provide system-wide presets for associations, but other distributions and specific desktop environments may do so via packaged {{ic|mimeapps.list}} files, or the older and deprecated {{ic|defaults.list}} files.}}
 
 
 
Your choice of desktop environment, or none, will affect how your default applications are associated. Further, note that some packages use additional work-around presets, for example Mozilla's packages install a {{ic|/etc/mime.types}} file.
 
 
 
=== 共享的 MIME 信息数据库 ===
 
 
 
In background to the {{ic|mimeapps.list}} files, the system holds a database of MIME-type information registered via the installed applications' ''.desktop'' files, the [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.11.html#idm139839923550176 Shared MIME-info Database]. It is created automatically as soon as an application depending on it is installed, via the following [[pacman#Hooks]]:
 
* {{ic|/usr/share/libalpm/hooks/update-mime-database.hook}} from {{Pkg|shared-mime-info}}
 
:updates the MIME-info database in {{ic|/usr/share/mime}}, in particular also the ''.xml'' specification in {{ic|/usr/share/mime/packages/freedesktop.org.xml}} for the MIME-types standards
 
* {{ic|/usr/share/libalpm/hooks/update-desktop-database.hook}} from {{Pkg|desktop-file-utils}}
 
:updates the {{ic|mimeinfo.cache}} located (per default) in {{ic|/usr/share/applications}}
 
 
 
These files keep track of which MIME-types are associated with which ''.desktop'' files overall. When an application is installed, updated or removed, the pacman hooks keep the database updated accordingly.
 
 
 
{{警告|The database files are not meant to be edited directly.}}
 
 
 
Application specific configuration is stored in ''.xml'' files and further files of the database (see [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.11.html#idm139839923550176 .xml source files]) are stored in ''.keys'' and ''.mime'' files that are located in {{ic|/usr/share/mime-info/}}.
 
 
 
Global directories for the ''.xml'' files are:
 
 
 
/usr/share/mimelnk/application/
 
/usr/share/mime/packages/
 
 
 
{{Warning|Above should not be modified, for user-specific configuration see below.}}
 
 
 
Any user-specific ''.xml'' configuration may be stored in:
 
~/.local/share/mime/packages/
 
 
 
==== 范例:.xml 与关联的桌面配置项 ====
 
  
The following is a short example to ''create'' files to associate an application with a MIME-type. It only needs to be followed, if the application has '''not''' setup and registered its configuration sufficiently.  
+
[[xdg-open]] (part of [[xdg-utils]]) implements [[XDG MIME Applications]] and is used by many programs.
  
User-specific paths are used to override system defaults, while keeping the integrity of the rest of the system-wide configuration.  
+
Because of the complexity of the [[xdg-utils]] version of [[xdg-open]], it can be difficult to debug when the wrong default application is being opened. Because of this, there are many alternatives that attempt to improve upon it. Several of these alternatives replace the {{ic|/usr/bin/xdg-open}} executable, thus changing the default application behavior of most applications. Others simply provide an alternative method of choosing default applications.
  
Create and edit {{ic|~/.local/share/mime/packages/application-x-foobar.xml}}:
+
=== perl-file-mimeinfo ===
  
{{bc|1=
+
{{Pkg|perl-file-mimeinfo}} provides the tools {{man|1p|mimeopen}} and {{man|1p|mimetype}}. These have a slightly nicer interface than their {{pkg|xdg-utils}} equivalents:
<?xml version="1.0" encoding="UTF-8"?>
+
# determine a file's MIME type
<nowiki><mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"></nowiki>
+
$ mimetype photo.jpeg
    '''<mime-type type="application/x-foobar">'''
+
photo.jpeg: image/jpeg
        <comment>foo file</comment>
+
        <icon name="application-x-foobar"/>
+
# choose the default application for this file
        <glob-deleteall/>
+
$ mimeopen -d photo.jpeg
        '''<glob pattern="*.foo"/>'''
+
Please choose an application
    </mime-type>
+
</mime-info>
+
    1) Feh (feh)
}}
+
    2) GNU Image Manipulation Program (gimp)
{{提示|To see all file name extensions in ''.xml'' configuration or database files, that available in ''glob pattern'', with their description in ''comment'' use {{AUR|lsdesktopf}}: {{ic|lsdesktopf --gdx -gfx}}.}}
+
    3) Pinta (pinta)
Create a related [[desktop entry]] file:
+
 +
  use application #
 +
 +
# open a file with its default application
 +
$ mimeopen -n photo.jpeg
  
{{hc|~/.local/share/applications/foobar.desktop|2=
+
Most importantly, [[xdg-utils]] programs will actually call {{ic|mimetype}} instead of {{ic|file}} for MIME type detection, if it does not detect your [[desktop environment]]. This is important because {{ic|file}} does not follow the XDG standard.
[Desktop Entry]
 
Name=Foobar
 
Exec=/usr/bin/foobar
 
'''MimeType=application/x-foobar'''
 
Icon=foobar
 
Terminal=false
 
Type=Application
 
Categories=AudioVideo;Player;Video;
 
Comment=
 
}}
 
  
Now update the application and mime database with:
+
{{Note|{{pkg|perl-file-mimeinfo}} before 0.28-1 does not ''entirely'' follow the XDG standard. For example it does not read [https://github.com/mbeijen/File-MimeInfo/issues/20 distribution-wide defaults] and it saves its config in [https://github.com/mbeijen/File-MimeInfo/issues/8 deprecated locations].}}
  
$ update-desktop-database ~/.local/share/applications
+
=== mimeo ===
$ update-mime-database    ~/.local/share/mime
 
  
Programs that use MIME-types, such as file managers, should now open {{ic|*.foo}} files with foobar  (you may need to restart your file manager to see the change.)
+
{{aur|mimeo}} provides the tool {{ic|mimeo}}, which unifies the functionality of {{ic|xdg-open}} and {{ic|xdg-mime}}.
  
{{Style|The relation between environment variables and the above discussion on MIME types is not immediate. Setting {{ic|BROWSER}} e.a, while also related to default applications, is a different approach entirely.}}
+
# determine a file's MIME type
 +
$ mimeo -m photo.jpeg
 +
photo.jpeg
 +
  image/jpeg
 +
 +
# choose the default application for this MIME type
 +
$ mimeo --add image/jpeg feh.desktop
 +
 +
# open a file with its default application
 +
$ mimeo photo.jpeg
  
See also [[Environment variables#Examples]] about global variables that can be used in start-up scripts to set default applications for specific actions.
+
However a big difference with ''xdg-utils'' is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:
  
== MIME 类型的管理工具 ==
+
# open youtube links in VLC without opening a new instance
 +
vlc --one-instance --playlist-enqueue %U
 +
  ^https?://(www.)?youtube.com/watch\?.*v=
  
Many of the file managers has options to set up and configure associations of mime types with programs.  
+
{{aur|xdg-utils-mimeo}} patches ''xdg-utils'' so that {{ic|xdg-open}} falls back to mimeo if no desktop environment is detected.
  
It can be done by using:
+
=== whippet ===
  
* Preferences for selected file
+
{{aur|whippet}} provides the tool {{ic|whippet}}, which is similar to {{ic|xdg-open}}. It has X11 integration by using {{pkg|libnotify}} to display errors and {{pkg|dmenu}} to display choices between applications to open.
* File manager configuration menu
 
* External program for a specific file manager
 
  
Other utilities to manage MIME-types are:
+
# open a file with its default application
 +
$ whippet -M photo.jpeg
 +
 +
# choose from all possible applications for opening a file (without setting a default)
 +
$ whippet -m photo.jpeg
  
{{Accuracy|Packages such as {{AUR|sx-open}} may replace xdg-utils in the sense that they have a provides/replaces field to pacman, but they miss tools used by other applications such as {{ic|xdg-screensaver}} and {{ic|xdg-icon-resource}}. This is not clear from the table. The table should also be better adapted to the section below, or the "based on" section could simply be left out.}}
+
In addition to implementing [[XDG MIME Applications]], ''whippet'' can also use a SQlite database of weighted application/MIME type/regex associations to determine which app to use.
  
{| class="wikitable"
+
=== Minimalist replacements ===
! Name/Package !! Method !! Based on !! xdg-utils replacement !! Configuration file
 
|-
 
| {{AUR|mime-editor}} || MIME-type || Utility for [http://rox.sourceforge.net/desktop/home.html ROX] applications || ||
 
|-
 
| {{AUR|busking-git}} || regex || {{Pkg|perl-file-mimeinfo}} || yes || custom
 
|-
 
| {{AUR|linopen}} || || {{Pkg|file}} || || custom
 
|-
 
| {{AUR|mimeo}} || MIME-type <br> regex || {{Pkg|file}} || {{AUR|xdg-utils-mimeo}} ||{{ic|mimeapps.list}} <br> {{ic|defaults.list}} <br> custom is optional
 
|-
 
| {{AUR|mimi-git}} || || {{Pkg|file}} || yes || custom
 
|-
 
| {{ic|rifle}} <br> part of {{Pkg|ranger}} || MIME-type <br> name <br> regex || {{Pkg|file}} || || custom
 
|-
 
| {{AUR|sx-open}} || regex || {{Pkg|file}} <br> bash regex || yes || custom
 
|-
 
| {{AUR|whippet}} || MIME-type <br> name <br> regex || SQLite database <br> {{Pkg|file}} <br> {{Pkg|perl-file-mimeinfo}}, etc || xdg-open || custom SQLite database <br> {{ic|mimeapps.list}}
 
|-
 
| {{Pkg|xdg-utils}} ||  || {{Pkg|file}} <br> {{Pkg|perl-file-mimeinfo}} || ||
 
|}
 
 
 
=== 用例 ===
 
 
 
Here is a short description about how to use command line tools to show MIME-type of a file or set preferred program as default to open MIME-type.
 
 
 
==== 侦测 MIME 类型 ====
 
 
 
{{Style|Unclear what is meant by "it is two standards". Going by the {{ic|Magic -> XML}} entries, the first seems to be a "compiled database", without indication how it is created. The second the XML files as described in [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.20.html]. Latter can also define file contents, and the "extension associated with one program but content is associated with another MIME-type" is vague at best.}}
 
  
Tools detecting MIME-type by reading meta-data from header of the file or detecting by magic number that is in two bytes identifier in the begin of the file. See [[Wikipedia:File format#File header|File header]].
+
The following packages conflict with and provide {{Pkg|xdg-utils}} because they provide their own {{ic|/usr/bin/xdg-open}} script.
  
Many programs are using command ''file'' to detect correct MIME-type. For detection it uses compiled database that is stored in the {{ic|/usr/share/misc/magic/}} directory. It has many options to determinate correct MIME-type and for showing output.
+
If you want to use one of these resource openers while still being able to use {{Pkg|xdg-utils}}, install them manually in a PATH directory before {{ic|/usr/bin}}.
  
In Linux it is two standards to detect MIME-type that can affect which program will start and open the file, e.g. extension is associated with one program but content is associated with another MIME-type. The simplest way to see what is your system prioritizes is by renaming file extension and check again with tools that can use custom [[#Associate file extensions with applications MIME-type|*.xml]]{{Broken section link}} configuration files.
+
* {{AUR|linopen}} - 170-line Bash script, supports regex rules
 +
* {{AUR|mimi-git}} - 130-line Bash script, can change command arguments for each MIME type
 +
* {{AUR|busking-git}} - 80-line Perl script similar to ''mimi'' but also supports regex rules
 +
* {{AUR|sx-open}} - 60-line Bash script, uses a simple shell-based config file
  
{{Accuracy|The following sentence is true only when {{pkg|perl-file-mimeinfo}} is installed. Otherwise, ''file'' is used and hence the detection method is different.}}
+
==== run-mailcap ====
  
Here is examples of utility ''xdg-mime'' that first checking association of extension in [[#Associate file extensions with applications MIME-type|*.xml]]{{Broken section link}} configuration files.
+
{{Warning|If you use {{AUR|run-mailcap}}, it is possible for {{ic|xdg-open}} to delegate to it. This will cause an infinite loop if you are using the {{ic|/etc/mailcap}} from {{Pkg|mailcap}}, because it also delegates to {{ic|xdg-open}}.}}
{| class="wikitable"
 
! Without  extension !! With extension
 
|-
 
|xdg-mime query filetype ''foo-file''  || xdg-mime query filetype ''foo-file''.''jpg''
 
|-
 
! application/vnd.oasis.opendocument.text ||  image/jpeg
 
|-
 
|}
 
 
 
Comparison functionality of the tools
 
 
 
Here is shown options only for simple or multiple checks of a file, for more options read their own documentation.
 
{| class="wikitable"
 
! Tool !! Option !! Detection order !! Functionality !! Type !! Interface
 
|-
 
|file || ''(has many)'' || Magic(Byte/Pattern) || Show only || binary || terminal
 
|-
 
|xdg-mime || query filetype || *.xml -> Magic || Show / Set || script || terminal
 
|-
 
|mimetype || -i ''(*.xml)'' -M ''(Magic)'' || *.xml -> Magic || Show only || script || terminal
 
|-
 
|mimeo || -m || *.xml -> Magic  || Show / Set / Launch ||  script || terminal
 
|}
 
 
 
==== Set use of MIME-type by default ====
 
 
 
Comparison functionality of the tools
 
 
 
Here is shown options only for single file associations, for more options read their own documentation.
 
 
 
{| class="wikitable"
 
! Tool !! Option !!  Functionality !! Type !! Interface
 
|-
 
|xdg-mime || default ''*.desktop'' Type1/Extension1 Type2/Extension2 ||  Show / Set || script || terminal
 
|-
 
|mimeo || --prefer 'regex:^Type/(Extension'''1'''<nowiki> |</nowiki>Extension?'''2''')$'  ''*.desktop'' || Show / Set / Launch ||  script || terminal
 
|-
 
|mimeopen || --ask-default ''(interactive)'' || Set / Launch  || script || terminal
 
|}
 
 
 
=== 应用程序加载器 ===
 
 
 
{{Style|Why start over with sections when we already have a table? Pick one.}}
 
 
 
==== xdg-open ====
 
 
 
''xdg-open'' (from the {{Pkg|xdg-utils}} package) uses {{Pkg|perl-file-mimeinfo}} as a fallback ("generic") method if no [[desktop environment]] is detected. It is a desktop-independent tool. Many applications invoke the {{ic|xdg-open}} command internally. Inside a [[desktop environment]] it passes the arguments to desktop supported environment's file-opener applications (e.g. ''gvfs-open'', ''kde-open'', or ''exo-open''). When no desktop environment is detected the ''xdg-open'' will use its own configuration files.
 
 
 
==== mimeopen ====
 
 
 
''mimeopen'' (from the {{Pkg|perl-file-mimeinfo}} package) can launch applications from command line. It can use custom database and prompt user to chose default application from a list of detected relevant and offers to chose own alternative.
 
 
 
Example of the prompt:
 
 
 
{{hc|$ mimeopen -d /path/to/foo-file|
 
Please choose a default application for files of type ''Type''/''Extension''
 
        1) notepad  (wine-extension-txt)
 
        2) Leafpad  (leafpad)
 
        3) OpenOffice.org Writer  (writer)
 
        4) gVim  (gvim)
 
        5) Other...
 
}}
 
 
 
Your answer becomes the default handler for that type of file. Mimeopen is installed as {{ic|/usr/bin/vendor_perl/mimeopen}}.
 
 
 
If you run ''xdg-open'' without a desktop environment, you should also install {{Pkg|perl-file-mimeinfo}}, or {{AUR|xdg-utils-mimeo}} and {{AUR|mimeo}} from the [[AUR]] for a faster alternative.
 
 
 
==== mailcap ====
 
 
 
The [http://linux.die.net/man/4/mailcap mailcap] file.
 
 
 
{{Expansion|When using {{AUR|run-mailcap}}, ''xdg-open'' may refer to it.[http://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-open.in#n266] It should then clearly not be combined with the below file to prevent endless loops.}}
 
 
 
The ''.mailcap'' file format is used by mail programs such as {{Pkg|mutt}} and {{Pkg|sylpheed}}. To have those programs use ''xdg-open'', edit {{ic|~/.mailcap}}:
 
 
 
{{hc|~/.mailcap|
 
*/*; xdg-open "%s"
 
}}
 
 
 
=== 扩展练习实例 ===
 
 
 
{{Style|Redundant and oddly named header; it is also odd that alternatives are discussed before the regular {{Pkg|xdg-utils}}.}}
 
 
 
==== xdg-open ====
 
 
 
{{Accuracy|Both ''xdg-mime'' and ''xdg-settings'' ignore the {{ic|~/.config/mimeapps.list}} file ''for writing'', instead they use the deprecated {{ic|~/.local/share/applications/mimeapps.list}}. If the former exists, the latter is ignored, so the changes have no effect.[https://github.com/The-Compiler/qutebrowser/issues/22#issuecomment-224507278]}}
 
 
 
''xdg-mime'' modifies the local file {{ic|~/.local/share/applications/mimeapps.list}} (deprecated).
 
 
 
To '''query''' the mime type used by an existing file, use
 
$ xdg-mime query filetype ''file.ext''
 
 
 
To change an associated desktop entry by setting [[Thunar]] as the default file browser:
 
 
 
$ xdg-mime default Thunar.desktop inode/directory
 
Note that you should not specify the complete path, but only the name of the ''.desktop'' file.
 
 
 
This command can take multiple mime-types, allowing related files to be handled by the same program. The example below associates [[Emacs]] to all known source files:
 
$ xdg-mime default emacs.desktop $(grep '^text/x-*' /usr/share/mime/types)
 
 
 
===== 设置默认浏览器 =====
 
 
 
To set the default application for {{ic|http(s)://}} internet protocols:
 
 
 
$ xdg-mime default midori.desktop x-scheme-handler/http
 
$ xdg-mime default midori.desktop x-scheme-handler/https
 
 
 
As an alternative try:
 
 
 
$ xdg-settings set default-web-browser netsurf.desktop
 
 
 
To verify if the URLs opens correctly:
 
 
 
$ xdg-open <nowiki>https://archlinux.org</nowiki>
 
 
 
To associate ''.html'' files with the {{Pkg|netsurf}} web-browser:
 
 
 
$ xdg-mime default netsurf.desktop text/html
 
 
 
== 排错 ==
 
 
 
=== 桌面配置项文件中影响应用程序加载的变量 ===
 
 
 
{{Expansion|1=The fact {{ic|MimeType}} entries may be missing in the desktop files is only implied here, i.e. "Even if an application...", though this is a common cause of errors. Some openers may also associate mime types not explicitely listed in a desktop file (such as {{Pkg|exo}}). Further environment-specific factors are at play, e.g. whether {{ic|1=Terminal=true}} has an effect, though latter should arguably be expanded on in [[Desktop entries]].}}
 
 
 
Desktop environments and file managers supporting the specifications launch programs according to definition in the ''.desktop'' files. See [[Desktop entries#Application entry]].
 
 
 
Usually, configuration of the packaged ''.desktop'' files is not required, but it may not be bug-free. Even if an application containing necessary MIME-type description in the ''.desktop'' file {{ic|MimeType}} variable that is used for association, it can fail to start correctly, not start at all or start without opening a file.  
 
 
 
This may happen, for example, if the {{ic|Exec}} variable is missing internal options needed for how to open a file, or how the application is shown in the menu. The {{ic|Exec}} variable usually begins with {{ic|%}}; for its currently supported options, see [https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables exec-variables].
 
 
 
The following table lists the main variable entries of ''.desktop'' files that affect how an application starts, if it has a MIME-type associated with it.
 
 
 
{| class="wikitable"
 
! Variable names !! Example 1 content !! Example 2 content !! Description
 
|-
 
| DBusActivatable || DBusActivatable=true || DBusActivatable=false || Application interact with [https://www.freedesktop.org/wiki/Software/dbus/ D-Bus]. <br> See also configuration: [https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus D-Bus].
 
|-
 
| MimeType || MimeType=application/vnd.oasis.opendocument.text || MimeType=application/vnd.sun.xml.math || List of MIME types supported by application
 
|-
 
| StartupWMClass || StartupWMClass=google-chrome || StartupWMClass=xpad || Associate windows with the owning application
 
|-
 
| Terminal || Terminal=true || Terminal=false || Start in default terminal
 
|}
 

Revision as of 08:42, 3 July 2018

翻译状态: 本文是英文页面 Default applications翻译,最后翻译时间:2018-07-03,点击这里可以查看翻译后英文页面的改动。

Programs sometimes need to open a file or a URI in the user's preferred application. To open a file in the user's preferred application the filetype needs to be detected (usually using filename extensions or magic numbers mapped to MIME types) and there needs to be an application associated with the filetype.

Freedesktop.org has standardized filetype detection with the Shared MIME database specification and application association with the XDG MIME Applications specification.

Some desktop environments also provide a GUI or a file-manager which can interactively configure default applications. If you do not use a desktop environment, you may need to install additional software in order to conveniently manage default applications.

Resource openers

  • XDG MIME Apps: implements the XDG MIME Applications specification
  • RegEx rules: allows MIME types to be associated with applications using regular expressions
  • URI support: allows arbitrary URI schemes to be associated with applications
Name Package XDG MIME Apps RegEx rules URI support
xdg-open xdg-utils Yes No Yes
mimeopen(1p) perl-file-mimeinfo Yes No No
mimeo mimeoAUR Yes Yes Yes
whippet whippetAUR Yes No Yes
linopen linopenAUR No Yes Yes
mimi mimi-gitAUR No No partly
busking busking-gitAUR No Yes Yes
sx-open sx-openAUR No Yes Yes
rifle(1) ranger No Yes No

xdg-open

xdg-open (part of xdg-utils) implements XDG MIME Applications and is used by many programs.

Because of the complexity of the xdg-utils version of xdg-open, it can be difficult to debug when the wrong default application is being opened. Because of this, there are many alternatives that attempt to improve upon it. Several of these alternatives replace the /usr/bin/xdg-open executable, thus changing the default application behavior of most applications. Others simply provide an alternative method of choosing default applications.

perl-file-mimeinfo

perl-file-mimeinfo provides the tools mimeopen(1p) and mimetype(1p). These have a slightly nicer interface than their xdg-utils equivalents:

# determine a file's MIME type
$ mimetype photo.jpeg
photo.jpeg: image/jpeg

# choose the default application for this file
$ mimeopen -d photo.jpeg
Please choose an application

    1) Feh (feh)
    2) GNU Image Manipulation Program (gimp)
    3) Pinta (pinta)

use application #

# open a file with its default application
$ mimeopen -n photo.jpeg

Most importantly, xdg-utils programs will actually call mimetype instead of file for MIME type detection, if it does not detect your desktop environment. This is important because file does not follow the XDG standard.

Note: perl-file-mimeinfo before 0.28-1 does not entirely follow the XDG standard. For example it does not read distribution-wide defaults and it saves its config in deprecated locations.

mimeo

mimeoAUR provides the tool mimeo, which unifies the functionality of xdg-open and xdg-mime.

# determine a file's MIME type
$ mimeo -m photo.jpeg
photo.jpeg
  image/jpeg

# choose the default application for this MIME type
$ mimeo --add image/jpeg feh.desktop

# open a file with its default application
$ mimeo photo.jpeg

However a big difference with xdg-utils is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:

# open youtube links in VLC without opening a new instance
vlc --one-instance --playlist-enqueue %U
  ^https?://(www.)?youtube.com/watch\?.*v=

xdg-utils-mimeoAUR patches xdg-utils so that xdg-open falls back to mimeo if no desktop environment is detected.

whippet

whippetAUR provides the tool whippet, which is similar to xdg-open. It has X11 integration by using libnotify to display errors and dmenu to display choices between applications to open.

# open a file with its default application
$ whippet -M photo.jpeg

# choose from all possible applications for opening a file (without setting a default)
$ whippet -m photo.jpeg

In addition to implementing XDG MIME Applications, whippet can also use a SQlite database of weighted application/MIME type/regex associations to determine which app to use.

Minimalist replacements

The following packages conflict with and provide xdg-utils because they provide their own /usr/bin/xdg-open script.

If you want to use one of these resource openers while still being able to use xdg-utils, install them manually in a PATH directory before /usr/bin.

  • linopenAUR - 170-line Bash script, supports regex rules
  • mimi-gitAUR - 130-line Bash script, can change command arguments for each MIME type
  • busking-gitAUR - 80-line Perl script similar to mimi but also supports regex rules
  • sx-openAUR - 60-line Bash script, uses a simple shell-based config file

run-mailcap

Warning: If you use run-mailcapAUR, it is possible for xdg-open to delegate to it. This will cause an infinite loop if you are using the /etc/mailcap from mailcap, because it also delegates to xdg-open.