Re: Open Dialog Sort Order
Re: Open Dialog Sort Order
- Subject: Re: Open Dialog Sort Order
- From: Nigel Garvey <email@hidden>
- Date: Tue, 16 Dec 2003 02:52:43 +0000
Pat Stanford wrote on Mon, 15 Dec 2003 12:58:13 -0800:
>
When I use a script to get a list of files in a folder it comes in in
>
normal expected ASCII order. When I look at the same folder in the open
>
file dialog, it is sorted differently. Why? How can I get a list in my
>
script that will match the Finder display on screen?
Long, but fast. It creates another list with the numbers in the names
padded with leading zeros. It the sorts this list alphabetically,
mirroring the moves in the original list - which thus gets sorted as you
wish:
-- From an implementation by Rick Bargerhuff alias Cougar & Nigel Garvey
-- July/August 2003
property mainList : {}
property doctoredList : {}
on FinderSort(inputList)
set mainList to inputList
set doctoredList to getDoctoredList()
QuickSort(1, count inputList)
return mainList
end FinderSort
(* Create a list similar to mainList, but with the numeric
portions of namespadded with leading-zeros *)
on getDoctoredList()
script o
property partList : {}
end script
-- An unlikely character to use as a delimiter
set asciiChr1 to ASCII character 1
set listLen to (count mainList)
set outputList to {}
set l to 1
repeat until l > listLen
-- Process 3900 items (or less) at a time because of the TIDs limit
set r to l + 3899
if r > listLen then set r to listLen
-- Coerce 3900- items to a single string using the unlikely
delimiter
set workStr to ListToString(items l thru r of my mainList,
asciiChr1)
considering case
-- If the string contains underscores, replace them with spaces
if (workStr contains "_") then set workStr to
ListToString(StringToList(workStr, "_"), space)
-- Break the string into numeric and non-numeric sections,
-- padding the former
if (workStr contains "1") or (workStr contains "2") or (workStr
contains "3") or (workStr contains "4") or (workStr contains "5") or
(workStr contains "6") or (workStr contains "7") or (workStr contains
"8") or (workStr contains "9") or (workStr contains "0") then
set digits to "1234567890"
set o's partList to {}
set i to 1
set numeric to (character i of workStr is in digits) -- true or
false
repeat with j from 2 to (count workStr)
if (numeric) then
if (character j of workStr is not in digits) then
set end of o's partList to padNumeric(text i thru (j - 1)
of workStr)
set i to j
set numeric to false
end if
else if (character j of workStr is in digits) then
set end of o's partList to text i thru (j - 1) of workStr
set i to j
set numeric to true
end if
end repeat
if (numeric) then
set end of o's partList to padNumeric(text i thru j of
workStr)
else
set end of o's partList to text i thru j of workStr
end if
-- Rejoin the bits of the string
set workStr to ListToString(o's partList, "")
end if
end considering
-- Break up the work string again using the original, unlikely
delimiter
-- and append the result to the output list
set outputList to outputList & StringToList(workStr, asciiChr1)
set l to r + 1
end repeat
return outputList
end getDoctoredList
-- Prepend enough leading zeros to a numeric string to make it 10
digits wide
on padNumeric(theString)
return (text 1 thru (10 - (count theString)) of "0000000000") &
theString
end padNumeric
-- turn string into list
on StringToList(theString, theDelim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelim
set textItems to theString's text items
set AppleScript's text item delimiters to astid
return textItems
end StringToList
-- turn list into string
on ListToString(theList, theDelim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelim
set theString to theList as string
set AppleScript's text item delimiters to astid
return theString
end ListToString
(* QuickSort by Arthur J Knapp *)
-- This variation is hardwired to sort doctoredList
-- and to mirror the sort moves in mainList
on QuickSort(l, r) -- sorts in-place
local i, j, v
-- Much thanks to both Serge Belleudy-d'Espinose and Victor Yee
-- for the script-referencing techniques that they helped to
-- refine.
--
set i to l
set j to r
set v to my doctoredList's item ((l + r) div 2)
repeat while (j > i)
set u to my doctoredList's item i
repeat while (u < v)
set i to i + 1
set u to my doctoredList's item i
end repeat
set w to my doctoredList's item j
repeat while (w > v)
set j to j - 1
set w to my doctoredList's item j
end repeat
if (i > j) then
else -- 'not' tests take longer to do
set my doctoredList's item i to w
set my doctoredList's item j to u
-- Do the same swap in the main list
tell my mainList's item i
set my mainList's item i to my mainList's item j
set my mainList's item j to it
end tell
set i to i + 1
set j to j - 1
end if
end repeat
if (l < j) then QuickSort(l, j)
if (r > i) then QuickSort(i, r)
end QuickSort
set fileNames to {"150F77.dwg", "150M75.dwg", "200F45.DWG",
"200f45a.dwg", "200F85.DWG", "200M07.DWG", "200M09.DWG", "22C05.DWG",
"22C05NEW.DWG", "22C05OLD.DWG", "2B4P23.DWG", "2B4P24.dwg", "2BC01R.DWG"}
FinderSort(fileNames)
--> {"2B4P23.DWG", "2B4P24.dwg", "2BC01R.DWG", "22C05.DWG",
"22C05NEW.DWG", "22C05OLD.DWG", "150F77.dwg", "150M75.dwg", "200F45.DWG",
"200f45a.dwg", "200F85.DWG", "200M07.DWG", "200M09.DWG"}
NG
_______________________________________________
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.