How to save and restore a desktop icons positions in MATE?

I'm currently using Ubuntu 18.04 LTS as daily system. I have never faced the problem with misplaced desktop icons. I'm using MATE on Ubuntu since 12.04 LTS.

I did some research. Really the problem is deeper.

Every icon has its own metadata attribute. So one can:

  • read it by

    gio info -a "metadata::caja-icon-position" ~/Desktop/file1.txt
    

    to get something like

    attributes:
      metadata::caja-icon-position: 922,382
    
  • write it by

    gio set -t string ~/Desktop/file1.txt metadata::caja-icon-position 300,400
    

    and ask Caja to refresh the desktop by calling caja -q.

There is script elsewhere. But it does not work.
Also there is a script on french forum. But initially it also have issues. The adapted version of the script is below.
To use it you have to install xdotool package by sudo apt-get install xdotool and then execute single long command below to create the script programmatically:

cat << \EOF > ~/prepare_restore_icons.sh
#!/bin/bash
# name and path of the script to be created (to be modified if necessary) 
file=~/restore_icons.sh

echo '#!/bin/bash'>$file
icons_xy()
{
    for i in ~/Desktop/*
     do echo -n gio set '"'"$i"'"';gio info --attributes=metadata::caja-icon-position "$i"|grep metadata
    done

}
icons_xy >>$file
sed -i 's/n:/n/g' $file

chmod u+x $file

# focus the Desktop and simulate pressing F5 shortened to refresh the Desktop (to be adapted according to the Desktop used, here Mate) 
echo 'id=$(xdotool search --classname desktop) ; xdotool windowfocus --sync $id; xdotool key F5'>>$file
EOF

If understand things correctly, then you use the script as follows:

  1. run sh ~/prepare_restore_icons.sh. It will save current icon positions to the second script.
  2. run second script by ~/restore_icons.sh

Note that these scripts cause creation of ~/.config/caja/desktop-metadata file. Its contents may change on desktop update.

The overall problem is very interesting. I still hope to get the solution of your problem. Thank you!