Mac – Change Date Created based on the filename

applescriptcommand linemac

I've downloaded a bunch of my Instagram images but the Date Created of the files is set to the time that I initiated the download. What I do have though is the Date Created in the filename. What I want to do is take the date in the filename, which I've changed to YYYYMMDDhhmm, and set that as the creation date of each file.

I changed the date in the filename into the YYYYMMDDhhmm because this is the format that the touch -t command likes. Now all I need is a way to take the first 12 characters of the filename, pass it to touch -t as the date to touch the file with and follow that with the full filename.

I imagine this could be accomplished with a combination of Hazel and a shell/AppleScript so that the date could be passed on from each individual image.

Cheers for any help 🙂

Best Answer

So after some hunting I stumbled on this little AppleScript on Mac OS X Hints. Thanks Krioni! I'm sure with some tinkering I could modify it to work with my filenames, but I just batch edited the image filenames to fit in with the script.

on open files_
    repeat with file_ in files_
        tell application "Finder"
            set file_name to name of file_
            set od to AppleScript's text item delimiters
            set AppleScript's text item delimiters to {"_"}
            set new_creation_date to second text item of file_name
            set new_creation_date to new_creation_date & "1000"
            set file_ to POSIX path of file_
            do shell script "touch -t " & new_creation_date & " " & quoted form of file_
            set AppleScript's text item delimiters to od
        end tell
    end repeat
end open

It simply looks for the second text item of the filename in between underscores and uses that as the date. It then adds "1000" as the time to the touch command. Hopefully this will help someone else out there.

The original thread can be found here.