Copy video create date from original file

This is the companion script for: Video re-encode script

It uses exiftool to copy the video date atom(s) and setfile (is or was included with Apple’s Developer Tools) to change the file system dates. It will more than likely throw exiftool “File not found” errors and I’m not quite sure why. It has never failed to actually work despite this in my experience, however be very careful to not use this on your original source videos.

Hopefully the settings and prompts make sense.

(* http://strawhousepig.net/

This uses Apple's command line developer tools command 'setfile' to set the filesytem dates.

https://developer.apple.com/download/more/

I believe an Apple Developer account is required for that, but I am not sure.

Uses 'exiftool' to set track date atoms.

https://www.sno.phy.queensu.ca/~phil/exiftool/

*)

-- Set to 'false' to omit initial dialog prompts. Does not affect final confirmation prompt.
property prompt_me : true

-- UTC timezone. Leave blank ("") to not use the timezone setting.
property tzone : "-8:00"

on run
  do_it(false)
end run

on do_it(redo)
  repeat
    if tzone is not "" then set tzone to "-timezone="" & tzone & "" " -- There is a trailing space here.    
    if prompt_me is true then
      if button returned of (display dialog "Choose a TARGET file to set creation and modification dates of.") is "OK" then
        set newfile to choose file with prompt "Select TARGET file to set creation and modified dates of:"
      end if
    else
      set newfile to choose file with prompt "Select TARGET file to set creation and modified dates of:"
    end if
    
    set filepath to newfile as string
    if prompt_me is true then
      if button returned of (display dialog "Choose a SOURCE file to *get* creation and modification dates from.") is "OK" then
        set oldfile to choose file with prompt "Select SOURCE file to get dates from: " & filepath
      end if
    else
      set oldfile to choose file with prompt "Select SOURCE file to get dates from: " & filepath
    end if
    set newfilename to name of (info for of newfile)
    if newfilename is not (name of (info for of oldfile)) then
      set warn_button to button returned of (display alert "File names do not match. Proceed?" as warning buttons {"Cancel", "Start Over", "Proceed"})
      if warn_button is "Cancel" then
        return
      else
        if warn_button is "Start Over" then
          set redo to true
          exit repeat
        end if
      end if
    end if
    tell application "System Events" to set [c_date, m_date] to [creation date of oldfile, modification date of oldfile]
    set [p_date_c, p_date_m] to [c_date, m_date] -- Saving "pretty" date for dialog prompt.
    set c_date to ((month of c_date as integer) & "/" & day of c_date & "/" & year of c_date & " " & hours of c_date & ":" & minutes of c_date & ":" & seconds of c_date) as string
    log c_date
    set m_date to ((month of m_date as integer) & "/" & day of m_date & "/" & year of m_date & " " & hours of m_date & ":" & minutes of m_date & ":" & seconds of m_date) as string
    log m_date
    if button returned of (display dialog "This will set the creation and modification dates of file " & filepath & " to:" & return & return & p_date_c & return & p_date_m & return & return & "Proceed?") is "OK" then
      try
        -- Have to include full path to exiftool binary because AppleScript uses the Bourne shell which doesn't have /usr/local/bin in its path? *rolls eyes*
        do shell script "/usr/local/bin/exiftool " & tzone & "-*date="`/usr/local/bin/exiftool -time:CreateDate " & quoted form of POSIX path of oldfile & "`" -wm w " & quoted form of POSIX path of newfile
      on error theErr
        -- Will throw a "File not found" error if this takes over a certain amount of time. rm must be running faster than exiftool can finish?
        display dialog "The 'exiftool' command said: " & theErr
      end try
      delay 1
      try
        -- Ready for this, exiftool adds "_original" to the end of the file it's going to modify tags/atoms of. This is still 'newfile' but with a new name.
        -- If we manually add "_original" to 'newfile' rm will throw an error looking for newfile_original_original... Wha?
        do shell script "rm " & quoted form of POSIX path of newfile
      on error theErr
        display dialog "The 'rm' command said: " & theErr
      end try
      delay 1
      try
        -- Ready again? The original path to 'newfile' is valid again for this... Wha??
        do shell script "setfile -d '" & c_date & "' -m '" & m_date & "' " & quoted form of POSIX path of newfile
      on error theErr
        display dialog "The 'setfile' command said: " & theErr
      end try
    end if
    exit repeat
  end repeat
  if redo then do_it(false)
end do_it

Leave a Reply

Your email address will not be published. Required fields are marked *