0

By now, I can only get image file creation date which is several seconds later than capture date. How can I get image's capture date from Exif?

tell application "Finder"
    try
        set the source_folder to (folder of the front window) as text
    on error -- no open folder windows

        set the source_folder to (path to desktop folder) as text

    end try
    set these_items to the selection
end tell

set repeatCount to 1
repeat with i from 1 to the count of these_items

    set this_item to (item i of these_items) as alias
    set this_info to info for this_item
    set {file_name, file_ext} to splitExtension from the name of this_info
    set capture_date to (the creation date of this_info)

    set formatted_date to (my dateFormat(capture_date))
    set new_name to formatted_date & file_ext
    -- check to see if it's already there
    tell application "Finder"
        if (exists item (source_folder & new_name)) then
            --- display dialog new_name & " already exists."
            set new_name to formatted_date & "_" & repeatCount & file_ext
            set name of this_item to new_name
            set repeatCount to 1 + repeatCount
        else
            set name of this_item to new_name
        end if
    end tell
end repeat
jdleung
  • 1,088
  • 2
  • 10
  • 26

1 Answers1

1

You can get the EXIF data with help from AppleScriptObjC and AppKit.

The result is an Applescript record containing the data, the capture date is probably the value for key DateTimeDigitized which is a string.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "AppKit"

set theFile to (choose file)

set imageRep to current application's NSBitmapImageRep's imageRepWithContentsOfFile:(POSIX path of theFile)
set exifData to imageRep's valueForProperty:(current application's NSImageEXIFData)
if exifData is missing value then
    display dialog "No EXIF data found" buttons "Cancel" default button 1
else
    set exifData to exifData as record
    set captureDate to DateTimeDigitized of exifData
end if
vadian
  • 274,689
  • 30
  • 353
  • 361
  • There's also `DateTimeOriginal`, which might be the same as `DateTimeDigitized`. – l'L'l Aug 23 '18 at 15:18
  • @vadian Thanks. It works but the date format is something like this: 2018/08/17 13/12/04.JPG, how to get rid of the slash? Can I define the date format before getting it? – jdleung Aug 24 '18 at 02:36
  • @vadian Ooooh, the slash should be ":" which is allowed in file name, Mac shows it as "/" automatically. By now I replace it with `set AppleScript's text item delimiters to the "/"` – jdleung Aug 24 '18 at 02:59