Help: I need to make 'selected items' process by 'date'

This is a script called ‘ClipID’ . If i have a bunch of ‘.mpg’ clips listed by creation date with random names, I use this droplet to give them a common name:

bTV movie 1.mpg
bTV movie 4.mpg
bTV movie 9.mpg

should become

NewNameGiven01.mpg
NewNameGiven02.mpg
NewNameGiven03.mpg

SCRIPT:

on open fileList
display dialog “Name Files:” default answer “” --user inputs here
set nameheader to text returned of result
tell application “Finder”
if (count of characters of nameheader) > 27 then
activate
display dialog “Header must be less than 27 characters” buttons “Cancel”
end if
end tell
display dialog “First number?” default answer “1”
set counter to text returned of result as number

tell application "Finder" --do changes here
	repeat with typeitem in fileList
		try
			if counter < 10 then set name of typeitem to nameheader & "0" &         counter & ".mpg"
			if counter > 9 then set name of typeitem to nameheader & counter & ".mpg"
		on error errortext
			display dialog "An error has occured with file: " & typeitem & " :- " & errortext
		end try
		set counter to counter + 1
	end repeat
end tell

end open

It works great, UNLESS the file names TO BE ALTERED have ‘.1,.2,.3 etc’ AFTER the file extension (.mpg). Example 1:

bTV movie 1.mpg
bTV movie 2.mpg
bTV movie 3.mpg
bTV movie 1.mpg.1
bTV movie 2.mpg.1
bTV movie 3.mpg.1
bTV movie 1.mpg.2
bTV movie 2.mpg.2
bTV movie 3.mpg.2

MY PROBLEM: The script is selecting them for processing by order of NAME instead of CREATION DATE ( the order the list is currently in)

Example 2: It selects them in this order-

bTV movie 1.mpg
bTV movie 1.mpg.1
bTV movie 1.mpg.2
bTV movie 2.mpg
bTV movie 2.mpg.1
bTV movie 2.mpg.2
bTV movie 3.mpg.
bTV movie 3.mpg.1
bTV movie 3.mpg.2

Can anyone make the script process the ‘selected items’ [b] by the order they appear in the containing folder(creation date or date modified)[b].

THANKS!!!
SITCOM

Hi, sitcom. I assume from your script and the fact that you’re enquiring in this forum that your droplet is meant for Mac OS 9 or earlier.

The order of files in a folder is undefined as far as scripts are concerned, but pre-X Finders do have a working ‘sort’ command which will return you a list of items sorted in the way you want. Unfortunately, it needs a Finder reference to work on, and the list of items passed to an ‘open’ handler is just a list. You can get round this by using the Finder’s selection, since items dragged to a droplet from a Finder window will be selected in the Finder. This is frowned upon in some circles as it’s theoretically possible to change the selection accidentally while the droplet’s still starting up. To reduce the danger, the following example reselects the dropped items in the droplet itself:

on open fileList
  tell application "Finder"
    select fileList
    set fileList to reverse of (sort the selection by modification date)
    --> fileList sorted by modification date, oldest to youngest
  end tell
  
  -- Your existing code here.

end open

This won’t work in OS X and I’ve only tested it with manual drops of items from a Finder window…

Thank you very much - you verified my theory that the list is no longer be able to sorted (by script) once selected. I was trying to work out some sort of ‘pre-selection list’ but I’m an amature at best. Thanks for saving me the brain cells!!
Apologies for leaving out the ‘OS 9’ application part…
SiTCoM

Thank you Thank you Thank you Thank you
If anyone needs this script for OS9 I’ll post it in builders…
ClipID