-------------------------------------------------------------------------------------------
# Auth: Shane Stanley; ASObjC Guru
# dCre: 2015/09/15 20:08
# dMod: 2015/09/16 12:41
# Appl: ASObjC
# Task: Get a list of Files from a Directory sorted by Last-Added (other sort-keys listed for convenience).
# Libs: None
# Osax: None
# Tags: @Applescript, @ASObjC, @Script, @Sorted, @File, @List, @Shane
# Note: Tested only on OSX 10.10.5.
-------------------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
set targetFolder to path to downloads folder
set thePath to POSIX path of targetFolder
set sortedList to its filesIn:thePath sortedBy:(current application's NSURLAddedToDirectoryDateKey) returningSingleItem:false
-------------------------------------------------------------------------------------------
(*
OTHER SORT CRITERIA
NSURL AddedToDirectoryDateKey
NSURL AttributeModificationDateKey
NSURL ContentAccessDateKey
NSURL ContentModificationDateKey
NSURL CreationDateKey
NSURL FileAllocatedSizeKey
NSURL FileSizeKey
NSURL LabelColorKey
NSURL LabelNumberKey
NSURL LocalizedLabelKey
NSURL LocalizedTypeDescriptionKey
NSURL NameKey
NSURL TotalFileAllocatedSizeKey
NSURL TotalFileSizeKey
NSURL TypeIdentifierKey
* REMOVE THE SPACE AFTER 'NSURL' TO USE!
*)
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on filesIn:folderPOSIXPath sortedBy:sortKey returningSingleItem:singleItemBoolVal
# 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 a NSURL for the folder because that's what's needed.
set theFolderURL to current application's class "NSURL"'s fileURLWithPath:folderPOSIXPath
# Get a reference to the file manager.
set theNSFileManager to current application's NSFileManager's defaultManager()
# Get the NSURLs of items in folder while at the same time getting values for our keys 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 in which to store just the key values.
set valuesNSArray to current application's NSMutableArray's array()
# Loop through the NSURL list retrieving key values and 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 non-packages directories 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 specific sort key.
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
# Sort the array.
set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}
if singleItemBoolVal = false then
# Extract just the paths and convert to an AppleScript list.
return (theSortedNSArray's valueForKey:(current application's NSURLPathKey)) as list
else
# If you want the single most-recent file, use this instead of the previous line.
return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text
end if
end filesIn:sortedBy:returningSingleItem:
-------------------------------------------------------------------------------------------