In a JavaScript for Automation version (‘JXA’ in the minds of a few, though not in Apple’s documents ) you can quite easily use JS itself to do the sorting.
(The NSURL keys are built-in properties of the $ object (ObjC.$)).
Example below, retrieving the AddedToDirectory and ContentModification dates, together with the TypeIdentifier. The sorting on AddedToDirectory is done in JS.
(function () {
'use strict';
var strPath = '~/Desktop'; // Choose the folder you want
// lstKeys :: [$.Keys] // Choose the keys you want
var lstKeys = [
$.NSURLAddedToDirectoryDateKey,
$.NSURLContentModificationDateKey,
$.NSURLTypeIdentifierKey
].map(ObjC.unwrap);
// folderFilesWithKeyValues :: String -> [$.NSURLConstant] -> [Dictionary]
var folderFilesWithKeyValues = function (strPath, lstKeys) {
var fm = $.NSFileManager.defaultManager,
oURL = $.NSURL.fileURLWithPathIsDirectory($(strPath)
.stringByStandardizingPath, true);
return ObjC.unwrap(
fm.contentsOfDirectoryAtURLIncludingPropertiesForKeysOptionsError(
oURL, lstKeys, 1 << 2, null
)
)
.map(function (x) {
var dctProps = {
path: x.path.js
};
lstKeys.forEach(function (k) {
var ref = $();
x.getResourceValueForKeyError(ref, k, null);
dctProps[k.slice(5, -3)] = ref.js;
});
return dctProps;
});
};
// GENERIC FUNCTIONS -----------------------------------------------------
// show :: a -> String
var show = function (x) {
return JSON.stringify(x, null, 2);
};
// sortWith :: Ord b => (a -> b) -> [a] -> [a]
var sortWith = function (f, xs) {
return sortBy(compare(f), xs);
};
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
var sortBy = function (f, xs) {
return xs.sort(f);
};
// compare :: (a -> b) -> (a -> a -> Ordering)
var compare = function (f) {
return function(x, y) {
var a = f(x),
b = f(y);
return a < b ? -1 : a > b ? 1 : 0
};
};
// TEST ------------------------------------------------------------------
return show(sortWith(function (x) {
return x.AddedToDirectoryDate;
}, folderFilesWithKeyValues(strPath, lstKeys))
.map(function (x) {
return [ // Return the path, and the attributes by key
// (dropping 'NSURL' from start and 'key' from end)
x.path,
x.AddedToDirectoryDate,
x.ContentModificationDate,
x.TypeIdentifier
];
}));
})();
Sample output:
[
[
"/Users/houthakker/Desktop/TP fixes",
"2017-01-03T01:30:51.000Z",
"2017-01-03T01:33:21.000Z",
"public.folder"
],
[
"/Users/houthakker/Desktop/TuringTestTerritory2.pdf",
"2017-01-04T22:46:31.000Z",
"2017-01-04T22:46:31.000Z",
"com.adobe.pdf"
],
[
"/Users/houthakker/Desktop/evenOdd.scpt",
"2017-01-12T18:09:36.000Z",
"2017-01-13T10:34:23.000Z",
"com.apple.applescript.script"
],
[
"/Users/houthakker/Desktop/formatTest.zip",
"2017-01-13T10:32:08.000Z",
"2017-01-13T10:32:08.000Z",
"public.zip-archive"
],
[
"/Users/houthakker/Desktop/formatTest.zip.cpgz",
"2017-01-13T10:32:33.000Z",
"2017-01-13T10:32:33.000Z",
"com.apple.bom-compressed-cpio"
],
[
"/Users/houthakker/Desktop/compiled.scpt",
"2017-01-13T10:46:09.000Z",
"2017-01-13T10:46:09.000Z",
"com.apple.applescript.script"
],
[
"/Users/houthakker/Desktop/sample.graffle",
"2017-01-13T15:23:55.000Z",
"2017-01-13T15:23:55.000Z",
"com.omnigroup.omnigraffle.graffle"
]
]