Okay, this one has an option in the handler-call.
---------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/02/09 06:58
# dMod: 2016/06/03 00:31
# Appl: ASObjC
# Task: Get the most recent item path from the designated directory.
# : Allows for exclusion of folders.
# Tags: @Applescript, @Script, @ASObjC, @Get, @Most, @Recent, @Path
---------------------------------------------------------------------------------
useAppleScript
version"2.4"
useframework
"Foundation"
usescripting additions
---------------------------------------------------------------------------------
# POSIX Path of the folder you're interested in.
setthePathtoPOSIX
pathof (path todownloads
folder astext)
# Returns the POSIX Path of the last added item.
setsortedList
toitsfilesIn:thePathsortedBy:(current
application'sNSURLAddedToDirectoryDateKey)
includeFolders:falsereturningAsList:false
---------------------------------------------------------------------------------
--» HANDLERS
---------------------------------------------------------------------------------
onfilesIn:folderPOSIXPathsortedBy:sortKeyincludeFolders:includeFoldersBoolreturningAsList:returningAsListBool
# 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
setkeysToRequest
to {current application'sNSURLPathKey,
¬
current application'sNSURLIsPackageKey, ¬
current application'sNSURLIsDirectoryKey, ¬
sortKey}
# Make an NSURL for the folder because that's what's needed
settheFolderURL
tocurrent application'sclass"NSURL"'sfileURLWithPath:folderPOSIXPath
# Get a ref to the file manager
settheNSFileManager
tocurrent application'sNSFileManager's
defaultManager()
# Get list of NSURLs of items in folder, getting the values for our keys at the same time, and skipping invisible items
setlistOfNSURLs
to (theNSFileManager'scontentsOfDirectoryAtURL:theFolderURL
¬
includingPropertiesForKeys:keysToRequest ¬
options:(current application'sNSDirectoryEnumerationSkipsHiddenFiles)
¬
|error|:(missing value))
# Create an array to store just the key values in
setvaluesNSArray
tocurrent application'sNSMutableArray's
array()
# Loop through retrieving key values, adding each set as a dictionary/record to the array/list
repeatwithoneNSURL
inlistOfNSURLs
(valuesNSArray'saddObject:(oneNSURL'sresourceValuesForKeys:keysToRequest|error|:(missing
value)))
end repeat
# Filter out all directories that aren't packages using a predicate
ifincludeFoldersBool ≠
truethen
settheNSPredicate
tocurrent application'sNSPredicate's
predicateWithFormat_("%K == NO OR %K == YES",
current application'sNSURLIsDirectoryKey,
current application'sNSURLIsPackageKey)
setvaluesNSArray
tovaluesNSArray's
filteredArrayUsingPredicate:theNSPredicate
end if
# Make a sort descriptor that describes the key to sort on
settheDescriptor
tocurrent application'sNSSortDescriptor's
sortDescriptorWithKey:(sortKey)
ascending:true
# Sort the array
settheSortedNSArray
tovaluesNSArray's
sortedArrayUsingDescriptors:{theDescriptor}
ifreturningAsListBool =
truethen
# Extract just the paths and convert to an AppleScript list
return (theSortedNSArray'svalueForKey:(current
application'sNSURLPathKey))
aslist
else
# Or if you only want the most-recent, use this instead of the previous line:
return (theSortedNSArray'slastObject()'s
valueForKey:(current application'sNSURLPathKey))
astext
end if
endfilesIn:sortedBy:includeFolders:returningAsList:
# You can sort on other criteria, including: NSURLAttributeModificationDateKey, NSURLContentAccessDateKey, NSURLCreationDateKey, NSURLLabelColorKey, NSURLLabelNumberKey, NSURLLocalizedLabelKey, NSURLLocalizedTypeDescriptionKey,
NSURLNameKey, NSURLTypeIdentifierKey, NSURLFileSizeKey, NSURLFileAllocatedSizeKey, NSURLTotalFileAllocatedSizeKey, and NSURLTotalFileSizeKey.
---------------------------------------------------------------------------------