Best way to get most recent file in a folder

Thanks all. I’m running 10.5.8 so, if I have this correct, I need to mod the script in some way to
get the most recent?

Carl

Hello Nigel

I tested under Mavericks after adding an instruction logging the entire list.

Here is a short subset :

document file .DS_Store of folder 2345 mises à jour of folder tempo of folder Documents of folder yvankoenig of folder Users of startup disk, document file .DS_Store of folder tempo of folder Documents of folder yvankoenig of folder Users of startup disk, document file

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 15:44:38

Hello.

I have hopefully fixed the script in post # 17 so it doesn’t consider .DS_Store files, and respects the sort order by modification date as of Mac Os X Leopard.

Here is a version behaving correctly on the different systems :

set sourceFolder to (path to home folder as text) & "Documents:tempo:" #"Macintosh HD:Users:TV:Sites:ss2"
script o
	property l : {}
end script
tell application "Finder"
	set o's l to sort (every file of entire contents of folder sourceFolder) by modification date
	# log o's l
	repeat with i from (count o's l) to 1 by -1
		if name of item i of o's l is not ".DS_Store" then
			set maybe1 to (item i of o's l)
			exit repeat
		end if
	end repeat
	repeat with i from 1 to (count o's l)
		if name of item i of o's l is not ".DS_Store" then
			set maybe2 to (item i of o's 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


I kept the script object even if it’s not really useful in this case.

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 15:57:44

Hello.

I dare say the script object is useful if you have to reference the items of the returned result of the sort command for sifting out the .DS_Store files.

That is under the assumption that it is faster to reference an item in a list that is referenced by a script property. If my assumptions are wrong, then please enlighten me. :slight_smile:

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.

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

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. :slight_smile:

Have a nice evening Yvan!

Perfect! Works great.

Many thanks to all,

Carl

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:


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