gio openがどのプログラムを使用しているか確認してください。

gio openがどのプログラムを使用しているか確認してください。

このコマンドを実行すると、その特定のファイルタイプのデフォルトアプリケーションを使用してファイルが開かれていることをgio open --help確認できます。gio open

ファイルをコマンドラインで開くアプリケーションの出力をどのように取得できますか?

私は同様のものをgio open somefile.txt --print-application出力したいと思いますgedit

ベストアンサー1

gioこれは使用できませんが、xdg-mimeファイルのファイル形式を照会し、基本アプリケーションの説明ファイルをインポートするために使用できます。

# This prints the mime type of somefile.txt
xdg-mime query filetype somefile.txt
# Output, for example: text/plain

# This queries the default application that opens a mime type
xdg-mime query default text/plain
# Output, for example: gedit.desktop

プロセスの置き換えにより、これら2つを組み合わせるか、シェル関数に入れることができます。

# do a substitution to do this on one line
xdg-mime query default $(xdg-mime query filetype somefile.txt)

# Or make a small function that solves the whole thing:
function get_handler_application() {
  filetype="$(xdg-mime query filetype "$1")"
  xdg-mime query default "${filetype}"
}
# call like:
# get_handler_application somefile.txt

しかし、実際には、あなたが得た結果が次のようになることがわかります。いいえこれは実行可能ファイルですが、.desktopファイル - デスクトップファイルには、プログラムの呼び出しに使用する必要があるすべてのオプションが含まれているため、これは重要な機能です。

:環境変数の各区切りパスの下にある「applications」というディレクトリにこれらのデスクトップファイルを見つけることができます$XDG_DATA_DIRS

シェルが次の場合、zsh非常にエレガントに処理されます。

#!/usr/bin/zsh
# Copyright 2023 Marcus Müller
# SPDX-License-Identifier: GPL-3.0-or-later

# We can get the name of the file handler this way,
# for example:
#   get_handler_application aaargh.txt
# outputs:
# text/plain
function get_handler_application() {
  filetype="$(xdg-mime query filetype "$1")"
  xdg-mime query default "${filetype}"
}

# Get the full path to the specific file that opens this file
# There's multiple paths, and user-defined ones take priority
# over system-defined ones. For example:
#   get_handler_desktop_file aaargh.txt
# outputs:
# pluma.desktop
function get_handler_desktop_file(){
  desktop_fname="$(get_handler_application "$1")"
  for candidate in ${(ps.:.)XDG_DATA_DIRS}; do
    fullpath="${candidate}/applications/${desktop_fname}"
    [[ -r "${fullpath}" ]] && printf '%s' ${fullpath} && break
  done
}

# Get all the commands that are specified in the desktop file.
# There can be multiple, because some file handlers have extended 
# options for opening things, like "open in new window" or "make
# new document from this template.
# For example:
#   get_handler_command_lines holygrail.html
# outputs:
# firefox %u
# firefox --new-window %u
# firefox --private-window %u
# firefox --ProfileManager
function get_handler_command_lines() {
  sed -n 's/^Exec=\(.*\)*/\1/p' "$(get_handler_desktop_file "$1")"
}

おすすめ記事