Need help putting a list together

I’m unfamiliar with applescript and need to compile a list. I have to access hundreds of files and need to list their names, creation dates and modified dates and in order of their creation dates and/or the file title. I came across this post here http://bbs.applescript.net/viewtopic.php?pid=60062#p60062 which seems what I’m looking for except for the date sorting. Could anybody help me out with how I would need to alter the script?

thanks

here is a little snippet


set afolder to choose folder
tell application "Finder"
	set FilesToSort to files of folder afolder
	set SortedFiles to sort FilesToSort by creation date -- or modification date
end tell

Thanks - I’m not sure how to use this. I post it into the script editor and run it but it gives me a “doesn’t understand the sort message” error.

thats strange what os are you running ?

Browser: Firefox 2.0.0.4
Operating System: Mac OS X (10.4)

10.3.9

dak,

pardon the dumb questions but I have to ask

you copied the code pasted into script editor then hit run you where prompted for a folder and you choose a folder with files in it ?

mm

sorry for the late reply
yes, i do what you said and get

Finder got an error: (list of all the files) doesn’t understand the sort message.

the code i linked to from the beginning works fine - it just includes all the get info information which i don’t need. I need name, creation and modification date.

the only thing I can think of is that you need 10.4.x for that functionality. could anyone else out there confirm this ?

mm

Yes, the sort command in the Finder is not available in Panther

Hi,

This should work in Panther. It prompts the user to choose a folder and a sort criterion (name or creation date) for the files and does the necessary to create a TextEdit document containing the details. The results are similar in form to those produced by Kai’s script in the other thread, but are limited to names, creation dates, and modification dates. Numerics in names are sorted by number value, a process that’s accurate for numerics up to 12 digits wide. (Append more "0"s to the ‘zeros’ variable in addDoctoredNames() if required.) Although long, this is considerably faster than Kai’s script when dealing with thousands of files. (It may still take a few seconds, though!)

