deek5 deek5 - 11 days ago 6
AppleScript Question

How to save your desktop elements (icons, files,folder ) always on the same place with applescript

With the script we are going to create another script where will be store position of all the elements of the desktop, the created script will be compile and usable to put back in place all the elements previously protected.

/adesktopsave/deskico.txt it is the temporary file which will be of use to the compilation.
/adesktopsave/savedicoposition.scpt It is the script of saving that is compiled to be used with applescrit

All the names used here exist that just for the example. These names have no particular property.

It is just necessary to plan to create a folder before using this script. Here it is:
/adesktopsave

Something else, end of line (\n) after " try
"
also " end try
"
and & "}
")

Are very important to respect so that the text is usable.

tell application "Finder" to set theList to {name, desktop position} of items of desktop
try
do shell script "rm -f /adesktopsave/deskico.txt"

do shell script "echo tell application " & quoted form of (quote & "Finder" & quote) & return & " >>/adesktopsave/deskico.txt"

end try

set n to (count (first item of theList))


repeat with i from 1 to n

set inp to do shell script "echo " & quoted form of (item i of first item of theList)

set xy to (item i of second item of theList)

set AppleScript's text item delimiters to ","

set xyz to do shell script "echo " & xy
set wxyz to ("{" & xyz & "}
")

set ligne to "try
" & "set desktop position of item " & quoted form of (quote & inp & quote) & " of desktop to " & quoted form of (wxyz) & "end try
"
set ligne to do shell script "echo " & ligne & " >>/adesktopsave/deskico.txt"

end repeat

do shell script "echo " & "end tell" & return & " >>/adesktopsave/deskico.txt"

display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"

if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt" & " /adesktopsave/deskico.txt"
end if

return n

Answer

We can lighten the script to its simplest expression. At the risk of having errors can be.

set ligne to ""
do shell script "mkdir -p  /adesktopsave"
tell application "Finder" to set {names, positions} to {name, desktop position} of items of the desktop
set ligne to "tell application \"Finder\"
"
set n to (count names)
set AppleScript's text item delimiters to ","
repeat with i from 1 to n
set ligne to ligne & ("try
" & "set desktop position of item " & (quote & item i of names & quote) & "  to  {" & item i of positions & "}
end try
")
end repeat
set ligne to ligne & ("end tell" & return)
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
  if (button returned of result) is "Cancel" then 
    set n to do shell script "echo " & n
else
  do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt  -e  "  & quoted form of ligne
end if
set AppleScript's text item delimiters to ""
tell application "Finder" to open POSIX file "/adesktopsave/savedicoposition.scpt"
return n
Comments