I’ve worked out a brutaal workaround.
Turns out that it’s possible to have the finder ‘see’ icons selected in the window of the desktop, and the desktop itself, virtually at the same time.
If you select some on the desktop, then in the window of the desktop, then close the window, the icons on the actual deasktop are referred to by the script, sort of a ‘shadow’ effect I guess.
tell application "Finder"
try
set thelist to (selection as list)
set distribute to number of items in thelist
if distribute = 0 then
display dialog "There are no icons selected." & return & return & "Try selecting some, and running the script again." buttons {"OK"}
return
end if
if distribute = 1 then return -- Only one selected, so leave alone
set thisItem to item 1 of thelist
set EnclosingFolder to container of thisItem as string
if number of windows > 0 then
try
if name of window 1 = "Desktop" then
close window "Desktop"
display dialog "A Window of the Desktop was frontmost." & return & return & "If you selected Icons in it, and tried to organize them, it may seriously disrupt your actual Desktop Icon arrangement." & return & return & "However, if icons were also selected on the desktop at the same time, some alterations may occurr." buttons {"So, I've temporarily disabled it."}
set temp to (selection) as list
if number of items in temp = 0 then
open EnclosingFolder
return
else
open EnclosingFolder
display dialog "There appears to be " & number of items in temp & " icons selected on the desktop." & return & return & "Do you wish to Arrange them?." buttons {"No Thanks", "Arrange"}
if the button returned of the result is "No Thanks" then
return
end if
end if
end if
end try
end if
end try
end tell