Re: Neat strategies for sorting a list of files alphabetically by name
Re: Neat strategies for sorting a list of files alphabetically by name
- Subject: Re: Neat strategies for sorting a list of files alphabetically by name
- From: Adam Wuellner <email@hidden>
- Date: Mon, 14 Mar 2005 11:01:41 -0600
On Mon, 14 Mar 2005 11:02:19 +0100, Brennan <email@hidden> wrote:
> On 11/3/05 at 15:07, Barry Wainwright <email@hidden> wrote:
>
> > On 11/3/05 3:01 pm, "Brennan" <email@hidden> wrote:
> >
> > > Given that these files sort perfectly in alphabetical order,
> > > is there a good approach for sorting them without writing a
> > > full blown sort routine? Is there perhaps a shell command I
> > > can use?
> >
> >
> > On open (theFiles)
> > Tell app "finder" to Set nameList to name of theFiles
> > Set applescript's text item delimiters to {ascii character 10}
> > Set sortedNBameList to paragraphs of Do shell script "echo " & ¬
> > quoted form of (namelist as text) & "|sort"
> > End open
>
> Looks good, but "Finder" wont give me 'name of theFiles' (it's just a list
> of aliases that gets fed to 'open').
>
> If I have to iterate through them to get their names, I might as well
> perform my own sort. :(
Finder will give you 'name of theFiles' if theFiles is a list of
Finder's file class objects. You say you have a droplet, and when the
files are dropped on the droplet, the list comes in as a list of
aliases... is that a correct formulation of the problem?
> Conceivably I could assume that 'selection of app "Finder"' will contain a
> list of the same files as received by 'open', but I fear that this may not
> always work. Can I rely on this?
Perhaps - I assume this thought comes from the fact that in order to
drop a group of files on the droplet, first (in fact, immediately
prior) they must be selected in Finder... As long as the 'get
selection of application "Finder"' happens rather quickly in the on
open handler, I would be pretty confident that the state of that
selection had not changed. However, I would be more confident working
with only the data that was originally supplied - the list of aliases.
> Any other ideas?
Here is a way to get a sorted list of the files, represented as
Mac-style paths. It is done without a repeat loop, but a repeat loop
is probably necessary to get the list back as a list of aliases. If
you already have a processing loop, you can just add one line to the
top of that code block that coerces the path back into an alias. Not
sure what that'll do to the overall speed of you program.
-- this block provides a list of aliases, similar to the
-- list received by a droplet
tell application "Finder" to tell folder (choose folder)
set theFiles to files as alias list
end tell
-- this sorts the list of aliases into a list of Mac-style paths
set AppleScript's text item delimiters to ASCII character 10
set theFiles to theFiles as string
set sortedList to paragraphs of (do shell script "echo " & quoted form
of theFiles & " | sort")
-- this is a repeat loop to 'do something' with the sorted paths
repeat with aPath in sortedList
set aFile to aPath as alias
-- do something
end repeat
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden