How to set file's Created Date

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

How to set file's Created Date Sat Feb 19, 2022 10:37 pm • by ringgit
I use Drafts to enter my ideas. Once a week, I reviewed my Drafts and saved those I needed into iCloud as files. In Drafts, it will always put the timestamp in the first line when it saves, for example:
//Sat, 19 Feb 2022, 9:42 PM

The issue at hand is Drafts will set the Create Date to the current date-time when it creates the file. My notes could be taken a week ago but the Finder will show that the exported files are created today.

I have a Hazel rule that I want to use to help me fix the file's metadata. I managed to match the timestamp above in the condition. But I don't know how I can make Hazel change the Created date of the file. In theory, I should run a Terminal command to do "touch -t [date] %1", but what can I put as [date]? The touch command expects the date format to be yyyymmddhhmm, for example, 202202192142, but Hazel's date attribute, which I called z-now, is in this format: 19 Feb 2022, 9:42 PM

I appreciate any guidance or pointers to the right direction. Thank you.
ringgit
 
Posts: 14
Joined: Sun Oct 31, 2021 1:57 am

Re: How to set file's Created Date Mon Feb 21, 2022 4:11 am • by ringgit
Answering my own question - I researched, copied some codes and put these together. Hope it helps someone else:

(I am using a Embedded AppleScript and passed on z-now, an attribute I created to capture timestamp, into the AppleScript)
https://imgur.com/a/4BbqG4U

Code: Select all
set ddir to POSIX path of theFile
# this is to convert the path separated by colon to slash
set h_timestamp to item 1 of inputAttributes
set {year:y, month:m, day:d} to h_timestamp
# aka 1999 - January - 2
set nummonth to do shell script "date '+%m'"
# aka 01
set dd to text -2 thru -1 of ("00" & (day of h_timestamp))
# aka 02
set yy to text 3 thru 4 of (y as text)
# aka 99
set tt to time string of (h_timestamp)

tell application "System Events"
   display dialog "SetFile -d " & "'" & nummonth & "/" & dd & "/" & yy & " " & tt & "' " & quoted form of ddir
end tell
# quoted form of is to put the path in quote for long filenames

tell application "Finder"
   do shell script "SetFile -d " & "'" & nummonth & "/" & dd & "/" & yy & " " & tt & "' " & quoted form of ddir
end tell
ringgit
 
Posts: 14
Joined: Sun Oct 31, 2021 1:57 am

Re: How to set file's Created Date Mon Feb 21, 2022 8:14 am • by ringgit
One thing I am not entirely sure though - once Hazel changed the Date Created of the file, will it run the same AppleScript and make the change again and again?
ringgit
 
Posts: 14
Joined: Sun Oct 31, 2021 1:57 am

Re: How to set file's Created Date Mon Feb 21, 2022 9:56 am • by Mr_Noodle
It depends on the conditions in your rule.
Mr_Noodle
Site Admin
 
Posts: 11951
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: How to set file's Created Date Mon Feb 21, 2022 10:14 am • by ringgit
Mr_Noodle wrote:It depends on the conditions in your rule.

Hmm, my condition is pretty simple. It is Contents contain match "//" and (date time attribute).
It means that every time Hazel read this file, it will match. And it means it will keep on changing the Created Date. Hmm, not a good idea then. Maybe I will have to tag the file after processing. What would you recommend?
ringgit
 
Posts: 14
Joined: Sun Oct 31, 2021 1:57 am

Re: How to set file's Created Date Mon Feb 21, 2022 11:53 am • by Mr_Noodle
In general, Hazel will try not to run the actions again if the file keeps matching the same rule, but it's not guaranteed so yes, I think adding a tag would be a good idea. Or even better, if the file no longer needs to be monitored, move it to another location so that Hazel doesn't use resources scanning it again.
Mr_Noodle
Site Admin
 
Posts: 11951
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: How to set file's Created Date Tue Feb 22, 2022 3:33 am • by ringgit
Mr_Noodle wrote:Or even better, if the file no longer needs to be monitored, move it to another location so that Hazel doesn't use resources scanning it again.

In my current use case, I am exporting notes from Drafts to my Obsidian folder. So, I don't want Hazel to move them away as I will be filing them into the right directory from Drafts. My challenge was that if I use Drafts' Export, it will always create the .MD file on the date it was saved, not the date the note was written. That's where Hazel comes in, to help me fix the Creation Date.

I think I will use tags to mark that Hazel has processed this file. Does the sequence of the condition matter? To reduce the number of files Hazel has to read, I should put "tag" condition first before "contents contain match".
ringgit
 
Posts: 14
Joined: Sun Oct 31, 2021 1:57 am

Re: How to set file's Created Date Tue Feb 22, 2022 9:41 am • by Mr_Noodle
That would be a good idea.

Going back to the other idea, note that you can have your program put files into a transition folder which Hazel monitors and then moves to the final location.
Mr_Noodle
Site Admin
 
Posts: 11951
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support

cron