EXIF tool: Get Image File's existing metadata as record

Three fast plain-AppleScript handlers to get image file’s existing metadata as record:


set metadataRecord to my getImageMetadataAsRecord_v1(POSIX path of (choose file of type "public.image"))
-- or
-- set metadataRecord to my getImageMetadataAsRecord_v2(POSIX path of (choose file of type "public.image"))
-- or
-- set metadataRecord to my getImageMetadataAsRecord_v3(POSIX path of (choose file of type "public.image"))


on getImageMetadataAsRecord_v1(posixPath)
	script o
		property exiftoolPath : "/usr/local/bin/exiftool"
	end script
	-- hack, provided by user @Mark Anthony in other topic
	({«class usrf»:(do shell script o's exiftoolPath & " -a -u " & quoted form of posixPath & " | tr -s ' ' | egrep -o '\\w[^:]+|:.+' | awk '{gsub(\" +$|: \",\"\"); print} ' ")'s paragraphs})'s contents as any
end getImageMetadataAsRecord_v1


on getImageMetadataAsRecord_v2(posixPath)
	script o
		property exiftoolPath : "/usr/local/bin/exiftool"
	end script
	set metadata to do shell script o's exiftoolPath & " -a -u " & quoted form of posixPath & " | tr -s ' '"
	-- JXA, provided by user @CK in other topic
	run script "let R=`" & metadata & "`" & "
.replace(/\\s*(\\w[^:]+) : (.+)\\x12?/g, '\"$1\" : \"$2\" \\u{0A}')
.split( '\\u{0A}' )
.slice( 0, -1 )
.join( ',' );
JSON.parse( '{' + R + '}' );" in "JavaScript"
end getImageMetadataAsRecord_v2


on getImageMetadataAsRecord_v3(posixPath)
	script o
		property exiftoolPath : "/usr/local/bin/exiftool"
		property metadata : ""
		property myRec : {}
	end script
	-- method, provided by user @robertfern (returns integer values in native form)
	set o's metadata to paragraphs of (do shell script o's exiftoolPath & " -a -u " & quoted form of posixPath & " | tr -s ' '")
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " : "}
	set o's myRec to {}
	repeat with i in o's metadata
		set tmp to text items of i
		set end of o's myRec to item 1 of tmp
		set end of o's myRec to (rest of tmp) as {integer, text} -- the @CK's suggestion applied here
	end repeat
	set AppleScript's text item delimiters to ATID
	return {«class usrf»:o's myRec}'s contents as any
end getImageMetadataAsRecord_v3

1 Like

I’ve written something similar for use in DEVONthink using JXA and not needing exiftool, so avoiding any third party dependencies.

The code is here: https://discourse.devontechnologies.com/t/script-getting-image-metadata-into-dt/72502#the-code-8

Most important is the function readMetadata, which takes a POSIX path and returns the metadata found in the different NSDictionaries and by calling Image Event methods.