Best way to get most recent file in a folder

Hello.

Either you have to turn sourceFolder into an alias, or you’ll have to stick “folder” in front of sourceFolder in the Finder tell block

Tried that and a few variations but still no go.

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2" as alias

tell application "Finder"
	sort (get files of folder sourceFolder) by creation date
	-- This raises an error if the folder doesn't contain any files
	set theFile to (item 1 of result) as alias
end tell

Getting this error: Can’t make item 1 of {} into type alias.

Appreciate the help.

Thanks,

Carl

Hello.

I fixed that one for you, but now, if you have some more stuff to do with theFile, then you should do that within the if block. (Just mentioning it.) :slight_smile:

tell application "Finder"
	try
		set sortedList to sort (get files of folder sourceFolder) by creation date
	end try
	if sortedList is not {} then
		set theFile to (item 1 of sortedList) as alias
	end if
end tell

Thanks a bunch for the fix. Works great. I see now my security camera app is creating new
folders within the ss2 folder each time I set it to record a video file.

Have to try to work that out as well. Might you know if it’s possible to search subfolders as well within ss2?

Thanks,

Carl

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2"

tell application "Finder"
	try
		set sortedList to sort (get files of folder sourceFolder) by creation date
	end try
	if sortedList is not {} then
		set theFile to (item 1 of sortedList) as alias
		return theFile
	end if
	
end tell

Hello.

I think I know of a couple of ways to get the lastest modified file in a folder hierarchy.
We can’t use finder for that, so first I want to know, (and I’m not even sure if that is the way I want to go) does ASOC work for you? -I believe you use the indigo server, what I want to know, is whether you use Mavericks, and then if the ASOC statements within an “indigo” script, together with ASOC libraries works for you.

If you don’t know, and don’t care to find out, then that is fine, but this is the fastest way to go.

Save the script below as a Script Bundle with AppleScript editor, click the drawer icon and hook off for ASOC library, with the name ASOCTest, in your ~/Library/Script Libraries folder. (Make it if it isn’t there).

use AppleScript version "2.3"
use scripting additions

on testit()
	tell (path to frontmost application as text)
		tell me
			display dialog "it works"
		end tell
	end tell
end testit

Now, stuff the script below into an indigo script window, and try to run it.

use AppleScript version "2.3"
use scripting additions
use asoct : script "ASOCTest"


asoct's testit()

If you now see a dialog box, then we can use ASOC

No doubt that would be an excellent way to go but my indigo server is
running 10.5.8. I have used ASOC for other tasks on other machines and it does work very well.

Thanks,

Carl

Hello.

OK.

What I wonder is, is if there always are just going to be a small number of files there, and that the folder hierarchy is not deep, like 3-4 levels or deeper.

Then I think we can implement a solution with Finder first, to see if that works out, or if the consisistency :slight_smile: of your root folder is different, then we go straight for a do shell script involving mdfind and sort.

Hello.

Independently of how you reply to the former message, I’ll be back in some hours with a solution.

Hello.

Please see if this works for you. The script below is supposed to work under OS X 10.5, and won’t work under Mavericks, since .DS_Store files are visible, and the files are sorted in the opposite direction.

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2"
tell application "Finder"
	return item 1 of (sort (every file of entire contents of folder sourceFolder) by modification date) as alias	
end tell

That one is working but it does return the oldest file, not the newest.
The source folder may contain a number of folders, but would never be more than 1 folder deep.

Many thanks,

Carl

Hello cheyes888

I’m puzzled because here, McUsrII’s script returns correctly the most recent file.
Im running the current Mavericks.

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 15:26:52

Hi.

  1. The Finder doesn’t return “.DS_Store” files, so the repeat’s not necessary. The script object’s not necessary in any case.

  2. The result of sorting by dates with the Finder has recently changed, probably by accident, since it hasn’t been announced. In Mavericks, dates are sorted from oldest to most recent, rather than vice versa as before. McUsrII’s script gets the most recently modified file in Mavericks or the earliest otherwise. If you’re going to use the Finder’s ‘sort’, you either have to test the system and take the appropriate action, or get both the first and last items in the list and see which is younger.

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