Trust desktop icons without clicking them manually in Ubuntu 18.04 Gnome 3

Solution 1:

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)

Solution 2:

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}