Fixing incorrect Creation and Modification dates of PDFs downloaded in Big Sur beta
I’ve found a bug (filed as FB7987637) in Safari in Bug Sur betas (1 and 2), which manifests itself when saving PDF files from the built-in PDF viewer using the button on the floating button at the bottom of the PDF view.
Images saved by right-clicking on them and selecting Save Image to “Downloads” exhibit the same problem, as I found out later.
What happens is that the saved file has a modification and creation dates (as seen in Finder) to Jan 1, 1970 UTC (displayed in your time zone) instead of the expected ones.
I didn’t find a way to change the creation time from the command line (the touch
only appears to be able to change the modification time).
I modified a Python script that I found online to do the work of setting the creation and modification time to the current time. I also wrote a quick Swift command-line tool to do the same, and it works on my Mac, but releasing command-line tools is not so simple and required too many hoops for this simple utility. The Swift code is below for those interested. You can download the Python script from dr.apparentsoft.com/HEQhQL. You may need to set the executable flag on it:
chmod a+x touchCreationModification.py
It can be used similarly to the regular touch command, by passing a file name as the first argument.
As a user of Hazel, I created a quick rule for the Downloads folder which monitors for files with creation date before 1971 and invokes TouchCreationModification on these files. Hazel does not work well on Big Sur yet, though, so getting to the Edit Script stage was tricky (had to go through its “Run Apple Script” option first, to make the editor load.
Hopefully, all this will be unnecessary in the next beta. But in the meantime, programming for the win!
Swift code (has better error management than the Python script):
import Foundation
let args = CommandLine.arguments
guard args.count > 1 else {
print(“Error: Pass a file name argument”)
exit(-1)
}
let filePath = args[1]let fm = FileManager.default
guard fm.fileExists(atPath: filePath) else {
print(“Error: File (filePath) doesn’t exist”)
exit(-2)
}
do {
let now = Date()
let dates: [FileAttributeKey: Date] = [.modificationDate: now, .creationDate: now]
try fm.setAttributes(dates, ofItemAtPath: filePath)
} catch {
print(“Error setting attributes of (filePath)”)
exit(-3)
}