This is a handy Desktop application on virtually all version of MacOS since Tiger. Create the app with Apple Script, and place it on your Desktop. Double clicking it will launch it, and cause it to "Save Desktop", meaning it will remember where all the Desktop icons are located. If your Desktop gets disrupted, all you need to do is drag any Desktop icon onto the DIM2.2.app icon, and it will say 'Restoring Desktop', which it does almost instantly. I love it because sometimes I'll double-click on some icon, and it will 'jump' to some other place on the Desktop. I can quickly restore everything. One thing you should remember is to double-click DIM2.2.app any time you add or subtract icons from your Desktop. You want DIM2 to know where everything is located. I added the prompt at the start as a safeguard in case you meant to restore. Here's the code:
-- original DIM 2.0 code from Gregory J Parker
-- parameterized interface by Dick Guertin
-- set properties
property iconsall : {} -- list of all icons on desktop
property positionall : {} -- corresponding list of positions of the icons on desktop
property screenSize : {} -- screen size for the previous lists
property prompt_p : true -- should user be prompted or to just 'Restore' icon positions?
-- DIM was told to run (double-click, automatic, etc.)
on run argv -- normal double-click run, or shell-script caller
display alert "Do you wish to save?" buttons {"Cancel", "OK"} default button ("OK") giving up after 20
if button returned of result is not "OK" then
return
end if
if (count of iconsall) > 0 then
set rtn to ""
if (argv is not "") then
try
get item 1 of argv
set rtn to return
end try
if (rtn is not "") then
repeat with arg in argv
set rtn to (arg as text)
end repeat
end if
if (rtn is not "") then
if rtn is "2" then
restore()
else
update()
end if
return
end if
end if
end if
update()
end run
-- something was dragged onto DIM- do restore if possible
on open x -- get user request
if (count of iconsall) = 0 then -- first time run
update()
else -- not a first time run
restore()
end if
end open
on update()
-- store icon names & positions and screen size
copy {} to iconsall -- first zero the stuff out
copy {} to positionall
copy {} to screenSize
tell application "Finder" -- yeah, delete old info (if any)
activate
-- new bit from Stephane Madrau to make it 1.1+ much faster
set iconsall to name of items of desktop -- names of all the items on desktop
set positionall to desktop position of items of desktop -- corresponding list of their positions
copy the bounds of the desktop's window to the screenSize -- also store the size of the current desktop
say "Desktop Saved"
end tell
end update
on restore()
-- want to restore the icon positions
tell application "Finder"
activate
copy the bounds of the desktop's window to the newscreensize -- get new desktop size
set rSet to false -- assume we don't have to reset view option
set aSet to arrangement of (icon view options of window of desktop)
if (aSet is not "not arranged") then -- is option something other than 'not arranged'?
set arrangement of (icon view options of window of desktop) to not arranged -- yeah, set it, and set flag to restore it
set rSet to true
end if
say "Restoring Desktop"
end tell
-- let's try to do this fast: either screen size same or not
if newscreensize = screenSize then -- screen size the same, avoid math and just set positions
repeat with i from 1 to number of items of iconsall -- for each icon...
ignoring application responses -- added ignoring segment implied from Stephane, since we don't care how the Finder responds
-- set icon to new position if it exists, otherwise punt
tell application "Finder" to set desktop position of item (item i of iconsall) of desktop to ¬
{item 1 of (item i of positionall), ¬
item 2 of (item i of positionall)} -- new position
end ignoring
end repeat
else -- screen size is changed, do some math to scale positions
set newdx to item 1 of newscreensize -- exclude the menu bar, newdx should equal 0, but ...
set newdy to item 2 of newscreensize
set xrel to ((item 3 of newscreensize) - newdx) / ((item 3 of screenSize) - (item 1 of screenSize)) -- horizontal scaling
set yrel to ((item 4 of newscreensize) - newdy) / ((item 4 of screenSize) - (item 2 of screenSize)) -- vertical scaling
repeat with i from 1 to number of items of iconsall
ignoring application responses
tell application "Finder" to set desktop position of item (item i of iconsall) of desktop to ¬
{newdx + (0.5 + ((item 1 of (item i of positionall)) - newdx) * xrel) div 1, ¬
newdy + (0.5 + ((item 2 of (item i of positionall)) - newdy) * yrel) div 1} -- new position
end ignoring
end repeat
end if
if rSet = true then -- do we need to reset the view option?
ignoring application responses -- yeah, do it. quickly, i hope
tell application "Finder" to set arrangement of (icon view options of window of desktop) to aSet
end ignoring
end if
end restore