linux 命令行进行桌面图标的打开
近期在处理一个需求,需要在代码中打开桌面的某个图标,因此,做了一些搜索,最终发现,有两个比较好用的命令,下面来讲解一下。1、gtk-launch在linux系统一般已经集成这个工具所在位置: /usr/bin/gtk-launch使用方法:gtk-launc <app-name>下面是官方给出的指导NAMEgtk-launch - Launch an applicationSYNOPS
·
近期在处理一个需求,需要在代码中打开桌面的某个图标,因此,做了一些搜索,最终发现,有两个比较好用的命令,下面来讲解一下。
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 11月 23 19:30 ./
drwxr-xr-x 57 mt mt 4096 11月 22 08:30 ../
-rwxr-xr-x 1 mt mt 21390 9月 21 2019 libreoffice-writer.desktop*
-rw-rw-rw- 1 mt mt 334993 11月 4 2020 load.gif
root@Xubuntu18:/Desktop# gtk-launch libreoffice-writer.desktop
我的桌面上有一个动态图片,看看gtk-launch是否能打开?
root@Xubuntu18:/Desktop# ll
total 1367204
drwxrwxrwx 3 mt mt 4096 11月 23 19:30 ./
drwxr-xr-x 57 mt mt 4096 11月 22 08:30 ../
-rwxr-xr-x 1 mt mt 21390 9月 21 2019 libreoffice-writer.desktop*
-rw-rw-rw- 1 mt mt 334993 11月 4 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 11月 23 19:30 ./
drwxr-xr-x 57 root root 4096 11月 22 08:30 ../
-rwxr-xr-x 1 root root 21390 9月 21 2019 libreoffice-writer.desktop*
-rw-rw-rw- 1 root root 334993 11月 4 2020 load.gif
root@Xubuntu18:/Desktop# xdg-open load.gif
3. gtk-launch 和 xdg-open的区别
从下面的表格中我们可以看出,xdg-open更强大,因此建议大家后续可以多使用xdg-open
功能点 | gtk-launch | xdg-open |
---|---|---|
root 用户下打开应用 | Y | Y |
root 用户下打开图片 | N | Y |
root 用户下打开TXT文档 | N | Y |
root 用户下打开URL | N | Y |
普通用户下打开应用 | N | Y |
普通 用户下打开图片 | N | Y |
普通用户下打开TXT文档 | N | Y |
普通用户下打开URL | N | Y |
更多推荐
已为社区贡献3条内容
所有评论(0)