Recording and Restoring Icon positions

I would appreciate some help with the
following task.

I want to be able to select a folder and record
the names and icon positions and color of all the files and folders
(and similarly for all subfolders) it contains, into a text file on my hard
drive.

At some future point in time I would like to open the
file and restore all file and folder icon positions.

All folders should be set to ‘View Options’
‘Small icons’, with ‘Snap to grid’ = OFF
and ‘Keep Arranged’ = OFF

I am running Mac OS 9.1

Any advice or help would be much appreciated.

Thank you
Charles W

You haven’t said what aspect of the task is causing you difficulty. However, since it’s Christmas, here are a couple of complete, free scripts that have been tested in OS 9.2.2 and work well with modestly sized folder hierarchies. They should at least get you started. They won’t work in OS X.

The “record” script can be saved as an application and used as a droplet. It can also be used as a compiled script to work on the Finder selection, provided that the application running it isn’t the Finder itself. (See later.) The comments show what it does. If there are many items in the target folder hierarchy, the script may appear to be doing nothing for several seconds, so a dialog beeps up at the end to say when it’s finished. The saved settings file is stored in the Preferences folder. Although you specified a “text file”, I’ve assumed you don’t actually need to read it yourself. It’s easier and faster to save the information in AppleScript list format.

on run
  tell application "Finder"
    my recordHierarchyViews(item 1 of selection)
  end tell
end run

on open droppedItems
  recordHierarchyViews(item 1 of droppedItems)
end open

