Set Modification Timestamp To File Name

I have a folder of files, all named in the following Date/Time format:

2018/12/18 11.30.27

and I need to set each file’s modification date and time to match the filename.

The folder’s location is: “Hard Drive:Users:Bob’s Mac:Documents:Travel Photos”. I would appreciate anyone’s help in writing an Applescript that would do this without any external code necessary.

Thank you very much!

Probably, you need Read this topic. Is one of the solutions for your problem

Thanks for that, but is there some way to do this with purely Applescripting and not using an external command line file?

This should work. Although I am sure there is a more elegant and better way to do it.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set fileFolder to (choose folder) --
tell application "System Events"
	set hfsPaths to path of files of fileFolder
end tell

repeat with i from 1 to (number of items in hfsPaths)
	set thisFile to item i of hfsPaths
	quoted form of POSIX path of thisFile
	tell application "Finder"
		set thisFile_ext to name extension of (thisFile as alias)
		set thisFile_name to text 1 thru -((length of thisFile_ext) + 2) of (name of (thisFile as alias) as text)
		set full_name to name of (thisFile as alias)
	end tell
	set my_info to splitText(thisFile_name, " ")
	set myDate to splitText(item 1 of my_info, "/")
	set myTime to splitText(item 2 of my_info, ".")
	set my_command to (text 3 thru -1 of (item 1 of myDate) & (item 2 of myDate) & (item 3 of myDate) & (item 1 of myTime) & (item 2 of myTime) & "." & (item 3 of myTime))
	
	do shell script "touch -t " & my_command & " " & quoted form of POSIX path of thisFile
end repeat



on splitText(theText, theDelimiter)
	set AppleScript's text item delimiters to theDelimiter
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to ""
	return theTextItems
end splitText

I would try with this simple :

set fileFolder to (choose folder)
tell application "System Events"
	set hfsPaths to path of files of fileFolder
	repeat with aPath in hfsPaths
		set theName to name of disk item aPath --> your name "2018/12/18 11.30.27.png" is returned as "2018:12:18 11.30.27.png"
		set dateComponents to my splittext(theName, {":", " ", "."})
		--> {"2018", "12", "18", "", "11", "30", "27", "png"}
		set newDate to date ("1 / 1 / 1")
		tell newDate
			set year to dateComponents's item 1
			set day to dateComponents's item 3
			set its month to dateComponents's item 2
			set its hours to dateComponents's item 5 # ADDED its
			set its minutes to dateComponents's item 6 # ADDED its
			set its seconds to dateComponents's item 7
		end tell
		set modification date of file aPath to newDate
	end repeat
end tell

on splittext(theText, theDelimiter)
	set AppleScript's text item delimiters to theDelimiter
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to ""
	return theTextItems
end splittext

Alternate syntax :

set fileFolder to (choose folder)
tell application "System Events"
	set hfsPaths to path of files of fileFolder
	repeat with aPath in hfsPaths
		set theName to name of disk item aPath --> your name "2018/12/18 11.30.27.png" is returned as "2018:12:18 11.30.27.png"
		set {theYear, theMonth, theDay, bof, theHours, theMinutes, theSeconds} to my splittext(theName, {":", " ", "."})
		--> {"2018", "12", "18", "", "11", "30", "27"}
		set newDate to date ("1 / 1 / 1")
		tell newDate
			set {year, day, its month, its hours, its minutes, its seconds} to {theYear, theDay, theMonth, theHours, theMinutes, theSeconds}
		end tell
		set modification date of file aPath to newDate
	end repeat
end tell

on splittext(theText, theDelimiter)
	set AppleScript's text item delimiters to theDelimiter
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to ""
	return theTextItems
end splittext

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mars 2019 22:38:21

Following script works for me. I use the 24 hour setting for time, the US system. OS X Captain.



set myFolder to choose folder with prompt "Choose the folder with the photos!"

tell application "Finder"
	
	set photosFiles to (every file of myFolder whose name extension is "png") as alias list
	repeat with nextFile in photosFiles
		
		set nextName to name of nextFile
		set theYear to text 1 thru 4 of nextName
		set theMonth to text 6 thru 7 of nextName
		set theDay to text 9 thru 10 of nextName
		set theHour to text 12 thru 13 of nextName
		set theMinutes to text 15 thru 16 of nextName
		set theSeconds to text 18 thru 19 of nextName
		
		set dateString to theDay & "/" & theMonth & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds
		set the_date to my date dateString
		set modification date of file nextFile to the_date
		
	end repeat
