Choosing the last modified or created file...

Hi,

I’d like to know if there’s a way to choose the last modified or created file in a folder. Something like this:

set myFolder to "path:to:folder"
set lastItem to the last item modified of myFolder

This obviously doesn’t work, but you get the idea.

MacOS9.2.2

Thanks.

set myFolder to "path:to:folder"
tell application "Finder"
  try
    set lastItem to first item of (sort files of folder myFolder by modification date)
  on error
    -- no files in the folder
  end try
end tell

The result is a Finder reference, which you can coerce to string or to alias if you like.

Sorry. I didn’t allow for there being only one file in the folder.

set myFolder to "path:to:folder"
tell application "Finder"
  try
    set lastItem to first item of ((sort files of folder myFolder by modification date) as list)
  on error
    -- no files in the folder
  end try
end tell