SOLVED: How to modify Mac Automator Script to rename MOV with EXIF date/time?

Started by Doug Joseph, September 29, 2023, 11:11:48 PM

Previous topic - Next topic

Doug Joseph

Hello! Thanks for such a great tool. First time using it, and first time posting.

I had already posted this on the Mac Community forum, but since the existing Automator script requires ExifTool to be installed, I wondered if someone here might be able to help.

First, I am thrilled to have found an Automator script written and posted by VikingOSX here:

Script to rename pictures with date and t... - Apple Community

....Which totally works... in renaming HEIC, JPEG, and PNG images to their EXIF date and time data.

However, I also need to rename .MOV files too.

Now, VikingOSX's instructions were excellent. However, in order to get his automator script (app) to work, I had to do one more step. When VikingOSX wrote:

QuoteThen one drags and drops the following actions from their respective Library (in order) to the larger workflow window:
1. Files & Folders > Ask for Finder Items
2. Utilities > Run AppleScript

On that first one, I had to change the "Type" from "Files" to "Folders" as shown:

Before:



Choosing:




After:




Would anyone here be able to advise me on how to revise the script to also rename .MOV files?

Here's the original script:


property imageKind : {"HEIF Image", "JPEG image"}
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"property imageCnt : (0 as integer)

on run {input, parameters}

set parentdir to input
set EXIFTOOL to (do shell script "PATH=\"/usr/local/bin:/opt/homebrew/bin:$PATH\";/usr/bin/which exiftool")

tell application "Finder"
set img_list to (every item in entire contents of folder parentdir whose kind is in imageKind) as alias list
if img_list = {} then
display alert "No images found" return
end if

repeat with anImage in img_list
set EXT to "." & anImage's name extension
set DTO_string to (do shell script EXIFTOOL & " -s3 -d \"%F %H-%M-%S\" -DateTimeOriginal " & (POSIX path of anImage)'s quoted form)
if not DTO_string = "" then
set outFile to ((parentdir as text) & DTO_string & EXT) as text
set basename to (do shell script "basename " & (outFile's quoted form)'s POSIX path)
set name of anImage to basename
set imageCnt to imageCnt + (1 as integer)
else
log "NO-OP"
end if
end repeat

end tell

display dialog "Processed image count:  " & (imageCnt as text) with title "Processing complete" with icon POSIX file app_icon as alias
returnend run

Doug Joseph

OK, I solved it! :)

For sniffing out how to word the "kind" property, in Mac Finder, I right-clicked on both .MOV and .MP4 files. for .MOV the kind is listed as "QuickTime movie" while for .MP4 the kind is listed as "MPEG-4 movie."



I then cloned the "batch loop" part of the code in the script so I could tailor a new loop for dealing just with the two video file kinds. I tweaked for adding PNG and then I created "video" styled copies of the various property vars and other vars. For example, where the original script had

property imageKind : {"HEIF image", "JPEG image"}

I now have:

property imageKind : {"HEIF image", "JPEG image", "PNG image"}
property videoKind : {"QuickTime movie", "MPEG-4 movie"}

This not only adds PNG catching but allows catching the two video types, .MOV and .MP4.

Also, for having the ExifTool utility to "get at" the date and time of the video files, I sleuthed around, and found that I could get a glimpse of all that the tool could snag from a .MOV file with this command in terminal:

exiftool -G1 -a -s -Time:all /path/to/file.MOV

Which gives a whole list of values (see main part of screen shot below), and from that list, I honed in on "CreateDate" and ran this command in terminal:

exiftool -CreateDate /path/to/file.MOV

...which gives the desired info (see lowest part of screen shot below).



So based on that, where the original loop's crucial bit was:

set DTO_string to (do shell script EXIFTOOL & " -s3 -d \"%F %H-%M-%S\" -DateTimeOriginal " & (POSIX path of anImage)'s quoted form)

...the new loop's crucial bit has:

set DTO_string to (do shell script EXIFTOOL & " -s3 -d \"%F %H-%M-%S\" -CreateDate " & (POSIX path of aVideo)'s quoted form)

And... THAT WORKS! YAHOO!


Here is my whole script:


property imageKind : {"HEIF image", "JPEG image", "PNG image"}
property videoKind : {"QuickTime movie", "MPEG-4 movie"}
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"
property imageCnt : (0 as integer)
property videoCnt : (0 as integer)

on run {input, parameters}
   
    set parentdir to input
    set EXIFTOOL to (do shell script "PATH=\"/usr/local/bin:/opt/homebrew/bin:$PATH\";/usr/bin/which exiftool")
   
    tell application "Finder"
        set img_list to (every item in entire contents of folder parentdir whose kind is in imageKind) as alias list
        set vid_list to (every item in entire contents of folder parentdir whose kind is in videoKind) as alias list

        if img_list = {} and vid_list = {} then
            display alert "No images or videos found"
            return
        end if

        repeat with anImage in img_list
            set EXT to "." & anImage's name extension
            set DTO_string to (do shell script EXIFTOOL & " -s3 -d \"%F %H-%M-%S\" -DateTimeOriginal " & (POSIX path of anImage)'s quoted form)
            if not DTO_string = "" then
                set outFile to ((parentdir as text) & DTO_string & " " & anImage's name) as text
                set basename to (do shell script "basename " & (outFile's quoted form)'s POSIX path)
                set name of anImage to basename
                set imageCnt to imageCnt + (1 as integer)
            else
                log "NO-OP"
            end if
        end repeat

        repeat with aVideo in vid_list
        set EXT to "." & aVideo's name extension
        set DTO_string to (do shell script EXIFTOOL & " -s3 -d \"%F %H-%M-%S\" -CreateDate " & (POSIX path of aVideo)'s quoted form)
        if not DTO_string = "" then
            set outFile to ((parentdir as text) & DTO_string & " " & aVideo's name) as text
            set basename to (do shell script "basename " & (outFile's quoted form)'s POSIX path)
            set name of aVideo to basename
            set videoCnt to videoCnt + (1 as integer)
        else
            log "NO-OP"
        end if
        end repeat
       
    end tell

    display dialog "Processed image count:  " & (imageCnt as text) with title "Image name processing complete" with icon POSIX file app_icon as alias
   
    display dialog "Processed video count:  " & (videoCnt as text) with title "Video name processing complete" with icon POSIX file app_icon as alias

    return
end run