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
---------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
---------------------------------------------------------------------------------
# POSIX Path of the folder you're interested in.
set thePath to POSIX path of (path to downloads folder as text)
# Returns the POSIX Path of the last added item.
set sortedList to its filesIn:thePath sortedBy:(current application's NSURLAddedToDirectoryDateKey) includeFolders:false returningAsList:false
---------------------------------------------------------------------------------
--» HANDLERS
---------------------------------------------------------------------------------
on filesIn:folderPOSIXPath sortedBy:sortKey includeFolders:includeFoldersBool returningAsList: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
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 class "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))
# 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
if includeFoldersBool ≠ true then
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
end if
# 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}
if returningAsListBool = true then
# Extract just the paths and convert to an AppleScript list
return (theSortedNSArray's valueForKey:(current application's NSURLPathKey)) as list
else
# 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 if
end filesIn:sortedBy:includeFolders:returningAsList:
# You can sort on other criteria, including: NSURLAttributeModificationDateKey, NSURLContentAccessDateKey, NSURLCreationDateKey, NSURLLabelColorKey, NSURLLabelNumberKey, NSURLLocalizedLabelKey, NSURLLocalizedTypeDescriptionKey, NSURLNameKey, NSURLTypeIdentifierKey, NSURLFileSizeKey, NSURLFileAllocatedSizeKey, NSURLTotalFileAllocatedSizeKey, and NSURLTotalFileSizeKey.
---------------------------------------------------------------------------------