Lists of files getting mangled

I’m working on (what I assume to be) a reasonably sized AppleScript that is meant to sort files into folders based on their extension, among other things.

In working on this script, I have been having problems with lists I keep of files getting mangled if I tell the finder to move or delete any files that I am keeping track of in one of these lists. It seems to me that the file list gets shuffled to replace the file that is missing.

For example, let’s say that a folder contains files A, B, C, and D, and I am keeping aliases of files A and C in a list. If I then tell the finder to delete C, the second item in that list will now be D, and if I tell the finder to delete A, then the list will contain {B, D} rather than {A, C}

I figured that this had something to do with the items in the list being references to elements of some data structure that the Finder maintains, so I had a function that is supposed to work on these files make its own copy of the list of files the command

copy the contents of file_list to my_file_list

This worked in that function, but when I tried to implement the same fix at other points in my code, it didn’t seem to make a difference - both copies of the list get mangled in the same way.

If anyone could tell me a)what gives? b) how do I avoid this and c) how do I tell if I have a reference to a value or the value itself, I would love you forever.

Hi, nauthiz.

You haven’t posted any of your problem code, but you’re probably right about having a Finder reference problem. Your “list of aliases” is probably no such thing, but possibly a list of Finder references to numbered items in the folder.

{file 1 of folder blah blah blah, file 3 of folder blah blah blah}

If you delete the third file in the folder, the fourth file obviously becomes the third file and the reference in the list now refers to that. Similarly, when you delete the first file, files 2 thru 4 are then files 1 thru 3.

A copy of this list won’t make any difference because the copy will contain the same references to numbered items.

It’s hard to be succinct about how to avoid the problem as you haven’t said how you created the list in the first place. It may even be that the list itself isn’t really a list at all, but a Finder reference to a group of files in the folder. Using this without resolving the reference first is a common cause of such problems:

-- Cycling through a Finder reference
tell application "Finder"
  repeat with thisFile in files of folder "fred" of desktop whose name contains "bert"
    -- Do something with thisFile
  end repeat
end tell

The first time round this repeat, the value of ‘thisFile’ is ‘item 1 of every file of folder “fred” of desktop of application “Finder” whose name contains “bert”’. If what you do to the file is to delete or move it, the next time round the loop, ‘item 2 of every file of folder “fred” of desktop of application “Finder” whose name contains “bert”’ will refer to a different (or even non-existent) file.

-- Resolving the Finder reference first
tell application "Finder"
  set myList to files of folder "fred" of desktop whose name contains "bert"
  repeat with thisFile myList
    -- Do something with thisFile
  end repeat
end tell

Here, the Finder reference is evaluated just once, before the loop, and the result is stored as an AppleScript list. This list contains Finder references, but these are to individually named files, which can thus be deleted without causing the same mix-up.

I hope this has helped, but please ask again with a few more details if it hasn’t.