Thank you so much Camelot for this answer. Also thank you Jacjac for posting the original question! A 5-year old post that apparently is still very helpful for my issue.
A little background: I have an Android camera that stores files as "yyyymmdd_hhmmss.jpg", I store photos in Google Photos (usually using its auto-backup feature, or its Android app, except in certain cases), and use a Mac.
In some cases Google Photos takes the creation/modification date (unsure) as opposed to the EXIF data for some reason (not always consistent in my observation). However, being Android, every time you move or copy a file within it, the creation/modification date is also updated, and it so happens that I was not able to maintain the original times. The adjusted times causes my photos to appear out of sequence. Thus I needed to modify the times myself, based on either EXIF or filename, and here we have a solution based on the filename.
So here's the updated version of the above script, taking into account the seconds indicated in the filename. As you can see, I only modified it to fit my filename syntax.
on open of theFiles
-- This is executed when files are dropped on the script
repeat with i in theFiles
if folder of (info fori) then
processAFolder(i)
else
processAFile(i)
end if
end repeat
end open
on processAFile(theFile)
set theName to name of (info fortheFile)
-- Assumes file syntax begins with YYYYMMDD_hhmmss
try
set yy to characters 3 through 4 of theName as text as integer
if yy < 10 then set yy to "0" & yy
set mm to characters 5 through 6 of theName as text as integer
if mm < 10 then set mm to "0" & mm
set dd to characters 7 through 8 of theName as text as integer
if dd < 10 then set dd to "0" & dd
set hr to characters 10 through 11 of theName as text as integer
if hr < 10 then set hr to "0" & hr
set min to characters 12 through 13 of theName as text as integer
if min < 10 then set min to "0" & min
set sec to characters 14 through 15 of theName as text as integer
if sec < 10 then set sec to "0" & sec
do shell script "touch -t " & yy & mm & dd & hr & min & "." & sec & " " & quoted form of POSIX path of theFile
end try
end processAFile
on processAFolder(theFolder)
tell application "Finder"
set theContents to items of foldertheFolder as alias list
end tell
repeat with i in theContents
if folder of (info fori) then
processAFolder(i)
else
processAFile(i)
end if
end repeat
end processAFolder
Peace!