Check index numbers of data in two lists

I have a question about how best to do somethng with lists.

I have two lists of file aliases and all of the files are located in one folder. The first list is of files selected in the Finder (selectedFiles) and the second list is of all files in the folder (allFiles).

I need to do the following:

  1. Get the item number of item 1 of selectedFiles within allFiles.

  2. Get the item number of item 2 of selectedFiles within allFiles

  3. Repeat step 2 for the remainder of the selectedFiles (i.e. item 3 through item -1 of selectedFiles).

  4. Prompt the user if the item number obtained in step 1 is higher than any of the item numbers obtained in steps 2 and 3.

I wrote the following which works but I have to think there’s a simpler method. Any suggestions are appreciated.


on checkFileOrder()
	--Set variable to index number of the first-selected file.
	repeat with a from 1 to count allFiles
		if item a of allFiles = item 1 of selectedFiles then
			if a = 1 then
				return
			else
				exit repeat
			end if
		end if
	end repeat
		--Check the index numbers of the rest of the selected files.
	repeat with b from 2 to count selectedFiles
		repeat with c from 1 to count allFiles
			if item c of allFiles = item b of selectedFiles then
				if c < a then
					display dialog "Files selected out of order." buttons {"OK"} ¬
						cancel button 1 default button 1 with title "PDF Add" with icon stop
				else
					exit repeat
				end if
			end if
		end repeat
	end repeat
end checkFileOrder

Hi peavine.

I’m not sure exactly what you want to do or why, since the order of items in a folder is no more to be relied upon than the order of items in a selection. But for the sake of the exercise, if I’m right in thinking you just want to check that the selected items are in the same order in both lists, a simple solution would be:

tell application "Finder"
	set allFiles to files of desktop as alias list
	set selectedFiles to selection as alias list
end tell

checkFileOrder(selectedFiles, allFiles)

on checkFileOrder(selectedFiles, allFiles)
	-- Build a list containing the selected files in the order they appear in allFiles.
	set matchedFiles to {}
	repeat with i from 1 to (count allFiles)
		set thisFile to item i of allFiles
		if (thisFile is in selectedFiles) then set end of matchedFiles to thisFile
	end repeat
	
	-- If matchedFiles and selectedFiles aren't exactly the same (size, contents, and order), tell the user.
	if (matchedFiles is not equal to selectedFiles) then
		display dialog "Files selected out of order." buttons {"OK"} ¬
			cancel button 1 default button 1 with title "PDF Add" with icon stop
	end if
end checkFileOrder

But practically, you may find it more useful to force the lists to be in the same order like so:

tell application "Finder"
	set allFiles to (sort files of desktop by name)
	set selectedFiles to (sort selection by name)
end tell

In this case, the results are lists of Finder references rather than of aliases, so you’d need to use the Finder to interpret them, at least initially. And you’d have to index the items by their positions in the lists rather than in the folder, but that’s a better idea anyway. :slight_smile:

Thanks Nigel. Your first suggestion worked perfectly in my existing script. I then rewrote the script along the lines in your second suggestion and that was even better–much cleaner and not prone to error. I use this script a lot, so it’s nice to get it working well.