Is there any way to get the oldest file in a folder?
I mean, among several files, the file that was first added to a folder.
After reading both Finder and System Events dictionaries, I couldn´t find any property that could tell me from what time a file belongs to a folder.
Is it posible?
Sure, although Finder and System Events can’t help you.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set posixFolderPath to "/Users/shane/Desktop/Explorer stuff"
set sourceFolderURL to current application's |NSURL|'s fileURLWithPath:posixFolderPath
-- get file manager
set theNSFileManager to current application's NSFileManager's defaultManager()
-- get contents of directory, ignoring invisible items
set theURLs to theNSFileManager's contentsOfDirectoryAtURL:sourceFolderURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
-- loop through URLs, comparing dates
set lastDate to current application's NSDate's distantFuture()
set theFirst to missing value
repeat with aURL in theURLs
-- get date added
set {theResult, theDate} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLAddedToDirectoryDateKey) |error|:(missing value))
-- test if date is before lastDate
if theResult as boolean and (theDate's compare:lastDate) as integer < 0 then
set lastDate to theDate
set theFirst to aURL
end if
end repeat
return theFirst's |path|() as text
2 Likes