1

I am trying to incorporate an exiftool command into an Applescript routine that performs complex image manipulations. The following routine is aimed at modifying the creation dates and modification dates of the image files moved to a directory after manipulation.

I would like to use the classic exiftool command:

exiftool "-filemodifydate<datetimeoriginal" "-filecreatedate<datetimeoriginal" DIR

However the directory is named by the script with the command :

set pTargetFoldername to "Edited-pictures" & space & date string of (current date) & space & time of (current date) as string.

And therefore the directory name contains spaces.

My routine AdjustDates does not work. The resulting command does not compensate for the spaces and cannot be used in a "do shell script cmd" call The command created (with the dates in the local french language) is :

"/usr/local/bin/exiftool -filemodifydate<datetimeoriginal -filecreatedate<datetimeoriginal '/Users/yves/Desktop/Edited-Pictures mercredi 31 juillet 2024 70545'"

I do not seem to see a solution which should seem trivial.

on AdjustDates(pTargetFolderPath)
    tell application "Finder"
        set ThePath to (POSIX path of pTargetFolderPath)
    end tell
        set exiftool_path to "/usr/local/bin/exiftool"
        set exiftool_args to "-filemodifydate<datetimeoriginal" & space & "-filecreatedate<datetimeoriginal"
        set cmd to exiftool_path & space & exiftool_args & space & (quoted form of ThePath)
        return cmd --for test only
        --do shell script cmd -- not functional due to the spaces in the directory path
end AdjustDates
4
  • It isn't just the spaces, it is also the "<" characters in the command. From the documentation, "Note that quotes are required around this argument to prevent shell redirection since it contains a < symbol."
    – red_menace
    Commented Jul 31 at 21:27
  • Pointing out that the problem with spaces in the directory name is self-inflicted. Commented Jul 31 at 21:51
  • Can you provide the exact error? By the way, the exiftool command requires (as suggested by @red_menace) single quotes around the arguments, eg exiftool '-FileModifyDate<DateTimeOriginal' dir (example taken from man page).
    – Mockman
    Commented Aug 1 at 1:43
  • The command cmd is currently returned as : "/usr/local/bin/exiftool -filemodifydate<datetimeoriginal -filecreatedate<datetimeoriginal '/Users/yves/Desktop/Edited-Pictures jeudi 1 août 2024 34890'" Which obviously cannot be used in a do shell script. I agree that the spaces in the directory names should be removed, but I could not find a way. I need to create a number of separate directories identified this way or similar.
    – Yves
    Commented Aug 1 at 7:43

1 Answer 1

1

I have changed the command to :

set exiftool_args to quote & "-filemodifydate" & "<" & "datetimeoriginal" & quote & space & quote & "-filecreatedate" & "<" & "datetimeoriginal" & quote
    set cmd to exiftool_path & space & exiftool_args & space & (quoted form of ThePath)

This change did the work, in addition to eliminating the spaces from the target directory names by using a routine I found somewhere:

set pTargetFoldername to my ReplaceText(pTargetFoldername, " ", "-")

on ReplaceText(t, s, r)
    set tids to AppleScript's text item delimiters
    set AppleScript's text item delimiters to s
    try
        set temp_list to text items of t
        set AppleScript's text item delimiters to r
        set new_text to temp_list as string
        set AppleScript's text item delimiters to tids
    on error err_msg
        set AppleScript's text item delimiters to tids
        error err_msg
    end try
    return new_text
end ReplaceText
2
  • You don't necessarily need to remove spaces in the name (unless there is something that refuses to work with them), spaces and other special characters just need to be escaped or quoted if you don't want the shell to use them.
    – red_menace
    Commented Aug 1 at 12:55
  • Why are you inserting the spaces only to remove them again? Commented Aug 1 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .