+64-3-3595735

Home » Tips and Hints » Linux » Locking And Restoring Desktop Icons In Mint Linux Mate / Caja Desktop

Locking And Restoring Desktop Icons In Mint Linux Mate / Caja Desktop

I wanted a way to lock my desktop icons in place in Mint Linux Mate ( Linux Mint 22 Wilma base: Ubuntu 24.04 noble)

I also wanted a way to restore them to a set position if they got moved, maybe after a monitor failed or my laptop was off dock. I tried a few solutions but none worked quite right so here is my working solution to restore desktop icons if they are moved.  This will be especially useful if you have a disabled or elderly relative whose hands shake, or they accidentally move icons around and get confused.

Locking Desktop Icons

  • Move the icons to the position you want them
  • Right click the desktop
  • Choose Lock Icons Position

Saving And Restoring Desktop Icon Positions

1) You will need to install xtoolsdo

sudo apt-get install xdotool

2) Then create a script called backup-desktop.sh (I located mine in /usr/local/bin)

#!/bin/bash
# name and path of the script to be created (to be modified if necessary)
file=~/restore_desktop.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 '/usr/local/bin/refresh-desktop.sh'>>$file

This will get all the locations of all your desktop icons and create a file called restore-desktop.sh in your home folder.

3) You will also need another script called refresh-desktop.sh  ( again I placed mine in /usr/local/bin/)

This script is the equivalent of clicking on your desktop and hitting the F5 (refresh) button.


#!/bin/bash
echo "Getting Id for Desktop then refreshing it by simulating F5"
# Get a list of window IDs for Caja
window_ids=$(xdotool search --classname "desktop")
# Iterate through each window ID
for id in $window_ids; do
# Get the window name for the current window ID
window_name=$(xdotool getwindowname $id)
echo "Checking ID $id : $window_name"
# Check if the window name is "Desktop"
if [[ "$window_name" == "Desktop" ]]; then
# Run your script with the window ID
echo "Found Desktop window with ID: $id"
# Replace the following line with the script you want to run
xdotool windowfocus --sync $id
xdotool key F5
echo "Completed refreshing icons"
exit 0
fi
done
# If no matching window was found
echo "No Desktop window found."
exit 0

You then need to make them all run-able


chmod +x /usr/local/bin/backup-desktop.sh
chmod +x /usr/local/bin/refresh-desktop.sh
chmod +x ~/restore-desktop.sh

 

To backup your desktop icon positions run /usr/local/bin/backup-desktop.sh

To restore icons run ~/restore-desktop.sh

Job done!

I have placed a copy of the backup and refresh scripts in this text file: backup-restore-desktop

The restore-desktop will be made by the backup-desktop being run.

Thanks to Norbet_X ( https://ubuntu-mate.community/t/how-to-save-and-restore-a-desktop-icons-positions-in-mate/23651/21?page=2 ) and ChatGPT for pointing the way, helping with the research and giving me enough clues to make this work.