Hi, this is Gary with MacMost.com. Today I'm going to show you how to
change the creation date of a file to match the photo date.
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to
MacMost.com/patreon. There you can read more about the Patreon Campaign.
Join us and get exclusive content and course discounts.
Now I get a lot of questions about this particular problem that happens when you export a photo
from something like the Photos App. So say you take a photo like this one. If I get information
on it with Command i, I can see this photo was taken on May 27 at 6:59. If I drag and drop it
to the Desktop to create a file I can select it, do Command i to get info in the Finder on it. I
can see now the created date is June 14th at 4:01 which is right now. So what's going on here. Well,
both are correct. This is the creation date for this file. I just created the file.
The photo in it has metadata that is the date that the photo was taken. That's what we see here in
the Photos App when we get info on it. Now you may think that by exporting the file it has wiped out
that photo metadata. So it no longer remembers when the photo was taken. That's not true.
If I double click on the photo it opens up in Preview. I go to Tools and then Show Inspector
and then here under info exif I get the metadata and there is the date of the original photo.
So it does remember the metadata. If I were to import this into another photo management app
or back into photos it would use this date and time here as the date the photo was taken. So
everything is fine and that may be all that you need to know. That the metadata is preserved.
However some people would like the photo creation date to match the photo date. Like
they took the photo and instantly put it onto their Mac. Now technically this isn't kosher.
A file creation date should be the date that the file was created. If you want to
cheat and change it you can though. So it's your file. If you want to do it here's how.
Let's start off in Terminal and I'm already here on the Desktop. So I've gone there. So I
could see this file here. I could actually change the creation date using the Touch Command. Touch
dash t changes the creation date. You have to use a weird format. It's the year as four digits
and then the month, let's say January 31st, and then the hours as two digits. So let's say
13 for one o'clock and then thirty for 1:30. Then the file name. So I can start typing the
file name and autocomplete it there. That would actually change the creation date for that file.
However, I'm not going to do it here. The Touch Command is very temperamental. First of all it
can only set a creation date if it's earlier than the current creation date of the file. So if this
file had a creation date that was say 2020 and I tried to change it to 2021 it wouldn't work.
Also if you tried to do it too much or even just a second time with the same file
sometimes it doesn't work either. So it's kind of temperamental.
macOS and the Finder are not built for having you manually change the
creation dates of files. It's kind of sacred to keep the file creation dates as they were.
The next thing we want to do is get the actual date of this file. MDLS will get us all of the
metadata. So for this file I get a huge list of metadata. Way up here at the top of it
I will see one item here that gives me the actual creation date. You can see that matches.
Or it doesn't quite match. Notice the time is different. This has it at midnight. Midnight
the next day. It's six hours ahead because there is a time difference. This is universal time.
So plus is zero. Whereas I'm actually at plus six here in mountain time. We'll deal with that later.
For now I'm going to go and pipe the results. Pipe is the character above the Return key on
your keyboard. You have to use Shift and it's just a straight up and down line. That means
send the results of this into something else. I'm going to do grep and I'm going to look for
a line that starts with this. When I do that I get that creation date right here. I also get another
line here, underscore Ranking. I don't want that. So I'm going to pipe yet again to head dash
n1 which will say just give me the first line at the head of the results. There I get that. Now I'm
going to pipe yet again a third time to awk which allows you to run more commands. I'm going to use
this which will substitute digits, actually things that are not digits, I've got the not here and
put a space there instead. In other words clear out everything except the digits with spaces
in-between. So this is just going to create a bunch of numbers and I'm going to get the first
number, second number, third, fourth, and fifth number and that will give me that. So instead
of this here it's actually going to be taking just these numbers and it's going to just have the
numbers. No punctuation or spaces in-between which is the correct format for that touch command.
Okay, so the next thing I want to do with this is subtract that six hours to get
the time right. So I want to use the Date command for that. So date and j and f and
I want to give it a format to accept for input. So that would be this. So you can see the year,
month, and day, hour, minute. This format here. I'm going to do dash v and subtract six hours.
Capital H there to get the time right. So you're going to have to play around with this and see
how far the times are off for your photos and adjust with either a plus or minus and a number
with a capital H for the number of hours. Now I want to pass the time in there. So I going to do
dollar sign and parentheses and this will make it evaluate what's inside the parentheses which
is going to be all of this. So I'm going to paste it in and then close the parentheses.
Now I want to output the time in the same format. So I'm actually going to take this format here
and output it. So now you could see I get the time adjusted. Now it is 6:59.
So now all we need to do is use this inside of Touch. So we'll do touch dash t
and we will take this entire thing, copy it, but I'm also going to have to tell touch to
evaluate that before using it. So put that inside a parentheses with a dollar sign in front, close
it off like that, and then I want to have the file name again, so the same file name here, and
this should change the date there. So let's go and bring up the info on this. You could see here it's
June 14th. Let's run this. You could see, ah!, it changed it and got it right. Now let's put all of
this inside an Automator script so we can run it whenever we want just by dragging it and dropping.
So in Automator I'm going to create an Application. I'm going to simply look for
the Shell Script here, run Shell Script. I'm going to say asking put as argument. This will
put this handy little loop here for us. It's going to take the arguments and it's going to
loop through each one. So if there are multiple files it will do the same thing for each one.
It's going to store the name of the file in f and here it's just going to just print out the name
of the file. That's not what we want. What we want is to use our Shell Script here. I'm going to grab
this and paste it in. But I don't need the file name now. Instead I need this f right
here. So dollar sign f. We have it actually in another place, dollar sign f. So now we'll save
it and we'll save it to the Desktop. We'll say change photo file date and we can test it out.
We've got the app right there. Let's go to Photos and grab a couple of photos here. This one, this
one, and this one. I'm going to drag them here. Great. Now, let's get info on all three of these.
We can see here that they are now set to the current time. That's not what we want.
They should be set to the time when they were originally taken. What's in the metadata. So
let's drag and drop these all onto here. We can see here it changes them all. So, now we have a
handy way to drag and drop any photo files we want on here to use this to set the file creation date
equal to the photo date. It will work as long as we're doing it as pretty much the first thing we
do with these files once we add them to the Finder. Once we export them from the Photos
App and we only try to do it once. Otherwise the Touch Command may not work as expected.
I don't necessarily recommend doing this and I personally, when I export files, I don't
try to use this. But if you're doing a one time thing where you need to export a bunch of files
from a Photos Album and you want to set the dates right before you, say,
put them on a USB stick and give them to somebody, then I can see possibly using
this automated workflow to get it done. I'll include this bit of code here since it's pretty
long and you wouldn't want to type it and you get one character wrong and it's just not going to
work at all. It's got to be perfect and also make sure you're running this as a zShell bash script,
not one of these other ones, or it may not work. You've remembered to accept the input as arguments
and created an application here in Automator like this. It may also ask for permission to
access file the first time you run this. But maybe when you run it in a new location as well.
So I only recommend using this, or anything like this, if you really understand what it's doing and
really need to accomplish this particular task of changing those file creation dates
to match when the photos were actually taken.