on recordHierarchyViews(selectedFolder)
  tell application "Finder"
    activate
    -- Get the selected/dropped item as a Finder reference.
    set selectedFolder to item (selectedFolder as string)
    if (selectedFolder's class is not in {folder, disk}) then return
    
    -- Create a path for the settings file.
    set prefsFilePath to my makePrefsFilePath(selectedFolder's name)
    
    -- Sort out the selected folder's view. (Its window must be open for this.)
    open selectedFolder
    tell (selectedFolder's container window)
      set {has custom view settings, spatial view arrangement, view} to {true, not arranged, small icon}
      update
      close
    end tell
    
    -- Sort out the views of any subfolders. (Ditto.)
    tell (selectedFolder's entire contents)
      if ((count folders) > 0) then
        open folders
        tell (container window of folders)
          set {has custom view settings, spatial view arrangement, view} to {true, not arranged, small icon}
          update it
          close
        end tell
      end if
      
      -- *Then* get the paths, positions, and label indices of all the contents.
      set zero to ASCII character 0
      set astid to AppleScript's text item delimiters
      set AppleScript's text item delimiters to zero
      set thePaths to it as string
      set AppleScript's text item delimiters to astid
      considering case
        if (thePaths contains zero) then -- there's more than one item.
          set details to {thePaths, position, label index}
        else if ((count thePaths) > 0) then -- there's only one item.
          set details to {thePaths, {position}, {label index}}
        else -- the selected folder's empty.
          set details to {thePaths, {}, {}}
        end if
      end considering
    end tell
    
    -- Also get the path of the selected folder itself.
    set beginning of details to selectedFolder as string
  end tell
  
  -- Write the listed details to file.
  set fRef to (open for access file prefsFilePath with write permission)
  try
    set eof fRef to 0
    write details to fRef
  on error msg
    display dialog msg
  end try
  close access fRef
  
  beep 2
  tell application (path to frontmost application as string)
    display dialog "Done!" buttons {"OK"} default button 1 with icon note giving up after 5
  end tell
  return
end recordHierarchyViews

-- Get a name for the settings file and make a path setting it in the Preferences folder.
on makePrefsFilePath(folderName)
  if ((count folderName) > 26) then set folderName to text 1 thru 26 of folderName
  set folderName to folderName & ".fhvs"
  tell application "Finder" -- currently the frontmost app.
    set prefsFileName to text returned of (display dialog ¬
      "Please enter a name for the folder hierarchy view settings file, which will be saved in the Preferences folder...." default answer ¬
      folderName with icon note)
  end tell
  
  return (path to preferences as string) & prefsFileName
end makePrefsFilePath

Continued next post…

… Continued.

The “restore” script can likewise be used as an application or as a compiled script, with the same limitation about not being run by the Finder itself. (See below.) It prompts the user to choose a settings file and restores the hierarchy associated with that file.

restoreHierarchyViews()

on restoreHierarchyViews()
  -- Select the view settings file and read the listed details from it.
  set prefsFilePath to getPrefsFilePath()
  set {selectedFolder, thePaths, thePositions, theLabelIndices} to (read file prefsFilePath from 1 as list)
  
  tell application "Finder"
    set selectedFolder to item selectedFolder
    
    -- Sort out the selected folder's view. (Its window must be open for this.)
    open selectedFolder
    tell (selectedFolder's container window)
      set {has custom view settings, spatial view arrangement, view} to {true, not arranged, small icon}
      update
      close
    end tell
    
    -- Sort out the views of any subfolders. (Ditto.)
    tell (selectedFolder's entire contents)
      if ((count folders) > 0) then
        open folders
        tell (container window of folders)
          set {has custom view settings, spatial view arrangement, view} to {true, not arranged, small icon}
          update
          close
        end tell
      end if
    end tell
    
    -- Apply the stored positions and label indices.
    if (count thePaths) > 0 then
      set astid to AppleScript's text item delimiters
      set AppleScript's text item delimiters to ASCII character 0
      set thePaths to thePaths's text items
      set AppleScript's text item delimiters to astid
      repeat with i from 1 to (count thePaths)
        try
          tell item (item i of thePaths)
            set {position, label index} to {item i of thePositions, item i of theLabelIndices}
          end tell
        on error msg number -10006
          display dialog "Problem while positioning item "" & item i of thePaths & ""." & return & ¬
            "It may no longer exist." buttons {"OK"} default button 1 with icon note
        end try
      end repeat
    end if
    
    beep 2
    display dialog "Done!" buttons {"OK"} default button 1 with icon note giving up after 5
    return
  end tell
end restoreHierarchyViews

-- Choose the settings file from which to work and return the path to it.
on getPrefsFilePath()
  tell application "Finder"
    activate
    set possibilities to name of files of preferences folder whose name ends with ".fhvs"
    set theChoice to (choose from list possibilities with prompt ¬
      "Please select the settings file for the folder hierarchy whose view settings you want to restore....")
  end tell
  if (theChoice is false) then error number -128
  
  return (path to preferences as string) & item 1 of theChoice
end getPrefsFilePath

I don’t know the technical reason, but the scripts don’t execute properly if run as compiled scripts by the Finder. This means that if they’re selected from OSA Menu, and ‘Run scripts more compatibly’ isn’t checked in OSA Menu’s preferences, they won’t work properly if the Finder’s the frontmost application at the time. However, if they’re saved as applications, they can be opened by the Finder and then they can run themselves. So, you could use this script instead in OSA Menu to open the “record” script:

tell application "Finder"
  open selection using application file "Record Hierarchy Settings" of folder "blah:blah:blah:" -- depending on what you've called the "record" script and where you've saved it.
end tell

… and this to open the “restore” script:

tell application "Finder"
  open application file "Restore Hierarchy Settings" of folder "blah:blah:blah:" -- ditto.
end tell

Nigel

Many thanks for posting your reply…To be honest I tried to get a recursive script running
and made a complete mess of it. I usually dont attempt too much in AppleScript so dont have the expertese to attempt this on my own…

Where did you find these scripts. I have been looking all over the internet for something like this.

Thanks

CharlesW

I have had some sucess using the script you provided but have decided to try a slightly diffrent approach and would appreciate some additional assistance.

Instead of saving and restoring the icon positions I would like to attempt the following.

Using 2 diffrent folders (on diffrent servers) Lets call them a 'Master Folder ’ and a ‘Slave’
I would like to setp thru all the files and folders in the ‘Master’ and set the Icon positions
of all the files in the ‘Slave’ to the same location.

Its possible that some files may not exist in the ‘Slave’ so I need to ignore or set over them.

Can you help to get me started on this approach and if ( and when ) I need more help
I can come back…

With thanks for all the help so far…

Charles W