I had a go at writing the code with terminology. Not sure if you guys will think it is great or not as my coding can be basic… but it’s a start.
Usage is ;
use theLib : script "sortLib" -- name of the library you saved it in
use scripting additions
sorted list from folder (choose folder) by Key "NSURLContentModificationDateKey" including folders false entire contents false sort order ascending
Result:
{"/Users/UserName/Library/Script Libraries/a_folder/functions.php", "/Users/UserName/Library/Script Libraries/a_folder/b.png", "/Users/UserName/Library/Script Libraries/a_folder/c.png", "/Users/UserName/Library/Script Libraries/a_folder/a.png"}
The Applescript Library when opened looks some thing like this.
[code]
sorted list from folder‚v : Returns a sorted list of folder contents
sorted list from folder Specifier : The folder path to query. Can be a POSIX path string,‘alias’, POSIX file path -
by Key text : Name of the System’s Common File System Resource Key to use for sorting.
including folders boolean : Include sub folders in sort list
entire contents boolean : include the entire contents
sort order ascending/Œdescending : The Sort direction. ascending or descending
#example
use theLib : script “sortLib” – name of the library you saved it in
use scripting additions
sorted list from folder (choose folder) by Key “NSURLContentModificationDateKey” including folders false entire contents false sort order ascending
Result:
{“/Users/UserName/Library/Script Libraries/a_folder/functions.php”, “/Users/UserName/Library/Script Libraries/a_folder/b.png”, “/Users/UserName/Library/Script Libraries/a_folder/c.png”, “/Users/UserName/Library/Script Libraries/a_folder/a.png”}
To obtain a list of Common File System Resource Keys you can use
theLib’s RecourceKeys()
Or go to Apples documentation
(NSURL class Common File System Resource Keys) Documentation[/code]
The library code.
use framework "Foundation"
on sorted list from folder folderPath by Key theKey including folders includeFolders entire contents entireContents sort order ascendingOrder
set theClass to class of folderPath
-- check the class of the folder path var and coerce it into a posix path
if theClass is «class furl» or theClass is alias then
set folderPath to POSIX path of folderPath
else if theClass is text then
set testString to current application's NSString's stringWithString:folderPath
-- take care of path strings with colons
set isRange to testString's rangeOfString:":"
set tr to (|length| of isRange as integer > 0)
if tr then
set folderPath to POSIX path of folderPath
end if
end if
-- validate the sort order and set it if correct
set validType to {ascending, descending}
if ascendingOrder is in validType then
if the ascendingOrder is ascending then
set ascendingOrder to 1
else if the ascendingOrder is descending then
set ascendingOrder to 0
end if
else
set errStr to ascendingOrder & " is not valid sort order" as text
error errStr
end if
-- set the key from the string for the key
set theKey to current application's NSSelectorFromString(theKey)
set keysToRequest to {current application's NSURLPathKey, current application's NSURLIsDirectoryKey, theKey} -- keys for values we want from each item
set theInfo to current application's NSMutableArray's array() -- array to store new values in
set theURL to current application's NSURL's fileURLWithPath:folderPath -- make URL for folder because that's what's needed
set fileManager to current application's NSFileManager's defaultManager() -- get file manager
set theOptions to ((current application's NSDirectoryEnumerationSkipsPackageDescendants) as integer) + ((current application's NSDirectoryEnumerationSkipsHiddenFiles) as integer) -- options to use when enumerating directory
if not entireContents then -- extra option to skip deep search
set theOptions to theOptions + ((current application's NSDirectoryEnumerationSkipsSubdirectoryDescendants) as integer)
end if
-- enumerate the folder
set theEnumerator to fileManager's enumeratorAtURL:theURL includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value)
repeat
set aURL to theEnumerator's nextObject() -- nextObject() returns the next URL held by the enumerator
if aURL is missing value then exit repeat -- missing value means there are no more, so exit
theInfo's addObject:(aURL's resourceValuesForKeys:keysToRequest |error|:(missing value)) -- get values and add to array
end repeat
if not includeFolders then -- filter out folders
set thePred to current application's NSPredicate's predicateWithFormat_("%K != YES", current application's NSURLIsDirectoryKey)
theInfo's filterUsingPredicate:thePred
end if
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:theKey |ascending|:ascendingOrder -- describes the sort
theInfo's sortUsingDescriptors:{theDescriptor} -- do the sort
return (theInfo's valueForKey:(current application's NSURLPathKey)) as list -- extract paths and convert to list
end sorted list from folder
-- call list of some of the System's Common File System Resource Keys
on RecourceKeys()
set RecourceKeys to {"NSURLAttributeModificationDateKey", "NSURLContentAccessDateKey", "NSURLContentModificationDateKey", "NSURLCreationDateKey", "NSURLCustomIconKey", "NSURLEffectiveIconKey", "NSURLFileResourceIdentifierKey", "NSURLFileResourceTypeKey", "NSURLFileSecurityKey", "NSURLHasHiddenExtensionKey", "NSURLIsDirectoryKey", "NSURLIsExcludedFromBackupKey", "NSURLIsExecutableKey", "NSURLIsHiddenKey", "NSURLIsMountTriggerKey", "NSURLIsPackageKey", "NSURLIsReadableKey", "NSURLIsRegularFileKey", "NSURLIsSymbolicLinkKey", "NSURLIsSystemImmutableKey", "NSURLIsUserImmutableKey", "NSURLIsVolumeKey", "NSURLIsWritableKey", "NSURLLabelColorKey", "NSURLLabelNumberKey", "NSURLLinkCountKey", "NSURLLocalizedLabelKey", "NSURLLocalizedNameKey", "NSURLLocalizedTypeDescriptionKey", "NSURLNameKey", "NSURLParentDirectoryURLKey", "NSURLPathKey", "NSURLPreferredIOBlockSizeKey", "NSURLTagNamesKey", "NSURLTypeIdentifierKey", "NSURLVolumeIdentifierKey", "NSURLVolumeURLKey"}
return RecourceKeys
end RecourceKeys
and the sdef code.
[code]<?xml version="1.0" encoding="UTF-8"?>
<suite name="Misc Utilities" code="MOXS" description="Miscellaneous commands">
<command name="sorted list from folder" code="MOSXIDOL" description="Returns a sorted list of folder contents">
<direct-parameter type="Specifier" description="The folder path to query. Can be a POSIX path string,'alias', POSIX file path - "/>
<parameter name="by Key" code="ByKy" type="text" description="Name of the System's Common File System Resource Key to use for sorting."/>
<parameter name="including folders" code="INcl" type="boolean" description="Include sub folders in sort list"/>
<parameter name="entire contents" code="Etcs" type="boolean" description="include the entire contents"/>
<parameter name="sort order" code="SrOr" type="sort direction" description="The Sort direction. ascending or descending"/>
<documentation>
<html>
<![CDATA[
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 39.2px; text-indent: -13.8px; font: 12.0px Verdana}
p.p2 {margin: 0.0px 0.0px 6.0px 39.2px; text-indent: -39.3px; font: 12.0px Verdana; color: #042eee}
p.psort1 {margin: 0.0px 0.0px 0.0px 39.2px; text-indent: -39.3px; font: 12.0px Verdana; color: #5e6161}
p.psort2 {margin: 0.0px 0.0px 0.0px 39.2px; text-indent: -39.3px; font: 12.0px Verdana; min-height: 15.0px}
p.psort3 {margin: 0.0px 0.0px 0.0px 39.2px; text-indent: -39.3px; font: 12.0px Verdana; color: #0433ff}
p.psort4 {margin: 0.0px 0.0px 0.0px 41.6px; text-indent: -41.6px; font: 12.0px Verdana; color: #012fbe}
span.ssort1 {color: #000000}
span.ssort2 {color: #4f8f00}
span.ssort3 {color: #0433ff}
span.ssort4 {font: 12.0px Menlo; color: #942193}
</style>
</head>
<body>
<p class="psort2"><br></p>
<p class="psort1">#example</p>
<p class="psort2"><br></p>
<p class="psort1"><span class="ssort1"><b>use</b> </span><span class="ssort2">theLib</span><span class="ssort1"> : </span><span class="ssort3"><i>script</i></span><span class="ssort1"> "sortLib" </span>-- name of the library you saved it in</p>
<p class="psort3"><span class="ssort1"><b>use</b> </span><i>scripting additions</i></p>
<p class="psort2"><br></p>
<p class="psort4"><b>sorted list from folder</b><span class="ssort1"> (</span><b>choose folder</b><span class="ssort1">) </span>by Key<span class="ssort1"> "NSURLContentModificationDateKey"</span><span class="ssort4"><span class="Apple-converted-space"> </span></span><span class="ssort1"> </span>including folders<span class="ssort4"> false<span class="Apple-converted-space"> </span></span><span class="ssort1"> </span>entire contents<span class="ssort1"><span class="Apple-converted-space"> </span></span><span class="ssort4"> false </span>sort order<span class="ssort4"> ascending</span></p>
<p class="psort1"><b><span class="Apple-tab-span"> </span>Result:</b></p>
<p class="p1"><span class="Apple-tab-span"> </span> {"/Users/UserName/Library/Script Libraries/a_folder/functions.php", "/Users/UserName/Library/Script Libraries/a_folder/b.png", "/Users/UserName/Library/Script Libraries/a_folder/c.png", "/Users/UserName/Library/Script Libraries/a_folder/a.png"}</p>
<p class="psort2"><br></p>
<p>To obtain a list of Common File System Resource Keys you can use</p>
<p class="psort1"> </span><span class="ssort2">theLib's RecourceKeys</span>()</p>
<p>Or go to Apples documentation</p>
<p class="psort2"><br></p>
<p class="p2"><span class="s1"><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/doc/uid/20000301-SW3">(NSURL class Common File System Resource Keys) <span class="Apple-converted-space"> </span>Documentation</a></span></p>
</body>
]]>
</html>
</documentation>
</command>
<enumeration name="sort direction" code="SRTD">
<enumerator name="ascending" code="Acen" description=""/>
<enumerator name="descending" code="Decn" description=""/>
</enumeration>
</suite>
[/code]