Because of changes Apple made to Photos for Mac with Catalina, I have to do some additional scripting work with scans of photos to add to Photos.
What I need help with is how to use Applescript to pull info out of some specific EXIF fields for the photo.
I know from doing some searching on the net that I can run Applescript code something like this to put info INTO the EXIF fields:
do shell script "/usr/local/bin/exiftool -IPTC:DateCreated='" & NewDate & "' -IPTC:TimeCreated='" & NewTime & "' -Overwrite_Original " & quoted form of (POSIX path of your_file)
where NewDate is “YYYY:MM:DD” and NewTime is “HH:MM:SS-06:00” (the last being my time zone adjustment from GMT)
Getting info out of EXIFTOOL is unclear to me. If I run these three commands in Terminal, I get this output:
exiftool -usercomment d.jpg
User Comment : This is a custom title.
exiftool -datecreated d.jpg
Date Created : 2019:12:31
exiftool -timecreated d.jpg
Time Created : 10:00:00-06:00
What would be the appropriate Applescript commands to get the info past the colon into an Applescript variable on which to do further processing?
set aFile to quoted form of POSIX path of (choose file)
set theDatas to (do shell script "/usr/local/bin/exiftool -a -u -g1 " & aFile)
(*
"---- ExifTool ----
ExifTool Version Number : 11.71
---- System ----
File Name : flyer_1.jpg
Directory : /Users/**********/Desktop
File Size : 741 kB
File Modification Date/Time : 2019:09:16 16:11:08+02:00
File Access Date/Time : 2019:11:25 19:26:24+01:00 <-- ask for this one
File Inode Change Date/Time : 2019:10:26 19:36:22+02:00
File Permissions : rw-r--r--
---- File ----
File Type : JPEG
File Type Extension : jpg
MIME Type : image/jpeg
Exif Byte Order : Big-endian (Motorola, MM)
Image Width : 1213
Image Height : 857
Encoding Process : Baseline DCT, Huffman coding
Bits Per Sample : 8
Color Components : 3
Y Cb Cr Sub Sampling : YCbCr4:4:4 (1 1)
---- JFIF ----
JFIF Version : 1.01
Resolution Unit : None
X Resolution : 1
Y Resolution : 1
---- ExifIFD ----
Exif Image Width : 1213
Exif Image Height : 857
---- Composite ----
Image Size : 1213x857
Megapixels : 1.0"
*)
set theParas to paragraphs of theDatas
set wanted to "File Access Date/Time"
repeat with aPara in theParas
if aPara starts with wanted then
set theValue to my recolle(items 2 thru -1 of my decoupe(aPara, ": "), ": ")
exit repeat
end if
end repeat
return theValue --> "2019:11:25 19:28:54+01:00"
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 novembre 2019 19:33:54
Thanks, I can see how to use your example to grab the information I need from the photo, using the EXIFTOOL routine.
My next question is less specific about EXIFTOOL and more a general Applescript question: I would like to do this with not just a file that I select in the Finder when this executes, but would rather select a range of files (or if easier, a set of files in a folder), and have it recursively execute code on each photo. I will be taking information from the filename, coding it as a date, and then putting it into the EXIF info for that file, and then taking the title from the filename and putting that into the UserComment EXIF field for later use, extracting that title from the photo (hopefully) within Photos, and replacing the blank Title with that correct title.
I have already learned how to do it recursively within Photos, but am unclear how to do it in the Finder.
I would like to run EXIFTOOL on a photo from within Photos for Mac. But I now realize that in order to run that tool, I have to give EXIFTOOL a filename. But how do I find the file location for a photo in this program? It’s hidden, more or less, in the Photos Library container.
set picturesFolder to (path to pictures folder) as string
set photosLibrary to alias (picturesFolder & "Photos Library.photoslibrary")
tell application "Finder"
set masterFolder to folder "Masters" of photosLibrary
set thePhotos to (every file of entire contents of masterFolder) as alias list
end tell
set exifDataList to {}
repeat with aPhoto in thePhotos
set aPhotoPath to quoted form of (POSIX path of aPhoto)
set fileAccessDate to (do shell script "/usr/local/bin/exiftool -s3 -FileAccessDate " & aPhotoPath)
set end of exifDataList to fileAccessDate
end repeat
return exifDataList
Just added the option ‘s’.
-s[NUM] (-short) Short output format
So this should work:
exiftool -s3 -fileType d.jpg
I use my AppleScript-Handler:
set xmp_subjectCode to my exiftool_executeCommand(filePath as string, "-s3 -XMP:SubjectCode")
on exiftool_executeCommand(theFile, theCommand)
try
return do shell script "/usr/local/bin/exiftool -overwrite_original_in_place " & theCommand & " " & quoted form of (POSIX path of (theFile as text))
on error
return false
end try
end exiftool_executeCommand
This question was never specifically answered. The thread has moved beyond this point including the very helpful -s options but, FWIW, I thought I would post an answer by way of a commented example.
set theImage to POSIX path of (choose file) -- select file
-- set variable to "Create Date" tag and date/time using exiftool
set createDate to do shell script "/usr/local/bin/exiftool -createdate " & quoted form of theImage
set TID to text item delimiters -- save existing text item delimiters
set text item delimiters to {": "} -- set text item delimiter to colon and space
set createDate to text item 2 of createDate -- set variable to date/time
set text item delimiters to {" "} -- set text item delimiter to a space
set theDate to text item 1 of createDate -- set variable to the date
set theTime to text item 2 of createDate -- set variable to the time
set text item delimiters to TID -- set text item delimiters to original value
-- Note: use "AppleScript's text item delimiters" in a tell block
The following script uses the ExifTool utility to get metadata from an image file. This metadata is then written to a text file on the desktop. To test this script, verify that the path to ExifTool is correct, then open the script in a script editor and run.
-- revised 2023.08.04
use framework "Foundation"
use scripting additions
set sourceFile to POSIX path of (choose file of type {"public.image"})
set targetFile to (POSIX path of (path to desktop)) & "Exif Metadata.txt"
try
set exifData to (do shell script "/usr/local/bin/exiftool -t -sort " & quoted form of sourceFile)
on error
display dialog "The selected file could not be read by ExifTool." buttons {"OK"} cancel button 1 default button 1
end try
set theString to current application's NSString's stringWithString:("Selected File:" & linefeed & sourceFile & linefeed & linefeed)
set theString to theString's stringByAppendingString:exifData
set theString to theString's stringByReplacingOccurrencesOfString:(tab & return) withString:(tab & "Not Available" & return)
set theString to theString's stringByReplacingOccurrencesOfString:return withString:(linefeed & linefeed)
set theString to theString's stringByReplacingOccurrencesOfString:tab withString:(":" & linefeed) -- edit as desired
theString's writeToFile:targetFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)