如何为 Linux 桌面条目文件指定带有相对路径的图标?
对于我的一个 Linux 应用程序,我有应用程序二进制文件、一个 launcher.sh 脚本(针对 LD_LIBRARY_PATH)和一个 .desktop 文件,所有这些都位于同一文件夹中。
我想使用图标的相对路径而不是绝对路径。
我已经尝试过:
Icon=app.svg
Icon=./app.svg
Icon=$PWD/app.svg
Icon=$(dirname %k)/app.svg
但这些都不起作用(只有 Icon=/path/to/app.svg
)。如果无法指定相对路径并且我必须使用解决方法,我想我可以在每次运行 launcher.sh 脚本时重新生成 .desktop 文件中的图标路径。
最好的方法是什么?使用 sed
或其他一些带有伪正则表达式的替换实用程序,例如 Icon=([valid path chars]+)\n
也许?
For one of my Linux applications, I have the application binary, a launcher.sh script (for the LD_LIBRARY_PATH) and a .desktop file, all in the same folder.
I'd like to use a relative path rather than an absolute path for the icon.
I've tried:
Icon=app.svg
Icon=./app.svg
Icon=$PWD/app.svg
Icon=$(dirname %k)/app.svg
but none of these work (only Icon=/path/to/app.svg
). If it's not possible to specify a relative path and I must use a workaround, I was thinking I could regenerate the icon path in the .desktop file every time the launcher.sh script is run.
What would be the best way to do that? Using sed
or some other replacement utility with a pseudo-regex like Icon=([valid path chars]+)\n
perhaps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过更多研究后,据我所知,似乎不可能在桌面条目文件中指定图标的相对路径。
我使用的解决方法是将以下代码添加到 launcher.sh 脚本的末尾:
这将在每次运行启动器脚本时更新图标的路径,并且由于 .desktop 文件指向启动器脚本,因此单击.desktop 文件有效地更新了其图标。
我知道您可以使用
cat
或 -i 选项来缩短上述代码,但我了解到我使用的解决方案更可靠。如果有人有更多相关信息,请发表评论。After doing some more research it doesn't look like it's possible to specify relative paths for an icon in a desktop entry file as far as I can see.
The workaround I used was to add the following code to the end of my launcher.sh script:
This will update the path of the icon each time the launcher script is run, and since the .desktop file points to the launcher script, clicking the .desktop file effectively updates its icon.
I know you could use
cat
or the -i option to shorten the above code but I've read that the solution I used is more reliable. If anyone has further information on that please do post a comment.确实,FreeDesktop 规范
不允许相对路径:
解决方法足够了,
尽管它可能不适用于菜单和面板启动器。
但如果您愿意修补桌面文件
运行
launcher.sh
脚本时,为什么不实际安装图标?
您可以用两行来完成:
然后放入
桌面文件(
app
只是不带文件扩展名的文件名)。这是定位图标的预期机制
没有绝对路径,
并将确保图标显示在菜单和自定义启动器中。
规范有这样的说法:
可以做到这一点的一种方法
使用
xdg-icon-resource
命令,例如xdg-icon-resource
不支持 SVG 图像,
实际上,这完成了同样的事情:(
这不是拼写错误:将 SVG 文件放在
48x48/apps
文件夹中菜单和面板将会非常满意。)
对于菜单,最好在安装后更新图标缓存。
然后,您可以简单地将
iconstring
指定为example-app
,如下所示:这不是相对路径,
但它解决了必须使用绝对路径的问题
如果桌面文件移动到其他位置也不会中断。
对于它的价值来说,
支持相对路径
在 FreeDesktop 邮件列表上进行了讨论
时间回到 2008 年 9 月:
https://lists.freedesktop.org/archives/xdg/2008-September /009940.html
我能找到的唯一反驳
该提案在这里:
https://lists.freedesktop.org/archives/xdg/2008-September /009962.html
相关问题:
相关链接:
It's true that the FreeDesktop specification
does not permit relative paths:
The workaround is adequate,
although it probably won't work for menus and panel launchers.
But if you're comfortable patching the desktop file
when running the
launcher.sh
script,why not actually install the icon?
You can do it in two lines:
and then put
in the desktop file (
app
is just the filename without a file extension).This is the intended mechanism for locating icons
that don't have an absolute path,
and will ensure the icons show up in menus and custom launchers.
The spec has this to say:
One way this can be done
is with the
xdg-icon-resource
command, e.g.However,
xdg-icon-resource
does not support SVG images,
and in practice this accomplishes the same thing:
(That's not a typo: put the SVG file in the
48x48/apps
folderand the menus and panels will be perfectly happy.)
For menus, it's a good idea to update the icon cache after installing.
Then you can simply give the
iconstring
asexample-app
like this:This is not a relative path,
but it solves the problem of having to use an absolute path
and won't break if the desktop file is moved to a different location.
For what it's worth,
support for relative paths
was discussed on the FreeDesktop mailing list
back in September 2008:
https://lists.freedesktop.org/archives/xdg/2008-September/009940.html
The only counterargument I was able to find
to this proposal is here:
https://lists.freedesktop.org/archives/xdg/2008-September/009962.html
Related questions:
Relevant links:
您可以使用
echo $(echo ~)
使用命令输出,或使用echo $(echo $var)
作为变量例如:
如果您是对多行 echo 命令感兴趣,请查看此链接
You can use
echo $(echo ~)
to use command output orecho $(echo $var)
for variablesFor example:
If you are interested in multi line echo command check out this link