Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can Automator convert an image AND keep the creation date?

I have created a simple Quick Action in automator that copies an HEIC file to a new folder, and converts it to JPEG. It's easy to find how to do this on the web. Works great!


But the new file's creation date is used in the new image. Is there any way in Automator to have it preserve the original creation date as well, copying it to the new JPEG? There must be a way.


I use the finder date to organize my images, so this is important. Thank You.


Posted on Aug 6, 2019 1:03 PM

Reply
Question marked as Best reply

Posted on Aug 6, 2019 6:22 PM

I have a working Automator Quick Action solution that you select the input images in the Finder, and then from Services menu, you run the Quick Action name. The service initially captures the original full path of the input files into a variable input_images, and then proceeds to copy them into the designated folder, where it converts them. The script then captures all the names of the converted images into another variable converted_images.


The Quick Action then retrieves these variables in order and feeds the entire collection of input and converted image names (in that order) as a single command line argument into a Run Shell Script action. I divert the input images to one Zsh array, and the converted images to another Zsh array. Then I use a nested loop to compare the basename of input file with its converted counterpart. When I have a match, I then apply the touch command and assign the original creation and modification date of the input file to its corresponding converted image name. I have tested this with five image files from different Finder locations, and the converted files bear the original creation and modification dates. Tested on Mojave 10.14.6.


The Quick Action has become more complicated to achieve this goal.


Replace the content of the Run Shell Script action with the following code:

#!/bin/zsh

# Note: There is one command-line argument of input followed by converted filenames
# count of all input and output files
typeset -i max=$#
typeset -i idx=0
let "idx = $# / 2"
# temporary array of combined input and converted image names
typeset -a temp=($@)
# slice temp array to get file categories. Zsh arrays are 1 based.
typeset -a input_images=("${(@)temp[1,idx]}")
let "idx++"
typeset -a convert_images=("${(@)temp[idx,max]}")

# now perform nested array comparison
for i in "${(@)input_images}";
do
    for c in "${(@)convert_images}";
    do
        # compare input and converted basenames
        [[ ! $i:t:r == $c:t:r ]] && continue
            # names match so borrow creation/modication date
            # from input filename and apply to output file.
            /usr/bin/touch -r $i $c
            # get another input filename
            break
    done
done

# print appended listing of input and converted filenames if arrays are non-zero in size
{ [ "${input_images[@]}" -eq 0 ] || printf 'input_images = %s\n' "${(@)input_images}";} >> ~/Desktop/foo.txt
{ [ "${convert_images[@]}" -eq 0 ] || printf 'converted_images = %s\n' "${(@)convert_images}";} >> ~/Desktop/foo.txt

exit 0


9 replies
Question marked as Best reply

Aug 6, 2019 6:22 PM in response to udawwgy

I have a working Automator Quick Action solution that you select the input images in the Finder, and then from Services menu, you run the Quick Action name. The service initially captures the original full path of the input files into a variable input_images, and then proceeds to copy them into the designated folder, where it converts them. The script then captures all the names of the converted images into another variable converted_images.


The Quick Action then retrieves these variables in order and feeds the entire collection of input and converted image names (in that order) as a single command line argument into a Run Shell Script action. I divert the input images to one Zsh array, and the converted images to another Zsh array. Then I use a nested loop to compare the basename of input file with its converted counterpart. When I have a match, I then apply the touch command and assign the original creation and modification date of the input file to its corresponding converted image name. I have tested this with five image files from different Finder locations, and the converted files bear the original creation and modification dates. Tested on Mojave 10.14.6.


The Quick Action has become more complicated to achieve this goal.


Replace the content of the Run Shell Script action with the following code:

#!/bin/zsh

# Note: There is one command-line argument of input followed by converted filenames
# count of all input and output files
typeset -i max=$#
typeset -i idx=0
let "idx = $# / 2"
# temporary array of combined input and converted image names
typeset -a temp=($@)
# slice temp array to get file categories. Zsh arrays are 1 based.
typeset -a input_images=("${(@)temp[1,idx]}")
let "idx++"
typeset -a convert_images=("${(@)temp[idx,max]}")

# now perform nested array comparison
for i in "${(@)input_images}";
do
    for c in "${(@)convert_images}";
    do
        # compare input and converted basenames
        [[ ! $i:t:r == $c:t:r ]] && continue
            # names match so borrow creation/modication date
            # from input filename and apply to output file.
            /usr/bin/touch -r $i $c
            # get another input filename
            break
    done
done

# print appended listing of input and converted filenames if arrays are non-zero in size
{ [ "${input_images[@]}" -eq 0 ] || printf 'input_images = %s\n' "${(@)input_images}";} >> ~/Desktop/foo.txt
{ [ "${convert_images[@]}" -eq 0 ] || printf 'converted_images = %s\n' "${(@)convert_images}";} >> ~/Desktop/foo.txt

exit 0


Aug 6, 2019 2:18 PM in response to udawwgy

What Viking showed you is a shell command - you can add this via the 'Run Shell Script' action in Automator.


You will need to substitute the path of the original .heic and the new .jpg file in order for it to work. Without seeing more of your workflow it's impossible to be more specific - only you know where the relevant files are.

Aug 13, 2019 3:47 PM in response to udawwgy

When you pass that list of original image files and converted image file paths into the Run Shell Script, they are concatenated by spaces to form a single argument list ($@). On macOS Mojave 10.14.6, that argument list is a maximum of 262,144 characters. So yes, the number of filenames that you process will have variable filename lengths and that in turn, dictates a variable of how many files you can send to the Run Shell Script action before exceeding that ARG_MAX value.


Keep it simple with fewer filenames and you won't run up against that ARG_MAX (262,144) limit.


In the Terminal, you can get ARG_MAX:


getconf ARG_MAX


Can Automator convert an image AND keep the creation date?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.