Occasionally, people here ask how they can get EXIF data from their images into DT (custom) metadata. This is possible without installing any third-party tools using a JavaScript function like the following:
ObjC.import('AppKit');
/* Get the value for EXIF 'key' from image in 'file' */
function getEXIFvalue(file, key) {
const img = $.NSImage.alloc.initWithContentsOfFile($(file));
const rep = img.representations.objectAtIndex(0);
const exifDict = rep.valueForProperty($.NSImageEXIFData);
const exifJS = exifDict.js; /* Convert NSDictionary to JavaScript array */
return exifJS[key].js; /* Return the value for the key as a JavaScript type */
}
You can call this function for example like this to set the creation date of images to the date/time they were taken:
const app = Application("DEVONthink 3")
const recs = app.selectedRecords();
recs.forEach(r => {
const p = r.path();
/* Get the date and time when the picture was taken */
const dateString = getEXIFvalue(p, 'DateTimeOriginal');
/* Convert the EXIF date format to ISO */
const ISODate = dateString.replace(/(\d{4}):(\d\d):(\d\d) /,"$1-$2-$3T");
/* Set the record's creation date to the date/time when the photo was taken */
r.creationDate = new Date(ISODate);
Beware: There is absolutely no error checking in these examples. So if you run the code on a PDF or on a JPG not containing the EXIF key/value ‘DateTimeOriginal’, it will fail miserably.