Keeping Folder's modification date honest.

The Finder doesn’t seem to do a very good job (if it does it at all) of changing the modification date of a folder to match that of the newest item in it. This little script does that, and nothing else.

set fldr to (choose folder)
tell application "Finder" to set modification date of folder fldr to modification date of (info for (item 1 of (sort (get files of folder fldr) by modification date)) as alias)

On my machine, the sort puts the newest file first. If yours puts it last, change item 1 to item -1

Hi Adam I saw your post on macosxhint and was going to point to your script in relation to some one else on these forums asking why their Folder Action did not work when Files where not moved by the finder.

I was have a similar problem when using your script (above ) yesterday in a Folder Action .
When using unix to move a file into the Folder the Folder Action with this script did not kick in.

But I decided not to reply as My solution would not meet all the needs of the poster.

Any way you mentioned ‘Touch’ in the same thread. http://bbs.applescript.net/viewtopic.php?id=16622

Looking at ‘Touch’ which I have used before I realised there was a better way in doing the script (for me at least)

do shell script ("#!/bin/sh
cp /Users/username/Documents/images/ON/fw.jpg /Users/Username/Documents/images/Running/fw.jpg

touch -r /Users/username/Documents/images/Running/fw.jpg /Users/username/Documents/images ")

The cp copies my file

the touch using -r option gets the Modified time from “fw.jpg” and passes it on to the folder “images”

Notice that I can use this to pass the Md time to any folder.

How you write this in your applescript is up to you, but hopfully you get the idea.

Nice idea. As you point out, the “touch -r File_with_desired_modification_date File_to_update” (where the file can be a folder) instruction is the key. By using this immediately after you have copied the file, you give the folder the file’s date and don’t rely on the Finder to notice.

In an mail to to the News page, Seth Noble explained that he had a drop-script AppleScript to keep his Folder’s modification dates honest. He said:

(*  Seth Noble - http://www.DataExpedition.com/~sbnoble/ - 10/01/02 *)
(* Ignore .files - 02/04/04 *)

global reportstr

on open these_items
	set reportstr to "These folders were updated:
"
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		if (alias of the item_info is false) and (folder of the item_info is true) then
			process_item(this_item)
		end if
	end repeat
	-- display dialog reportstr
end open

on process_item(this_folder)
	set latest_date to the date "Thursday, January 1, 1970 12:00:00 AM"
	set these_items to list folder this_folder without invisibles
	repeat with j from 1 to the count of these_items
		set this_file to (((this_folder as string) & ((item j of these_items) as string)) as alias)
		if ((offset of "." in (name of (info for this_file))) is not 1) then
			set this_date to modification date of (info for this_file)
			if (this_date > latest_date) then set latest_date to this_date
		end if
	end repeat
	set reportstr to reportstr & this_folder & ": " & latest_date & "
"
	tell application "Finder"
		set the modification date of this_folder to latest_date
		update this_folder
	end tell
end process_item