------------------------------------------------------------------------ -- -- EXIF-touch V0.3 (C) exif-touch.info.skeeve@xoxy.net -- -- this program was first published on -- http://www.fischer-bayern.de/ -- -- You may use it without warranty under the terms of the GPL -- -- You can drop any combination of JPEG files and folders on this script -- or simply start it to select JPEG files. -- The file's creation date will then be change to the image's creation -- date, provided there is one in the EXIF tags. -- ------------------------------------------------------------------------ global ignoreMissingValue, ignoreWrongDate -- if run, you may choose jpeg files on run set imgAlias to choose file of type {"public.jpeg"} with multiple selections allowed without invisibles tell application "Image Events" to activate set ignoreMissingValue to false set ignoreWrongDate to false try -- when canceled err# -128 is returned EXIF_touch_list(imgAlias) on error errmsg number errn if errn /= -128 then display_error(errmsg, errn, false) end if end try tell application "Image Events" to quit end run -- you may drop any file or folder or combination thereof -- folders are handled recursively -- files with type identifier "public.jpeg" are handled -- all other files are simply ignored on open itemList tell application "Image Events" to activate set ignoreMissingValue to false set ignoreWrongDate to false try -- when canceled err# -128 is returned EXIF_touch_list(itemList) on error errmsg number errn if errn /= -128 then display_error(errmsg, errn, false) end if end try tell application "Image Events" to quit end open -- you may append this script to any folder -- it will work on all added items -- folders are handled recursively -- files with type identifier "public.jpeg" are handled -- all other files are simply ignored on adding folder items to this_folder after receiving these_items tell application "Image Events" to activate set ignoreMissingValue to false set ignoreWrongDate to false try -- when canceled err# -128 is returned EXIF_touch_list(these_items) on error errmsg number errn if errn /= -128 then display_error(errmsg, errn, false) end if end try tell application "Image Events" to quit end adding folder items to -- recursive handler to call EXIF_touch for image files on EXIF_touch_list(itemList) repeat with singleItem in itemList set information to info for singleItem as alias if folder of information then tell application "Finder" set x to (every file of singleItem) my EXIF_touch_list(x) -- all (image) files set x to (every folder of singleItem) my EXIF_touch_list(x) -- all folders end tell else if type identifier of information is "public.jpeg" then EXIF_touch(singleItem as alias) -- just one single (image) file end if end repeat end EXIF_touch_list -- does the touch on EXIF_touch(imgAlias) tell application "Image Events" set img to open imgAlias set allDates to value of every metadata tag of img whose name is "creation" close img end tell -- I'm not sure whether or not we have several creation dates for the image -- so I pick the oldest set creationDate to min(allDates) if creationDate is missing value then if not ignoreMissingValue then display_error("The image" & return & "«" & (imgAlias as string) & "»" & return & "Has no EXIF tags or at least no creation date", "No EXIF data", true) set ignoreMissingValue to true end if return end if -- check the creation date of the file tell application "Finder" to set fileCreation to creation date of imgAlias set fileCreation to EXIF_format(fileCreation) -- check today's date set today to EXIF_format(current date) -- all dates are in EXIF format, which is YYYY:MM:DD hh:mm:ss -- so we can easily compare if creationDate <= fileCreation then -- a simple touch works if we set the modified and access time -- to a date prior to file creation touch(creationDate, imgAlias) else if creationDate <= today then -- if the image was created before today, but after file creation -- (strange things might happen!) we refresh the image to -- today's date by copying it touch(creationDate, refresh(imgAlias)) else -- damn! Something is wrong! display_error("Your computer's date (" & today & ") is lower than the creation date (" & creationDate & ") of the image" & return & "«" & (imgAlias as string) & "»" & return & "Please try to fix this before you proceed.", "Wrong Date", true) set ignoreWrongDate to true end if end EXIF_touch -- make a cp (copy) of the file -- this sets the file's creation date to the current date on refresh(anAlias) set oldPath to POSIX path of anAlias if backup(anAlias) then do shell script "cp " & quoted form of POSIX path of anAlias & " " & quoted form of oldPath & " && rm " & quoted form of POSIX path of anAlias return (POSIX file oldPath) as alias end if return missing value end refresh -- renames the file -- appends " bck" to the filename (or " bck-#") if it already exists on backup(anAlias) tell application "Finder" set n to name of anAlias set e to name extension of anAlias if e is not "" then set n to text 1 thru (-2 - (length of e)) of n set e to "." & e end if set i to "" repeat try set name of anAlias to n & ".bck" & i & e exit repeat on error errmsg number errnr if errnr /= -48 then set errmsg to errmsg & return & return & "File: " & (anAlias as string) my display_error(errmsg, errnr, true) return false end if set i to i - 1 end try end repeat end tell return true end backup -- newDate has to be in EXIF format on touch(newDate, anAlias) if class of anAlias is not alias then return set x to words of newDate set x to ((items 1 thru -2 of x) as string) & "." & last item of x do shell script "touch -t " & x & " " & quoted form of POSIX path of anAlias end touch -- return the minimum of the list on min(aList) try set a to first item of aList on error return missing value end try repeat with b in rest of aList if a > b then set a to b end repeat return a end min -- format a date to EXIF format on EXIF_format(aDate) set {year:y, month:m, day:d, hours:hh, minutes:mm, seconds:ss} to aDate return (y & ¬ ":" & (leftfill of (m as integer) by "00") & ¬ ":" & (leftfill of d by "00") & ¬ " " & (leftfill of hh by "00") & ¬ ":" & (leftfill of mm by "00") & ¬ ":" & (leftfill of ss by "00")) as string end EXIF_format -- leftfill of 4711 by "000000" => "004711" to leftfill of aNumber by pattern set aNumber to aNumber as string set missing to (length of pattern) - (length of aNumber) if missing <= 0 then return aNumber return (text 1 thru missing of pattern) & aNumber end leftfill -- display the error on display_error(errmsg, errnr, ignore) set btns to {"OK"} if ignore then set btns to {"Ignore", "OK"} end if set errTitle to "Error" if errnr is not missing value then set errTitle to errTitle & " (" & errnr & ")" end if display dialog errmsg with title errTitle buttons btns default button "OK" with icon stop if button returned of result is "OK" then error number -128 end display_error