Setfile to change file mod date - Privilege error

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

That looks like a quarantine issue. Try moving them out of the Downloads folder first.

Thanks. Where does a quarantine issue arise from?
OS, Script Debugger, or? Trying to learn. Thanks

Any files downloaded into the Downloads folder has quarantine information added to them – moving them should move it.

Solved.
I modded the script to ask for files and iterate the same basic script as above, threw it into a Run AppleScript in Automator and ran it on the first file of photos I was sent. No errors or problems and everything is fine.
I am sure this looks like an amateurish script but it did the trick. Could be simpler if I could use RegEx but I don’t think that’s possible in AS, right?
Script Debugger for really is invaluable for stepping through the order of operations and seeing the results and building from there. Great learning tool.
Anyways, here’s what worked for me. It only works for that very specific filename format of course.

tell application "Finder"
	
	-- choose a file
	-- set theFile to (choose file)
	set theselectedFiles to choose file with prompt "Please select some images to process:" with multiple selections allowed
	
	
	-- get its filename
	repeat with theFile in theselectedFiles
		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 -d " & "'" & new_Mod & "'" & space & quoted form of POSIX path of theFile
			
		end if
	end repeat
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
		
		-- now switch to splitting the string on "T"
		set my text item delimiters to "T"
		set dateDay to text item 1 of dateClean
		set dateTyme to text item 2 of dateClean
		
		-- now switch to splitting the string on "+"
		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
		set {y, m, d, h, Min, s} to {dateYear, dateMonth, dateDay, timeHour, timeMin, timeSec}
		
		-- restore delimiters
		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 results back to the caller
	return {new_date, new_Time}
end getDateFrom

It’s entirely possible you specifically want to use setfile to change the creation date, but just in case you weren’t aware, you can set the modification date through Finder as well, e.g.

tell app "Finder" to set the modification date of theFile to...

Although you can’t set the creation date directly, if the modification date is set to something that’s earlier than the creation date, then the creation date is changed to match that of the modification date. This should work out well for your situation, as any photo would have been zipped or downloaded after it was taken, so you’ll always be adjusting the modification and creation dates to values earlier than what they come with.

Of course, this means you’ll have to supply Finder with an AppleScript date class object, which can be done like this:

to getDateFrom(theFileName)
		-- Assuming a filename similar to this:
		-- "00389_2022-08-27T181422+0200.jpg"
		
		-- Replace punctuation with spaces then split into words:
		set my text item delimiters to {space, "_", "-", "T", "+", "."}
		set components to words in (theFileName's text items as text)
		
		-- The components should be a list of 7 items resembling:
		-- {"00389", "2022", "08", "27", "181422", "0200", "jpg"}
		if the number of components ≠ 7 then return ""
		
		-- The first component and the last two can be discarded
		-- to give a list comprised of {yyyy, mm, dd, HHMMSS}
		set components to items 2 thru 5 of the components
		
		-- Convert HHMMSS into seconds (since midnight)
		tell (a reference to last item of the components) ¬
				to set its contents to (the ¬
				text 5 thru 6 + 60 * (the ¬
				text 3 thru 4 + 60 * (the ¬
				text 1 thru 2)))
		
		-- Get the current date and set its components to ours
		set the_date to the current date
		tell the_date to set the {year, its month, day, time} to the components
		return the_date
end getDateFrom

The rest of the code from within the Finder block stays virtually the same, replacing only the call out to do shell script and removing those lines that that went into creating the shell command string:

tell application "Finder"
		set theselectedFiles to choose file with prompt ¬
				"Please select some images to process:" ¬
				with multiple selections allowed
		
		repeat with theFile in theselectedFiles
				set f_name to name of theFile
				set the_date to my getDateFrom(f_name)
				if the_date ≠ "" then set the modification date of theFile to the_date
		end repeat
end tell
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.