on getFileDetails(theFolder)
	script o
		property theNames : missing value
		property creationDates : missing value
		property modDates : {missing value}
		property collatedDetails : {}
	end script
	
	repeat while o's modDates contains {missing value}
		tell application "Finder"
			update files of theFolder
			set {o's theNames, o's creationDates, o's modDates} to {name, creation date, modification date} of files of theFolder
		end tell
	end repeat
	
	repeat with i from 1 to (count o's theNames)
		set end of o's collatedDetails to {item i of o's theNames, item i of o's creationDates, item i of o's modDates}
	end repeat
	
	return o's collatedDetails
end getFileDetails

on getSortChoice()
	set sortChoice to button returned of (display dialog "Do you want the file details to be sorted by Name or by Creation Date?" buttons {"Name", "Creation Date", "Cancel"} with icon note)
	if (sortChoice is "Cancel") then error number -128 -- Unnecessary on English-language systems.
	
	return sortChoice
end getSortChoice

on addDoctoredNames(theDetails)
	script o
		property l : theDetails
		property subl : missing value
	end script
	
	set theDigits to "0123456789" as Unicode text
	set zeros to "00000000000" as Unicode text -- 11 zeros pads up to 12 digits wide. Add more here if required.
	set z to (count zeros)
	set empty to "" as Unicode text
	
	repeat with i from 1 to (count theDetails)
		set o's subl to item i of o's l
		set thisName to beginning of o's subl
		set doctoredName to empty
		set j to 1
		set numberInProgress to (character j of thisName is in theDigits)
		repeat with k from 2 to (count thisName)
			if (character k of thisName is in theDigits) then
				if not (numberInProgress) then
					set doctoredName to doctoredName & text j thru (k - 1) of thisName
					set j to k
					set numberInProgress to true
				end if
			else if (numberInProgress) then
				if (k - j < z) then
					set doctoredName to doctoredName & text (k - j) thru z of zeros & text j thru (k - 1) of thisName
				else
					set doctoredName to doctoredName & zeros & text j thru (k - 1) of thisName
				end if
				set j to k
				set numberInProgress to false
			end if
		end repeat
		set doctoredName to doctoredName & text j thru k of thisName
		set end of o's subl to doctoredName
	end repeat
end addDoctoredNames

on CustomQsort(theList, l, r, compObj)
	script o
		property cutoff : 10
		property p : theList
		
		on qsrt(l, r)
			set i to l
			set j to r
			set v to my p's item ((l + r) div 2)
			
			repeat while (j > i)
				
				set u to my p's item i
				repeat while (compObj's isLess(u, v))
					set i to i + 1
					set u to my p's item i
				end repeat
				
				set w to my p's item j
				repeat while (compObj's isGreater(w, v))
					set j to j - 1
					set w to my p's item j
				end repeat
				
				if (i > j) then
				else
					set my p's item i to w
					set my p's item j to u
					
					-- Perform any additional actions that may be required after this swap.
					compObj's swap(i, j)
					
					set i to i + 1
					set j to j - 1
				end if
			end repeat
			
			if (j - l < cutoff) then
			else
				qsrt(l, j)
			end if
			if (r - i < cutoff) then
			else
				qsrt(i, r)
			end if
		end qsrt
		
		on isrt(l, r)
			set X to l
			set z to l + cutoff - 1
			if (z > r) then set z to r
			
			set v to my p's item X
			set u to v
			repeat with y from (X + 1) to z
				tell my p's item y
					if (compObj's isLess(it, v)) then
						set X to y
						set v to it
					end if
				end tell
			end repeat
			
			set my p's item X to u
			set my p's item l to v
			
			-- Perform any additional actions that may be required after this swap.
			compObj's swap(l, X)
			
			set u to my p's item (l + 1)
			repeat with i from (l + 2) to r
				set v to my p's item i
				if (compObj's isLess(v, u)) then
					set my p's item i to u
					repeat with j from (i - 2) to l by -1
						tell my p's item j
							if (compObj's isLess(v, it)) then
								set my p's item (j + 1) to it
							else
								set my p's item (j + 1) to v
								
								-- Perform any additional actions that may be required after this insertion.
								compObj's shift(j + 1, i)
								
								exit repeat
							end if
						end tell
					end repeat
				else
					set u to v
				end if
			end repeat
		end isrt
	end script
	
	set listLen to (count theList)
	if (listLen > 1) then -- otherwise the handler will error
		-- Translate negative indices
		if (l < 0) then set l to listLen + l + 1
		if (r < 0) then set r to listLen + r + 1
		
		if (r = l) then
			-- No point in sorting just one item
		else
			-- Transpose transposed indices
			if (l > r) then
				set temp to l
				set l to r
				set r to temp
			end if
			
			if (r - l < o's cutoff) then
				-- Skip the Quicksort if cutoff or less items
			else
				o's qsrt(l, r)
			end if
			o's isrt(l, r)
		end if
	end if
	
	return -- nothing
end CustomQsort

on detailsToText(theDetails)
	script o
		property l : theDetails
		property subl : missing value
	end script
	
	set n to "Name: " as Unicode text
	set cd to "Creation date: " as Unicode text
	set md to "Modification date: " as Unicode text
	set e to "" as Unicode text
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return as Unicode text
	repeat with i from 1 to (count theDetails)
		set o's subl to item i of o's l
		set item i of o's l to {n & item 1 of o's subl, cd & item 2 of o's subl, md & item 3 of o's subl, e} as Unicode text
	end repeat
	set theDetails to theDetails as Unicode text
	set AppleScript's text item delimiters to astid
	
	return theDetails
end detailsToText

on main()
	-- The script object that controls the custom sort. Sorts a list of sublists on item X of each sublist.
	script sortOnItemX
		property X : missing value
		
		on isLess(a, b)
			(item X of a < item X of b)
		end isLess
		
		on isGreater(a, b)
			(item X of a > item X of b)
		end isGreater
		
		on swap(a, b)
		end swap
		
		on shift(a, b)
		end shift
	end script
	
	-- Get a list of lists containing the name, creation date, and modification date of each file in a chosen folder.
	set theDetails to getFileDetails(choose folder)
	if (getSortChoice() is "Name") then
		-- If the user opts for sorting by name, add a doctored name to the end of each sublist and arrange to sort on that.
		-- The doctoring pads any numerics in the names with leading zeros so that they sort lexically they way they would numerically.
		addDoctoredNames(theDetails)
		set sortOnItemX's X to -1
	else
		-- Otherwise, sort on the creation dates.
		set sortOnItemX's X to 2
	end if
	-- Sort the sublists on the selected items.
	CustomQsort(theDetails, 1, -1, sortOnItemX)
	-- Create the final text using the items in the sorted lists.
	set theDetails to detailsToText(theDetails)
	
	-- Create a TextEdit document containing that text.
	tell application "TextEdit"
		activate
		make new document at front with properties {text:theDetails}
	end tell
end main

main()

WOW Nigel did you have that thing hanging around or did you just throw it together ?

mm

Perfect! Thanks so very much.

:slight_smile:

I’ve been using the CustomQsort() handler (written by Arthur Knapp and myself) and some of the ideas in the others for a few years now. It was just a matter of deciding how to put them together for dak’s purposes. I’m glad the result apparently turned out to fit the bill.