6

After moving a .desktop file into the "Desktop" location ~/Desktop, it show the icon on the Desktop. For the first run you have to trust the application. Is there any way to trust it directly without clicking? chmod +x *.Desktop is already done to make the file executable

enter image description here

1
  • 2
    Sorry my solution only works on KDE, but I found this older solution which may still work in bionic, though it would still be more work than simply using the trust dialog for less than the number of new .desktop entries you find inconvenient askubuntu.com/questions/969231/… Aug 29, 2018 at 13:02

4 Answers 4

17

Desktop files can be trusted via command line:

gio set Your_desktop_file.desktop "metadata::trusted" yes

and the trust state can be obtained by:

gio info Your_desktop_file.desktop | grep "metadata::trusted"

NOTE:

  • You have to run this command with the same user as the owner of the desktop files
  • It only works if you run the command in the gnome shell (not via SSH)
  • To do this automatically at logon, you have to make a logon script what will execute an script, as this only run when Gnome is started. For example you can create an .desktop file in ~/.config/autostart/ what execute some shell script including the gio commands
  • Make sure nautilus-desktop is really started. You can create an while loop with a sleep 1 till nautilus-desktop run
  • Icon's will not refresh automatically. You can achieve this with pressing F5 on the desktop or restart nautilus in your autostart script. killall nautilus && nautilus-desktop & (the last & sign is to make sure the rest of your code will run. Otherwise it will stop the script there till you close nautilus-desktop process again)
6
  • Awesome, glad it worked for you :) Aug 29, 2018 at 13:57
  • 1
    I have been looking for something like this for awhile so thanks to you both! However, I ran into a couple issues. First, the metadata file (~/.local/share/gvfs-metadata/home) was owned by root instead of my user for some reason. I used chown to fix that. However, now I get an error "gio: Unable to set metadata key". Still looking for a solution for this.
    – ajh158
    Sep 24, 2018 at 21:40
  • 1
    This happen when you run this script in another user. It have to run with the same owner and also really in gnome interface (or autostart). It is usefull when you run this for .desktop files located in ~/Desktop.
    – Sander
    Sep 25, 2018 at 21:09
  • 2
    It's not working for me. I checked the home metadata archive and .desktop owner, but I'm still receiving gio: Setting attribute metadata::trusted not supported error message
    – artu-hnrq
    Dec 12, 2019 at 7:22
  • I also had this problem. Running sudo chown $USER:$USER ~/.local/share/gvfs-metadata/home* solved it.
    – Apogentus
    Dec 8, 2020 at 15:52
4

Inspired by the great answer of @Sander, i am quite happy with this approach in Ubuntu 18.04. I use this in an automated VM creation setup, where first the desktop icons are placed, and then these scripts are prepared. They are only executed at the first start.

I create a desktop file ~/.config/autostart/desktop-truster.desktop with the following content:

[Desktop Entry]
Name=Desktop-Truster
Comment=Autostarter to trust all desktop files
Exec=~/.config/autostart/desktop-truster.sh
Type=Application

Next to it, a script ~/.config/autostart/desktop-truster.sh, which is invoked by the autostart desktop file:

#!/bin/bash
# Wait for nautilus-desktop
while ! pgrep -f 'nautilus-desktop' > /dev/null; do
  sleep 1
done
# Trust all desktop files
for i in ~/Desktop/*.desktop; do
  [ -f "${i}" ] || break
  gio set "${i}" "metadata::trusted" yes
done
# Restart nautilus, so that the changes take effect (otherwise we would have to press F5)
killall nautilus-desktop && nautilus-desktop &
# Remove X from this script, so that it won't be executed next time
chmod -x ${0}
2
  • Exactly like this, good job @Janos Only thing when you remove the X from itself it will not run this anymore in case you put new icons in
    – Sander
    Nov 28, 2018 at 15:20
  • 2
    On ubuntu 19.10 gio set "${i}" "metadata::trusted" yes didn't work. Instead, I used the following: gio set "${i}" "metadata::trusted" true and now it's all good. Jan 12, 2020 at 14:21
0

You have to make the .desktop files executable (trust), which you can do by going to ~/Desktop in a terminal and issue the following command

chmod u+x foo*

Obviously use a wildcard suitable for your needs, then refresh your desktop. That would be more effort than the trust mechanism for a single entry, but save time if you imported say more than n entries (where n is your number of max inconvenience).

There are ways of monitoring the directory and automatically doing this with external tools but I would advise you to seriously consider the security implication of doing such a thing.

But there is no way of doing this within your DE that I am aware of.

NOTE: This works on KDE but apparently does not work on Gnome.

4
  • 1
    It's not that I can not execute them. If I don't make it "trusted" with the chmod you are suggesting, I am not able at all to open the .desktop file. After I have done the chmod +x I still need to click on every single entry on the Desktop and click on the button "trust". I have edited my question and added an image.
    – Sander
    Aug 29, 2018 at 11:56
  • 1
    @Sander Oh, that's different to what I get, which is why I suggested that solution. I get a dialog showing me the command that will be run, asking if I want to run the application on first execution. After clicking continue, I never get the dialog again. But if I pre set the execution permission on the .desktop file, then I do not get the dialog on first or subsequent executions, it is simply trusted. Seems gnome and kde have different ideas about trust, I thought it was a Ubuntu mechanism and as such would be the same across DEs. Aug 29, 2018 at 12:39
  • 1
    Ah, you are using kde. Guess that this must be a difference then, yes. After you make it executable with chmod +x *.desktop it ask this question. When you click once on "Trust and Launch" it will work forever. It's just the first time. Looking forward for the special trick! Thank you anyway :-)
    – Sander
    Aug 29, 2018 at 13:04
  • @Sander found an old post that has a cli trick in it which I put in a comment under your question, maybe that will do it for you, wish you luck in your quest :) Aug 29, 2018 at 13:08
0

I've adapted Janos answer to work for my setup (/etc/skel is created through pam_mkdir on an Ubuntu 22.04), by placing the following in /etc/profile.d/desktop-truster.sh (all users need to be able to read that file):

#!/bin/bash
# Trust all desktop files
for i in ~/Desktop/*.desktop; do
  [ -f "${i}" ] || break
  gio set "${i}" "metadata::trusted" true
done

# Restart nautilus, so that the changes take effect (otherwise we would have to press F5)
nautilus -q

The changes made:

  • replaced yes with true
  • removed the waiting as that did not work
  • uses nautilus -q to restart nautilus

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .