Friday, December 15, 2017

#26 2013-12-13 10:16:14 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3202

Re: Best way to get most recent file in a folder

Hello McUser

Is it a chance to have several .DS STORE files at the beginning or at the end of the sorted list ?
I guess that the answer is no.
If I guess correctly, dropping the script object will change nothing because the loops will be executed very short number of times.

Here is the version dropping the script object.

Applescript:

set sourceFolder to (path to home folder as text) & "Documents:tempo:" #"Macintosh HD:Users:TV:Sites:ss2"
tell application "Finder"
   set l to sort (every file of entire contents of folder sourceFolder) by modification date
   # l
   repeat with i from (count l) to 1 by -1
       if name of item i of l is not ".DS_Store" then
           set maybe1 to item i of l
           exit repeat
       end if
   end repeat
   repeat with i from 1 to (count l)
       if name of item i of l is not ".DS_Store" then
           set maybe2 to item i of l
           exit repeat
       end if
   end repeat
   if modification date of maybe1 < modification date of maybe2 then
       return maybe2 as alias
   else
       return maybe1 as alias
   end if
end tell

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 16:16:08

Offline

 

#27 2013-12-13 10:51:38 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

I agree with the fact that it is just a very few items that are looked up.

I think not the size of the list matters, since I seem to remember a post by hhas a long time ago, saying that the mechanism behind a list was vector based (like an array with a fixed lookup time.) Now, if the size of the list would matter, then it would help to have used a script object, since when we optimize, we do so for a worst case, and not the best. But then again, there are just so many .DS_store files that can have been updated before ours, ( I believe that number to be equal to the nesting depth.)

Enough nit-picking for today. smile

Have a nice evening Yvan!

Offline

 

#28 2013-12-14 12:48:04 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

Perfect! Works great.

Many thanks to all,

Carl

Offline

 

#29 2017-08-23 04:31:04 am

mslomski
Member
Registered: 2007-07-24
Posts: 1

Re: Best way to get most recent file in a folder

Hi all,
today I've had the sam job to find the most recent (or las added File) to a certain folder.
After giving the first codes amp a try I've found a much more shorter version which makes me happy and doesn't ask the Finder first to get all files of that folder:

Applescript:


tell application "Finder"
   set theFile to (item -1 of result) as alias
   set lastFileName to name of theFile
end tell

For me it works fine unter Sierra

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)