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: Sat, 17 Aug 2002 18:47:32 +0100
Michelle Steiner kindly informs:
>
In OS X, it says "Not available yet"
Typical.
...
Oh well, you can always [eg] pump the list through an AS sort function that
supports callbacks for performing the comparisons between items. For
example, this one sorts by filename (so no good if you've got a list
containing items taken from different folders):
======================================================================
on _quickSort(theList, evalObj)
script kludge
property kList : theList
property lesserList : {}
property greaterList : {}
end script
tell kludge
set listLength to (count its kList)
set midpointIndex to (listLength + 1) div 2
set thePivot to its kList's item midpointIndex
repeat with x from 1 to (midpointIndex - 1)
get its kList's item x
if evalObj's isLessThan(result, thePivot) then
set its lesserList's end to result
else
set its greaterList's end to result
end if
get its kList's item -x
if evalObj's isLessThan(result, thePivot) then
set its lesserList's end to result
else
set its greaterList's end to result
end if
end repeat
if (listLength - midpointIndex) is midpointIndex then
get its kList's item -midpointIndex
if evalObj's isLessThan(result, thePivot) then
set its lesserList's end to result
else
set its greaterList's end to result
end if
end if
if (count its lesserList) is greater than 1 then set its
[NO-BREAK]lesserList to my _quickSort(its lesserList, evalObj)
if (count its greaterList) is greater than 1 then set its
[NO-BREAK]greaterList to my _quickSort(its greaterList, evalObj)
return its lesserList & {thePivot} & its greaterList
end tell
end _quickSort
on quickSort(theList, evalObj)
if theList's class is not list then error "Not a list."
if (count theList) is greater than 1 then
return _quickSort(theList, evalObj)
else
return theList
end if
end quickSort
-------
--TEST
script evalObj
on isLessThan(val1, val2) --(must return boolean)
return (val1's name < val2's name) --(user's
[NO-BREAK]comparison-performing code goes here)
end isLessThan
end script
tell application "Finder" to set theList to files of startup disk
quickSort(theList, evalObj)
======================================================================
Super-fast under OS9 it is not, due to the number of times Finder object
properties are being accessed. It'll probably be nippier under OSX, where
Finder scripting is [hopefully] a bit less sluggish.
Alternatively, it goes faster if you work with an alias list instead:
======================================================================
--TEST
script evalObj
on isLessThan(val1, val2) --(must return boolean)
return (val1 as string < val2 as string) --(user's
[NO-BREAK]comparison-performing code goes here)
end isLessThan
end script
tell application "Finder" to set theList to files of startup disk as
[NO-BREAK]alias list
quickSort(theList, evalObj)
======================================================================
If that's not an option, you could at least add some pre-/post-processing
code that'll reduce the number of times you have to access those damned
Finder objects:
======================================================================
script evalObj
on isLessThan(val1, val2) --(must return boolean)
return (val1's item 1 < val2's item 1) --(user's
[NO-BREAK]comparison-performing code goes here)
end isLessThan
end script
tell application "Finder" to set theList to files of startup disk
--preprocess
repeat with eachItem in theList
set eachItem's contents to {eachItem's name, eachItem's contents}
end repeat
--sort
set theList to quickSort(theList, evalObj)
--postprocess
repeat with eachItem in theList
set eachItem's contents to get eachItem's item 2
end repeat
return theList
======================================================================
Though given the performance issues for AppleScript lists, you may have to
optimise those loops as well using further kludges... etc, etc, etc. Too,
too tedious for words. [1]
One tip: whack all this stuff in its own script file when done, and load it
as a library using, e.g.:
property fileSortLib : load script (file "path to library")
tell fileSortLib to sortFiles(theList)
You really don't want this sort of stuff uglying up the rest of your code
any more than necessary. I think I get queasy just looking at it...
HTH
has
[1] I can see why folk like JD [2] find the obnoxious-looking gibberish
that is Perl to be more appealling. I bet you don't have these stupid
problems there...
[2] Hey, so, John and Co... How about wrapping some of that ol' linenoise
magic in a nice, easy-to-use set of AS handlers for the benefit of us poor
dumb folks who can't/won't write it ourselves? We sure could use some
decent AS libraries round these parts...
--
(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.