Script to re-date folder to contents logic problem

Joined
Oct 20, 2015
Messages
6
Reaction score
0
Points
1
Hi,
I need to write an applescript to re-date folder(s) to the latest date of the folder contents.
(Right, now I open a folder, open a file in there, make no changes and/or do not save, so the file modification date does not change, but the folder mod date does. I need it to show the same date as the last modified file.)
I don't know how to do any of this, but I'll work it out!
However, I have a logic problem that I can't work out.
So, I need the script to cycle through all the files in a folder looking at their modification dates, and then if file B is older than file A I want to ignore it an go to file C, and if that file date is more recent than A, I want to keep C and ignore A.
But, how do I get the first instance into the loop? I figured I need a known very old start date, say set compareDate to 01/01/01, but then, how do I leave that out of the loop for the second comparison?
Am I explaining this well enough?

(10.12.2)
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Hi emilycurious

Your question seems a little confused to me, and I don't fully understand what you're asking for.
Sometimes you've got to slow down and be more mathematical, and solve one problem at a time.

I think you're asking how to find the file in any given folder with the latest modification date.

This very basic example in AppleScript below, does that in you're Documents folder, but could be made to check any folder on your system.

Code:
use AppleScript version "2.5"
use scripting additions
try
	set theDocumentsFolder to (path to documents folder from user domain) as text
	tell application id "com.apple.systemevents"
		set theFilesList to (every item in folder theDocumentsFolder whose visible is true) as list
		set theReferenceDate to date "Tuesday, 1 January 1901 at 01:01:01" as date
		set theReferenceFile to missing value
		repeat with theFile in theFilesList
			set theFileProperties to (properties of theFile) as record
			set theFileModDate to (modification date of theFileProperties) as date
			if theFileModDate > theReferenceDate then
				set theReferenceDate to theFileModDate as date
				set theReferenceFile to theFile as alias
			end if
		end repeat
	end tell
	tell application id "com.apple.finder"
		open file theReferenceFile
	end tell
on error
	return "An Error Occurred" as text
end try
return theReferenceFile

Copy and paste the above code into a new Scipt Editor file, and it will open the file with the latest modification date.
And also return an AppleScript alias reference to the file in the Script Editor's Result pane.
That's the lowest view in the Script Editor window, if you are not familiar Script Editor, or haven't used it before.

This might give you a start to what your trying to achieve.
But explain more clearly how we can help you further.

Regards Mark
 
Last edited:

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top