The above method gets a list of all the items in the folder, appending a slash after those that are directories, and then uses sed to remove the entries that end with a slash. The problem is that the shell sees package files as ordinary directories. So if the most recent file is an .app script applet, for example, the shell will regard it as a directory.
And it seems that's something to do with the fact that I have invisible files set to show in the Finder. i'm pretty sure that never used to affect Finder scripting, but it seems to now -- here at least (10.9.4).
Anyway, here's a version using a Mavericks script library. It's more code, but the beauty of script libraries is they only need to be written once. And it's still much, much faster than the Finder:
use framework "Foundation"
on filesIn:folderPOSIXPath sortedBy:sortKey
-- specify keys for the values we want for each item in the folder:
-- we want the path, whether it's a directory, whether it's a package, and the key to sort on
set keysToRequest to {current application's NSURLPathKey, ¬
current application's NSURLIsPackageKey, ¬
current application's NSURLIsDirectoryKey, ¬
sortKey}
-- make an NSURL for the folder because that's what's needed
set theFolderURL to current application's NSURL's fileURLWithPath:folderPOSIXPath
-- get a ref to the file manager
set theNSFileManager to current application's NSFileManager's defaultManager()
-- get list of NSURLs of items in folder, getting the values for our keys at the same time, and skipping invisible items
set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬
includingPropertiesForKeys:keysToRequest ¬
options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
|error|:(missing value)) as list
-- create an array to store just the key values in
set valuesNSArray to current application's NSMutableArray's array()
-- loop through retrieving key values, adding each set as a dictionary/record to the array/list
repeat with oneNSURL in listOfNSURLs
(valuesNSArray's addObject:(oneNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value)))
end repeat
-- filter out all directories that aren't packages using a predicate
set theNSPredicate to current application's NSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey)
set valuesNSArray to valuesNSArray's filteredArrayUsingPredicate:theNSPredicate
-- make a sort descriptor that describes the key to sort on
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
-- sort the array
set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}
-- extract just the paths and convert to an AppleScript list
return (theSortedNSArray's valueForKey:(current application's NSURLPathKey)) as list
-- or if you only want the most-recent, use this instead of the previous line:
return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text
end filesIn:sortedBy:
You can sort on other criteria, including: NSURLAttributeModificationDateKey, NSURLContentAccessDateKey, NSURLCreationDateKey, NSURLLabelColorKey, NSURLLabelNumberKey, NSURLLocalizedLabelKey, NSURLLocalizedTypeDescriptionKey, NSURLNameKey, NSURLTypeIdentifierKey, NSURLFileSizeKey, NSURLFileAllocatedSizeKey, NSURLTotalFileAllocatedSizeKey, and NSURLTotalFileSizeKey.