如何将 shell 脚本注册为给定文件扩展名的主应用程序

如何将 shell 脚本注册为给定文件扩展名的主应用程序

我有一个 shell 脚本“the_script”,我想将其注册为扩展名为“.ext”的文件的默认应用程序。为此,我编写了一个 xml 文件来注册 mime 类型,并编写了一个 .desktop 文件来将应用程序与 mime 类型关联起来。该脚本对用户具有可执行位,可在用户的路径中使用。

该 xml 文件名为“the_script.xml”,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/x-the-script">
        <comment>A file for the script</comment>
        <glob pattern="*.ext"/>
    </mime-type>
</mime-info>

然后我就跑了xdg-mime install --novendor the_script.xml

.desktop 文件,名为“the_script.desktop”,内容如下:

[Desktop Entry]
Version=1.0

Type=Application
MimeType=application/x-the-script

Name=The script that manages .ext files

Comment=Interpreter for .ext files

TryExec=the_script
Exec=the_script %f
Terminal=false

然后我就跑去xdg-desktop-menu install --novendor the_script.desktop登记这个文件。

一旦两个文件都注册了,.ext 文件就不再能用文本编辑器打开,但是 Gnome 告诉我没有为“脚本文件”文件安装应用程序。

我已经成功地将其他文件格式注册到其他应用程序。其他应用程序是编译程序,而 the_script 是 shell 脚本。这是我能看到的唯一区别。我如何将我的脚本注册到 .ext 文件?

答案1

如果你这样做的话,这似乎在终端上运行良好xdg-open foo.ext,但是除非你将脚本定义为打开 *.ext 文件的默认脚本,否则它似乎在 Nautilus 中不起作用:

  • 选择这样的文件,
  • 右键点击,
  • 选择特性
  • 选择打开用标签,
  • 点击显示其他应用程序
  • 选择你的脚本(应该是管理 .ext 文件的脚本),
  • 点击设为默认,最后按
  • 关闭

完成此操作后,脚本从 Nautilus 启动。

相关内容