linux 命令行进行桌面图标的打开

近期在处理一个需求,需要在代码中打开桌面的某个图标,因此,做了一些搜索,最终发现,有两个比较好用的命令,下面来讲解一下。

1、gtk-launch

在linux系统一般已经集成这个工具

所在位置: /usr/bin/gtk-launch
使用方法:gtk-launc <app-name>

下面是官方给出的指导

NAME
       gtk-launch - Launch an application

SYNOPSIS
       gtk-launch [OPTION...] APPLICATION [URI...]

DESCRIPTION
       gtk-launch launches an application using the given name. The application is started with proper startup notification on a default display, unless specified otherwise.

       gtk-launch takes at least one argument, the name of the application to launch. The name should match application desktop file name, as residing in /usr/share/application, with or without the '.desktop' suffix.

       If called with more than one argument, the rest of them besides the application name are considered URI locations and are passed as arguments to the launched application.

OPTIONS
       The following options are understood:

       -?, --help
           Prints a short help text and exits.

       --version
           Prints the program version and exits.

来一起看一个例子,打开 LibreOffice Writer

root@Xubuntu18:/Desktop# ll
total 1367204
drwxrwxrwx  3 mt mt       4096 1123 19:30 ./
drwxr-xr-x 57 mt mt       4096 1122 08:30 ../
-rwxr-xr-x  1 mt mt      21390 921  2019 libreoffice-writer.desktop*
-rw-rw-rw-  1 mt mt     334993 114  2020 load.gif

root@Xubuntu18:/Desktop# gtk-launch libreoffice-writer.desktop

在这里插入图片描述
我的桌面上有一个动态图片,看看gtk-launch是否能打开?

root@Xubuntu18:/Desktop# ll
total 1367204
drwxrwxrwx  3 mt mt       4096 1123 19:30 ./
drwxr-xr-x 57 mt mt       4096 1122 08:30 ../
-rwxr-xr-x  1 mt mt      21390 921  2019 libreoffice-writer.desktop*
-rw-rw-rw-  1 mt mt     334993 114  2020 load.gif

root@Xubuntu18:/Desktop# gtk-launch load.gif
gtk-launch: no such application load.gif

从上面的报错的结果可以看出,gtk-launch只能打开应用程序,后缀为.desktop,并且在/usr/share/application/目录下存在的应用。

2、xdg-open

相比gtk-launch,xdg-open的能力比较全面一些,除了能打开桌面应用,还能打开图片,文档等等

所在位置: /usr/bin/xdg-open
使用方法:gtk-launc <app-name>

官方给出的使用介绍,比较简单,使用方法跟gtk-launch差不多

NAME
       xdg-open - opens a file or URL in the user's preferred application

SYNOPSIS
       xdg-open {file | URL}

       xdg-open {--help | --manual | --version}

DESCRIPTION
       xdg-open opens a file or URL in the user's preferred application. If a
       URL is provided the URL will be opened in the user's preferred web
       browser. If a file is provided the file will be opened in the preferred
       application for files of that type. xdg-open supports file, ftp, http
       and https URLs.

       xdg-open is for use inside a desktop session only. It is not
       recommended to use xdg-open as root.

OPTIONS
       --help
           Show command synopsis.

       --manual
           Show this manual page.

       --version
           Show the xdg-utils version information.

EXIT CODES
       An exit code of 0 indicates success while a non-zero exit code
       indicates failure. The following failure codes can be returned:

       1
           Error in command line syntax.

       2
           One of the files passed on the command line did not exist.

       3
           A required tool could not be found.

       4
           The action failed.

举个例子来理解一下使用

root@Xubuntu18:/Desktop# ll
total 1367204
drwxrwxrwx  3 root root   4096 1123 19:30 ./
drwxr-xr-x 57 root root   4096 1122 08:30 ../
-rwxr-xr-x  1 root root   21390 921  2019 libreoffice-writer.desktop*
-rw-rw-rw-  1 root root   334993 114  2020 load.gif

root@Xubuntu18:/Desktop#  xdg-open load.gif

在这里插入图片描述

3. gtk-launch 和 xdg-open的区别

从下面的表格中我们可以看出,xdg-open更强大,因此建议大家后续可以多使用xdg-open

功能点gtk-launchxdg-open
root 用户下打开应用YY
root 用户下打开图片NY
root 用户下打开TXT文档NY
root 用户下打开URLNY
普通用户下打开应用NY
普通 用户下打开图片NY
普通用户下打开TXT文档NY
普通用户下打开URLNY
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值