Poor man's sort list. Or not?

Hi all,

I had to sort a list (of a folder with images; selected by the user) based on the order of a text file (containing unique numbers and specs; 360 lines).
The images should be placed in the order of the text file; the file names of the corresponding images are not in the same order, but alphabetically.
Then I came up with this, which works fine for me. Now I was thinking if this is a reliable way to do, or if there is a faster possibility. (Just to learn some more about Applescript.)

Thanks to anyone looking at it.
kjeld

PS: The script is an extract of a bigger one. If it’s necessary to post the complete one, let me know.


set specsFile to (choose file with prompt "Select the text file with specifications.")
set thisfile to read file (specsFile as string) using delimiter {return}

--Thisfile looks like (sorted on the year of production):
--AC.1.0395.073 [tab] blahblah, 1994.
--AC.1.0395.074 [tab] blahblah, 1996.
--AC.1.0196.144 [tab] blahblah, 1998.
--etc.

--Select images of the chosen folder.
set thefolder to (choose folder with prompt "Select the folder with images.")
set folderList to (list folder (thefolder as alias) without invisibles)
set picList to (choose from list folderList with prompt "Select " & numPicBox & " images." with multiple selections allowed)

--piclist looks like:
--{"AC.1.0196.144.eps", "AC.1.0395.073.eps", "AC.1.0395.074.eps"}


--Create new picture list (without extensions)
set oldPiclist to {}
repeat with n from 1 to count of picList
	set thefile to ((characters 1 thru 13 of item n) of picList as string)
	set end of oldPiclist to thefile
end repeat

--> --{"AC.1.0196.144", "AC.1.0395.073", "AC.1.0395.074"}


--Sort oldPicList according to thisFile
set newPiclist to {}
repeat with i from 1 to count of thisfile
	set thisImage to ((characters 1 thru 13 of item i) of thisfile as string)
	if thisImage is in oldPiclist then
		set end of newPiclist to thisImage
	end if
end repeat

--> --{"AC.1.0395.073", "AC.1.0395.074", "AC.1.0196.144"}

I didn’t check to see if it’s any faster than counting off the thirteen characters, but it’s less dependent on the details of the name to use text item delimiters to remove the .eps from the file name (provided they’re all .eps files):

set picList to {"AC.1.0196.144.eps", "AC.1.0395.073.eps", "AC.1.0395.074.eps"}

set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ".eps"}

set oldPiclist to {}
repeat with n from 1 to count of picList
	set thefile to first text item of item n of picList as string
	set end of oldPiclist to thefile
end repeat

set AppleScript's text item delimiters to TID

oldPiclist --> {"AC.1.0196.144", "AC.1.0395.073", "AC.1.0395.074"}

Thanks for you reaction Adam.
Well, the thing is that some of the image filenames contain suffixes like ‘_A.eps’ or ‘-detail.eps’.
But the first 13 characters are always the collection number of the painting which I can find in the textfile (which comes from a dbase. That’s why I thougth this was more reliable.

kjeld

Yours is the way to go then, kjeld. I was assuming you wanted everything but the extension because that took up space but didn’t add information you needed to deal with.

Ok. I cleaned it up a little and added some comments.
It’s posted in the Code Exchange.

Thanks for your input.
kjeld