Image File Creation Date

I haven’t encountered and can’t reproduce that, but I can certainly understand how it might occur. Perhaps a simple try statement would be a better approach.

use framework "Foundation"
use scripting additions


set theFile to POSIX path of (choose file of type {"public.image"})
set theDate to getDate(theFile)

on getDate(theFile) -- theFile requires POSIX path
	set theFile to current application's |NSURL|'s fileURLWithPath:theFile
	set theMetadata to (current application's NSMetadataItem's alloc()'s initWithURL:theFile)
	try
		set theDate to (theMetadata's valueForAttribute:"kMDItemContentCreationDate")
		set dateFormatter to current application's NSDateFormatter's new()
		dateFormatter's setDateFormat:"yyyy:MM:dd' 'HH:mm:ss" -- user edit as desired
		return ((dateFormatter's stringFromDate:theDate) as text)
	on error
		return "Spotlight metadata creation date not found"
	end try
end getDate

BTW, the info for command only appears to return the file system date, which may or may not be what is desired.

FWIW, the IPTC creation date and time can be obtained with the following:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set POSIXPath to POSIX path of (choose file)
set theImageRep to current application's NSBitmapImageRep's imageRepWithContentsOfFile:POSIXPath
set theIPTCData to theImageRep's valueForProperty:(current application's NSImageIPTCData)
try
	set theDate to theIPTCData's valueForKey:"DateCreated"
	set theTime to theIPTCData's valueForKey:"TimeCreated"
on error
	return "IPTC date and time data not found"
end try

For additional information on this topic see the thread here

A test image with IPTC metadata can be found here

I thought it might be worthwhile in certain instances to convert Exif date/time metadata to an NSDate, but this is not easily done. The reason is that Exif metadata does not record time zones and instead simply obtains the date and time from the camera (or other device).

In contrast, the IPTC metadata does include time zone information and thus allows the direct creation of an NSDate. The following is an example from Shane:

use framework "Foundation"
use scripting additions

set theDate to "20200108"
set theTime to "133001+0100"
set dateObject to getDate(theDate, theTime) --> (NSDate) "2020-01-08 12:30:01 +0000"

on getDate(theDate, theTime)
	set fullDate to theDate & theTime
	set theFormatter to current application's NSDateFormatter's new()
	theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
	theFormatter's setTimeZone:(current application's NSTimeZone's timeZoneWithAbbreviation:"GMT")
	theFormatter's setDateFormat:"yyyyMMddHHmmssZZZ"
	return theFormatter's dateFromString:fullDate
end getDate

Shane’s script can be modified to create an NSDate from Exif date/time metadata, but this requires the answers to two questions:

  • was the camera set to the correct date/time in the time zone where the picture was taken; and

  • what was the time zone where the photo was taken.

With the above, an NSDate can be gotten with the following. It assumes no daylight savings time.

use framework "Foundation"
use scripting additions

set theFile to POSIX path of (choose file of type {"public.jpeg"})
set exifDate to getExifDate(theFile)
set nsDate to getNSDate(exifDate)

on getExifDate(theFile)
	set theFile to current application's |NSURL|'s fileURLWithPath:theFile
	set imageRep to current application's NSBitmapImageRep's imageRepWithContentsOfURL:theFile
	set dateString to (imageRep's valueForProperty:(current application's NSImageEXIFData))'s valueForKey:"DateTimeOriginal"
	return dateString as text
end getExifDate

on getNSDate(exifDate)
	set theFormatter to current application's NSDateFormatter's new()
	theFormatter's setTimeZone:(current application's NSTimeZone's timeZoneWithAbbreviation:"MT")
	theFormatter's setDateFormat:"yyyy:MM:dd HH:mm:ss"
	return theFormatter's dateFromString:exifDate
end getNSDate

BTW, the above script is intended for learning purposes only–not for actual use.