Hello,
I am having a problem with a droplet that has a repeat handler. What I’m trying to do is take a certain number of drawing files and add them to a Filemaker Pro dbase in a sequence. The files are named with progressive figure numbering such as; ET ELEC142-1 f21, ET ELEC142-1 f22, ET ELEC142-1 f23, etc. When I drop a selection of these files on the droplet, it processes the highest figure-numbered file first then progressively on down to the lowest figure number. What I want is the lowest figure-numbered file to be added to the dbase first then up to the highest figure-numbered file.
Here is the repeat part of the droplet:
on open the_list
tell application “Finder”
set the_list to selection
repeat with source_file in the_list
– various script lines that add files to the dbase, etc. –
end repeat
end tell
end open
I tried to do a ‘repeat from 1 to item i of the_list’ etc. but it didn’t seem to work. The creation date of the files is in the same order as the figure numbering. I don’t know why it would select the highest figure-numbered file first. Any ideas as to how I could fix this problem?
Does this help? (I’m still fairly new at this, but it’s a bit I’ve been using, and I think it’s doing what you’re hoping to do…
tell application “Finder” to set the source_folder to (folder of the front window)
set the item_list to every file in source_folder
set source_folder to source_folder as string
repeat with targetfile in item_list
set fileinfo to info for (targetfile as alias)
– Actions to be taken–
end repeat
I’ve lost track of what scripting additions come standard with AppleScript these days, but maybe there’s a sort routine among them. If not, maybe a sort handler like
on mySort(lst)
set n to count of lst
if n <= 1 then return {n}
set indexL to {}
repeat with i from 1 to count of lst
set end of indexL to i -- initialize an index list as {1, 2, 3, ...}
end repeat
repeat with i from 1 to n - 1
set changed to false
repeat with j from i + 1 to n
if (item (item i of indexL) of lst) as text > (item (item j of indexL) of lst) as text then
set t to item i of indexL
set item i of indexL to item j of indexL
set item j of indexL to t
set changed to true
end if
end repeat
if not changed then exit repeat -- sorted by this point so no need to continue
end repeat
return indexL
end mySort
could be called like this:
on open the_list
tell app "Finder"
set the_list to selection
set indexList to my mySort(the_list)
repeat with i from 1 to count of the_list
set source_file to item (item i of indexList) of the_list
-- various script lines that add files to the dbase, etc. --
end repeat
end tell
end open
I use an index array instead of rearranging the list itself for efficiency here. I assume “selection” returns a list of aliases rather than text strings so I coerce them in the handler. For huge lists it might be better to set up a list of strings up front. I’m sure there are cleaner ways but this works on my simplistic test.
Thanks for all the prompt suggestions!
In my messing around with the script, I eventally stumbled on a one line solution that was too straightforward.
set file_names to every file of folder “some folder” of folder “another folder” of folder “mylastname” of folder “Users” of startup disk
Placed in the script after setting the_list gives me a list of file names that get processed from the top file in the window to the bottom one.
on open the_list
tell application “Finder”
set the_list to selection
set file_names to every file of folder “some folder” of folder “another folder” of folder “mylastname” of folder “Users” of startup disk
repeat with source_file in the_list
– various script lines that add files to the dbase, etc. –
end repeat
end tell
end open