Tuesday, September 27, 2022

#1 2022-08-15 09:47:55 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

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

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

Applescript:


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

Last edited by KniazidisR (2022-08-17 06:31:31 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)