end tell

The most important in the script is the use of the keyword my in the sentence set the_date to my date dateString. This tells the script that the script itself should process the date command, not the Finder.

Due to the omission of this keyword, many scripters believe that it is impossible to change the modification date only using AppleScript. As you will see, it is not.

KniazidisR:

I just tried your script but it set the date to: Jun 12, 2019 12:00 AM (in the future!)

What am I missing?

My script is designed for 24-hour setting of the US system. For example, Finder shows me the dates of file modification in the following format:

21 Mar 2018 20:42

Check if you have it. If not, then you need to either change the setting for dates through the System Settings panel, or slightly alter the script for your computer

Show me one of the modification dates of some file on your computer (as shown by the Finder)

For example Jun 12, 2019 6:50 PM would mean that you have a 12-hour setting. Also note the order of the day / month / year. If you have a different order in the Finder, I will tell you what to fix in the script.

Also, I assume that the names of your files use the 24-hour format, from the name format you specified. It means that everything works, you need to set up a 24-hour format for Finder at least temporarily. This temporary replacement could also be automated, but I would not like to dig deep into the subject.

If it would be possible to slightly modify the script for a 12-hour setting, I would be most grateful.

The date/time format is exactly as I had copied earlier and does use 24-hour format.

Thanks again for your help!

What you copied is from the Script Editor’s Results area. I asked you to show something completely different: in what format the Finder shows the modification dates in its windows. In a concatenation clause set dateString to theDay & “/” & theMonth & “/” & theYear & space & theHour &… the order of the components of a date must be the same in which Finder shows it. That is, we put correspondence in what our Apple-script does and what the Finder does.

For 12-hour setting, the order in Finder remain day/mounth/year:


set myFolder to choose folder with prompt "Choose the folder with the photos!"

tell application "Finder"
	
	set photosFiles to (every file of myFolder whose name extension is "png") as alias list
	repeat with nextFile in photosFiles
		
		set nextName to name of nextFile
		set theYear to text 1 thru 4 of nextName
		set theMonth to text 6 thru 7 of nextName
		set theDay to text 9 thru 10 of nextName
		set theHour to text 12 thru 13 of nextName
		set theMinutes to text 15 thru 16 of nextName
		set theSeconds to text 18 thru 19 of nextName
		
		set myAmPm to "AM"
		if (theHour as integer) ≥ 12 then
			set theHour to ((theHour as integer) - 12) as string
			set myAmPm to "PM"
		end if
		
set dateString to theDay & "/" & theMonth & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds & space & myAmPm
		set the_date to my date dateString
		set modification date of file nextFile to the_date
		
	end repeat
end tell
       set myAmPm to "AM"
       if (theHour as integer) ≥ 12 then
           set theHour to ((theHour as integer) - 12) as string
           set myAmPm to "PM"
       end if

Does this yield the correctly-formatted time between 12 midnight and 1 AM?

Why not?

This new 12-hour script also does not work either (nor does setting the system time format to 24 hours using the prior script).

The Finder timestamp for the filename

2018/12/18 11.30.27

says:

June 12, 2019 11:30 AM

so there is something more majorly off with the script.

In other words, it’s not just an AM/PM issue because the date is way off, in fact it’s in the future!

No issue, no. If this is indeed the Finder displays your dates in this form, then you should simply replace the code line

set dateString to theDay & “/” & theMonth & “/” & theYear & space & theHour & “:” & theMinutes & “:” & theSeconds & space & myAmPm

in the script with such a line:

set dateString to theMonth & “/” & theDay & “/” & theYear & space & theHour & “:” & theMinutes & “:” & theSeconds & space & myAmPm

It’s just that your Finder shows the date in the order month/day/year, and I have a day/month/year. What I have been talking about for 2 hours

Yes, perfect! Thanks for your help and patience!

The script which I posted is not related to the date-time format in use on your machine.
So it may be used worldwide .

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mars 2019 10:10:23

Wouldn’t your script, for example, change 00:01 (24-hour clock) to 00:01 AM (12-hour clock) rather than 12:01 AM and 12:01 (24-hour clock) to 00:01 PM (12-hour clock) rather than 12:01 PM.

BTW, my use of “correctly-formatted” was a poor choice of words.