As part of my next gig, I will be receiving a series of ZIP files of 300+ photos that have a date-based filename:
00389_2022-08-27T201422-0500
but due to some unknown problem, the Date Created and Date Modified are always the time of the ZIP file being created maybe or when it is downloaded or unzipped, so all the photos have the same useless creation and mod date.
I’ve been working on a script to grab the date/time from the name and format it and use setfile
to change the file creation and mod date.
I can get it to make (what I think is) the correctly formatted string but I keep getting a
Error: a privilege violation occurred (errAEPrivilegeError:-10004)
the command I send to setfile
from SD is
setfile -md '08/27/2022 20:14' '/Users/Production01/Downloads/ZIP021_Pictures/00389_2022-08-27T201422+0200.jpg'
I have Script Debugger with full file access in Prefs and the error seems to indicate it’s Finder related.
Point me in the right direction please?
N.B. I am quite new to scripting but I have been doing a lot of reading and lifting and repurposing of other people’s work. So I expect there is a lot of improvement possible in the script below.
Eventually I want to have an Automator app I can dump a new folder of files on and let it change the mod or creation date (or both) for the whole folder based on the name of each file.
tell application "Finder"
-- choose a file
set theFile to (choose file)
-- get its filename
set f_name to name of theFile
-- extract the date component
set {the_date, the_Time} to my getDateFrom(f_name)
set new_Mod to the_date & space & the_Time
-- assuming we get something that looks like a date...
if the_date ≠ "" then
-- use a shell command to change the creation date
do shell script "setfile -md " & "'" & new_Mod & "'" & space & quoted form of POSIX path of theFile
end if
end tell
on getDateFrom(theFileName)
-- assume the worst
set new_date to ""
-- save the current state
set old_tids to my text item delimiters
-- use a try block to catch errors
try
-- split the string on "- "
set my text item delimiters to "_"
-- get everything after the "_"
set dateString to text item 2 of theFileName
-- now switch to splitting the string on "."
set my text item delimiters to "-"
set dateYear to text item 1 of dateString
set dateMonth to text item 2 of dateString
set dateClean to text item 3 of dateString
set my text item delimiters to "T"
set dateDay to text item 1 of dateClean
set dateTyme to text item 2 of dateClean
set my text item delimiters to "+"
set dateTime to text item 1 of dateTyme
set timeHour to text 1 thru 2 of dateTime
set timeMin to text 3 thru 4 of dateTime
set timeSec to text 5 thru 6 of dateTime
-- extract the date components - may fail if the string doesn't match the expected format
set {y, m, d, h, Min, s} to {dateYear, dateMonth, dateDay, timeHour, timeMin, timeSec}
-- restore state
set my text item delimiters to old_tids
-- calculate the date string in the required format
set new_date to m & "/" & d & "/" & y
set new_Time to h & ":" & Min
end try
-- pass back to the caller
return {new_date, new_Time}
end getDateFrom