G’day
This has me flummoxed. The script works fine, until I save and use it as an App, then the ‘Write to file’ command writes a ‘long’ error to the file.
Any thoughts please.
EDIT I should have said that as an App, it only saves the position of 1 icon, then the rest is seen as ‘long’, even when I write them to the file individually
-- Restore Desktop Icons
-- Not Bloody Copyright
-- By Santa 2006
-- Sunny Land of Oz
-- bec9@tpg.com.au
-- Rough as Guts Software
-- Version 1.2
global ReturnIconsToPosition
set ReturnIconsToPosition to {}
global thelist
set thelist to {}
global WindowExists
set WindowExists to false
global thePath
global theStoragePath
global TheButton
set TheButton to {}
tell application "Finder"
try
set temp to name of the front window
set WindowExists to true
on error
set WindowExists to false
end try
end tell
if WindowExists then
display dialog "There is a window '" & temp & "' open and at the front. " & return & return & "Do you want to Save or Restore the icon positions in...." & return & return & "the Window, " & return & "or, " & return & "the Desktop?" buttons {"Cancel", "The Window", "The Desktop"}
set TheButton to the button returned of the result
if TheButton = "The Desktop" then
set WindowExists to false
my DoDesktop()
end if
if TheButton = "The Window" then
set WindowExists to true
my DoDesktop()
end if
else
my DoDesktop()
end if
-- ===========================================================================
on DoDesktop()
if my CheckIfFileExists() then
set tempString to "Save the Desktop Icon Positions, or"
if WindowExists then set tempString to "Save the Windows Icon Positions, or"
display dialog "Do you want to..." & return & return & tempString & return & return & "Restore them back to a Saved Position?" buttons {"Cancel", "Save Positions", "Restore Positions"}
set TheButton to the button returned of the result
if TheButton = "Save Positions" then
my GetPositions()
if TheButton = "OK" then return
my WritePositions()
end if
if TheButton = "Restore Positions" then
my ReadOldPositions()
my ReplacePositions()
end if
else
if TheButton = "The Window" then
display dialog "There does not appear to be any saved Icon positions for this Window." & return & return & "Do you want to save the Windows Icon Positions." & return buttons {"Cancel", "Save Window Positions"}
set TheButton to the button returned of the result
if TheButton = "Save Window Positions" then set WindowExists to true
my GetPositions()
if TheButton = "OK" then return --<-- Nothing to be saved
my WritePositions()
else
display dialog "There does not appear to be any saved positions for the Desktop." & return & return & "Do you want to save the Desktop Icon Positions." & return buttons {"Cancel", "Save Desktop Positions"}
set TheButton to the button returned of the result
if TheButton = "Save Desktop Positions" then set WindowExists to false
my GetPositions()
if TheButton = "OK" then return --<-- Nothing to be saved
my WritePositions()
end if
end if
end DoDesktop
on GetPositions()
tell application "Finder"
try
if WindowExists then
set temp to name of the front window
select every item of window temp
else
select every item of the desktop
end if
set thelist to (selection as list)
if number of items in thelist = 0 then
display dialog "Please only use me if you have some icons for me to remember positions for." buttons {"OK"} --<-- Nothing to be saved
set TheButton to the button returned of the result
if TheButton = "OK" then return
end if
set x to 0
repeat with distribute in thelist
if WindowExists then
set end of ReturnIconsToPosition to position of distribute
else
set end of ReturnIconsToPosition to desktop position of distribute
end if
end repeat
if exists window "Desktop" then close window "Desktop"
end try
end tell
end GetPositions
on WritePositions()
my SetPath()
set fRef to (open for access file theStoragePath with write permission)
try
set TheWriteList to {}
set eof fRef to 0
set i to 0
repeat with theItem in thelist
set i to i + 1
set the TheWriteList to TheWriteList & (theItem as string) & item 1 of item i of ReturnIconsToPosition & item 2 of item i of ReturnIconsToPosition
end repeat
write TheWriteList to fRef
end try
close access fRef
display dialog "The Icon Positions have been saved." buttons {"OK"} giving up after 4
end WritePositions
on ReadOldPositions()
set thelist to {}
set ReturnIconsToPosition to {}
set temp to {}
set WholeList to {}
my SetPath()
tell application "Finder"
try
set WholeList to (read file (theStoragePath) as list)
on error
display dialog "There does not seem to be any positions saved." buttons "Cancel"
end try
end tell
set i to 1
repeat until i > (number of items in WholeList)
set end of thelist to item i of WholeList
set temp1 to {item (i + 1) of WholeList as number}
set temp2 to {item (i + 2) of WholeList as number}
set temp to temp1 & temp2
set end of ReturnIconsToPosition to temp
set i to i + 3
end repeat
end ReadOldPositions
on ReplacePositions()
tell application "Finder"
try
set i to 0
set IconsReplaced to 0
set IconsNotReplaced to 0
repeat with distribute in thelist
set i to i + 1
if WindowExists then
if item distribute exists then
set position of item distribute to item i of ReturnIconsToPosition
set IconsReplaced to IconsReplaced + 1
else
set IconsNotReplaced to IconsNotReplaced + 1
end if
else
if item distribute exists then
set desktop position of item distribute to item i of ReturnIconsToPosition
set IconsReplaced to IconsReplaced + 1
else
set IconsNotReplaced to IconsNotReplaced + 1
end if
end if
end repeat
end try
set TextString to "There were " & IconsReplaced & " Icons repositioned"
if IconsReplaced = 0 then set TextString to "There were no Icons repositioned"
if IconsReplaced = 1 then set TextString to "There was one Icon repositioned"
if IconsNotReplaced = 0 then set TextString to TextString & "."
if IconsNotReplaced = 1 then set TextString to TextString & ", and one was missing."
if IconsNotReplaced > 1 then set TextString to TextString & ", and " & IconsNotReplaced & " were missing."
display dialog TextString buttons "OK" giving up after 6
end tell
end ReplacePositions
on SetPath()
tell application "Finder"
try
if WindowExists then
set thePath to (folder of the front window) as string
else
set thePath to (path to desktop folder as string)
end if
on error
return false
end try
end tell
set theStoragePath to thePath & "Icon Storage.dat"
end SetPath
on CheckIfFileExists()
my SetPath()
tell application "Finder"
try
set WholeList to (read file (theStoragePath) as list)
return true
on error
return false
end try
end tell
end CheckIfFileExists
Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)