Re: getting a file list in sorted order
Re: getting a file list in sorted order
- Subject: Re: getting a file list in sorted order
- From: has <email@hidden>
- Date: Sun, 18 Aug 2002 18:13:52 +0100
Steven Angier wrote:
>
1. Use the Macscript.com Library as it has several functions for listing
>
files and folders, including 2 that filter in only the supplied file and/or
>
creator types:
[...]
>
set theList to FilterFiles(source1,"TEXT",{})
What benefits does this have over using the Finder's 'whose' clause to
filter by? (Assuming this isn't already what it does internally - you seem
to infer below that it doesn't.) It's hard to beat, short of going outside
AS to do so.
>
2. If you must (or want to) do things yourself, AVOID USING THE FINDER
>
WHENEVER POSSIBLE. The Finder still craps out from time to time when sorting
>
or when getting long or recursive directory listings (stack overflows).
This seems a bit alarmist, imho. The problems with 'entire contents' are
known (I don't suppose this has been fixed in OS X then?). Bit
disappointing to learn that 'sort' also has stack overflow problems though
(sounds like some basic recursive sorting algorithm with inadequate
protection against this sort of thing; tsk). Mmmm... maybe the reason
'sort' hasn't yet been implemented in Finder/OSX is that they're planning
something more robust? [Just a hope.]
All the same, I don't think Finder scripting is _that_ problematic, and
other stuff seems to work fine. [Most problems that arise on the lists seem
to be with folk having difficulty figuring out how to do things - probably
because the documentation doesn't explain it very well - not because
they're running into Finder bugs.]
>
The "list folder" command (upon which all Macscript.com Library file listing
>
functions is based) returns file names sorted alphabetically. It returns a
>
list of names which you have to concatenate with the parent folder path, and
>
test the type and/or creator.
>
>
This method maybe slower than using the Finder, but at least you get the
>
right answer.
Mmmm... I did some checking (OS9.2.2./AS1.6). Depends what you're
definition of "right answer" is: I found that 'list folder' can include
the Desktop Folder (the Finder doesn't), and it has different ideas about
ordering; for example, an underscore has precedence over an alphabetical
character [1], which in AS and the Finder it does not.
--
Anyway, here's a [non-Finder-based] basic handler [2] for walking a
folder's contents if anyone's interested:
======================================================================
on processFolderContent(folderAlias, processorObj,
[NO-BREAK]includingInvisibles)
set folderPathString to folderAlias as string
script --speed kludge
property folderContents : list folder folderAlias invisibles
[NO-BREAK]includingInvisibles
end script
tell result
repeat with x from 1 to count its folderContents
set itemPath to alias (folderPathString & its
[NO-BREAK]folderContents's item x)
tell processorObj to processItem({itemAlias:itemPath} &
[NO-BREAK](info for itemPath))
end repeat
end tell
end processFolderContent
-------
--TEST
script processorObj
on processItem(itemInfo)
--DO STUFF HERE
log itemInfo's itemAlias --(test)
end processItem
end script
set fold to path to startup disk
processFolderContent(fold, processorObj, false)
======================================================================
You'd generally want to do some useful work in processItem() [e.g. file
renames], but I suppose you could use it just to create a list of found
files/folders (or use whatever other filtering criteria you like) if you
really wanted:
======================================================================
on getFolders(theFolder) -- [not very elegant!]
script processorObj
property foundList : {}
on processItem(itemInfo)
if (itemInfo's folder) then set foundList's end to
[NO-BREAK]itemInfo's itemAlias
end processItem
end script
processFolderContent(theFolder, processorObj, false)
return processorObj's foundList
end getFolders
getFolders(path to startup disk)
======================================================================
Again though, this seems a pretty inefficient way of performing a very
simple task that's so easily [and quickly] performed using the Finder and a
simple 'whose' clause [8 lines of code versus 1, plus a whole bunch of
relatively slow AS operations and osax calls].
You only really want to use such things when you're going to get some
worthwhile benefits from it, doing things that you can't do with the Finder
alone (e.g. filter files according to content, or processing items as you
go).
HTH
has
[1] Come to think of it, this could be considered a bug. Anyone know? If
so, is it the same in AS1.8.3?
[2] It's pretty shoddy really... but I don't imagine anyone's really that
bothered, so what the hey.:)
--
(My email address has changed from <email@hidden> to
<email@hidden>. Please update your address books accordingly.)
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.