0

I hope some clever guy/girl can help me out. I've got 1000s of photos in several folders that have the wrong created date (my HDD failed and when I recovered them they all had the recovery date set as created date). Most of the files have the date they were taken in the filename in the format '(YYYY-MM-DD HH-MM-SS)....' with some other text where the ... is and the filename has the ()

I am trying to write an Applescript to change the created date based on the filename. This would then allow the photos to be properly organised in Apple Photos. I haven't programmed for years (I was bad back in the day) and was struggling but needed to write something to say:

if filename contains (YYYY-MM-DD HH-MM-SS) set file date created to (YYYY-MM-DD HH-MM-SS)

and to do this for an entire folder of photos.

Thanks so much for any help. It is my wife's life's photos and you could really help my marriage :)

3 Answers3

0

The simplest method is to install Exiftool. I have used this utility for years, and although it is a command-line tool, it is simple to use via AppleScript's do shell script command.

I have a few dozen scripts that utilize it already, so if you have any difficulties, post back here.

Craig Smith
  • 1,063
  • 2
  • 11
  • 21
  • Thanks so much for your help. I will have a crack at this and let you know how I get on. Thanks again –  Feb 21 '16 at 17:11
0

Before Exiftool (thanks Craig), you must extract and convert your 'yyy-mm-dd HH-MM-SS' to exiftool date/time format which is yyyy:mm:dd HH:MM:SS

The piece of code below uses Applescript text items delimiters to parse and reformat. You may have to add test to make sure this is valid date/time.

set FileName to "(2016-01-16 15-53-37)my photo 001.jpg" -- just an example

set AppleScript's text item delimiters to {"(", ")"}
set TempDate to text item 2 of FileName
set AppleScript's text item delimiters to {"-"}
set NList to text items of TempDate
set AppleScript's text item delimiters to {":"}
set NewDateTime to NList as string -- format yyy:mm:dd HH:MM:SS

For Exiftool, syntax should be :

   do shell script "/usr/local/bin/exiftool -CreateDate='" & NewDateTime & "' -DateTimeOriginal='" & NewDateTime & "' -Overwrite_Original " & quoted form of (POSIX path of your_file)

Look in 'man' page of Exiftool after you installed it. It also allows you to set keywords directly in photos and many other attributes). The result is that when you copy your photos somewhere else, they will keep such attribute (not the case with attributes set in iPhoto and Photos).

pbell
  • 2,965
  • 1
  • 16
  • 13
  • Thanks so much for your help. I will have a crack at this and let you know how I get on. Thanks again –  Feb 21 '16 at 17:10
0

Although this question is already quite old I had a very similar problem: My OneDrive folder changed somehow all the creation-date timestamps of about 1000 photos I had. I created a Script that tries to convert the time-stamp based on the file-name for the selected files in Photos. The Files that cannot be converted are written in some log-file on the desktop.

Maybe this helps someone in the future

(* Batch change the capture-date of the selected photos based on filename.
 
How to use this script:
- Select all photos you want to change the date timestamp of in Photos
- Open this script and run it by pressing the "Run" button in the toolbar.
- The script will read the filename from the selected images and tries to convert the filename to some date-time format
- The script will return the last date of the last photo it changed
 
- if you save this script as an Application you can add it to the Dock and run it from there
 

*)

(* select at least 1 image in Photos *)

set originalDelimiters to AppleScript's text item delimiters
tell application "Photos"
    activate
    set image_selection to (get selection)
    set n_changed to 0
    if (image_selection is {}) then
        error "Please select at least one image."
    else
        repeat with i from 1 to count of image_selection
            set flag to true
            set image to item i of image_selection
            set image_name to (the filename of image)
            -- convert name to date
            if (image_name starts with "PHOTO") then
                -- Example filename: PHOTO-2015-09-13-22-34-35.jpg
                --set AppleScript's text item delimiters to "-" and "."
                set AppleScript's text item delimiters to {"-", "."}
                --set name_elements to text items of image_name
                set dateList to text items 2 thru 7 of image_name
                set AppleScript's text item delimiters to originalDelimiters
                set theYear to item 1 of dateList
                set theMonth to item 2 of dateList
                set theDay to item 3 of dateList
                set theHour to item 4 of dateList
                set theMinute to item 5 of dateList
                set theSecond to item 6 of dateList
                set theDate to createDate(theDay, theMonth, theYear, theHour, theMinute, theSecond) of me
            else if (image_name starts with "Foto ") then
                -- "Foto 23.06.13 02 35 59.jpg"
                set AppleScript's text item delimiters to {" ", "."}
                set dateList to text items 2 thru 7 of image_name
                set AppleScript's text item delimiters to originalDelimiters
                set theDay to item 1 of dateList
                set theMonth to item 2 of dateList
                set theYear to item 3 of dateList
                set theHour to item 4 of dateList
                set theMinute to item 5 of dateList
                set theSecond to item 6 of dateList
                set theDate to createDate(theDay, theMonth, theYear, theHour, theMinute, theSecond) of me
            else if (my is_int(items 1 thru 8 of image_name as string)) then
                --"20171101_165319642_iOS.jpg"
                set theYear to items 1 thru 4 of image_name as string
                set theMonth to items 5 thru 6 of image_name as string
                set theDay to items 7 thru 8 of image_name as string
                set theHour to "11"
                set theMinute to "11"
                set theSecond to "11"
                set theDate to createDate(theDay, theMonth, theYear, theHour, theMinute, theSecond) of me
                
            else
                -- skip image
                set flag to false
                --display dialog "If name does not start with \"PHOTO\" is not implemented yet."
                my WriteLog(image_name)
                
            end if
            if flag then
                tell image
                    set the date of image to theDate
                    set n_changed to n_changed + 1
                end tell
            end if
        end repeat
    end if
    return "Adjusted the dates of " & (n_changed as string) & " photos."
end tell

on createDate(theDay, theMonth, theYear, theHour, theMinute, theSecond)
    set theDateString to (theDay & "/" & theMonth & "/" & theYear & " " & theHour & ":" & theMinute & ":" & theSecond) as string
    --display dialog the theDateString
    set theDate to date theDateString
    return theDate
end createDate

on is_int(txt)
    try
        set int to txt as integer
        return true
    on error
        return false
    end try
end is_int

-- https://stackoverflow.com/a/3781066
on WriteLog(the_text)
    set this_story to the_text
    set this_file to (((path to desktop folder) as text) & "log_NonConvertedPhotoNames")
    my write_to_file(this_story, this_file, true)
end WriteLog

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    try
        set the target_file to the target_file as text
        set the open_target_file to ¬
            open for access file target_file with write permission
        if append_data is false then ¬
            set eof of the open_target_file to 0
        write (this_data & return) to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error
        try
            close access file target_file
        end try
        return false
    end try
end write_to_file
Ray
  • 1
  • 1