Apple Script: How to write Photos Metadata to a file

by: 
Last modified: Oct 26, 2020 9:09 AM
8 1577 Last modified Oct 26, 2020 9:09 AM

Written for Photos 5 on Catalina:


This is just a fragment, to show how we can write the metadata of selected photos to a text file. The Photos have to be selected in the All photos view or a top level album (not a smart album or an album nested in a folder). Select the photo, then run the script from Script Editor by pressing the Run button, or wrap it into an Automator quick action or service.

Version 2: I changed the print format - the metadata are now separated by keywords: The filename follows the keyword "Filename"; the title follows the keyword "Title", etc. Also, I added more error tests.


Copy the code below into the Script Editor:

-------- snip ----------------------------------------------

(* Write Metadata tags to a file

Author: Leonie Dreschler-Fischer 

To run this script select some images in Photos,
then start the script by pressing the "Run Button",
or wrap it as a service and installit in ~/Library/Services and launch it from the Services menu in Photos.
When prompted, enter a filename for the text output
*)



(* Write EXIF info on all selected images to a file *)


local outputFile, item_count, im
local nam, w, h, dat, dims, nl, thisline, file_name, thekeywords
local res

set outputFile to choose file name with prompt "Output File name"

set savedTextItemDelimiters to AppleScript's text item delimiters

try
	open for access outputFile with write permission
end try

set AppleScript's text item delimiters to {"."}


set tabu to ", 	" -- for formatting
set nl to "
"



tell application "Photos"
	try
		set imageSel to (get selection)
	on error errTexttwo number errNumtwo
		display dialog "Cannot get the selection: " & errNumtwo & errTexttwo
	end try
	
	if imageSel is {} then error "Please select an image."
	set the item_count to count of imageSel
	
	
	
	
	repeat with i from 1 to the item_count
		set im to item i of imageSel
		tell application "Photos"
			-- defaukt values, if data cannot be read
			set file_name to "missing-filename" -- default, if it cannot be read	
			set type_media to "missing-extension"
			set nam to "missing-title"
			set dims to "missing-width, missing height"
			set dat to "missing-date"
			
			set thisline to ""
			(* filename get media type, mov or else *)
			try
				tell im
					set file_name to its filename
				end tell
			on error errTexttwo number errNumtwo
				display dialog "Cannot get the filename " & errNumtwo & errTexttwo
			end try
			
			set AppleScript's text item delimiters to {"."}
			set type_media to (get last text item of file_name)
			set AppleScript's text item delimiters to savedTextItemDelimiters
			
			
			
			
			try -- title
				tell im
					set nam to its name as string --(get the name of im as string)						
				end tell
			on error errTexttwo number errNumtwo
				display dialog "Cannot get the title " & errNumtwo & errTexttwo
			end try
			--display dialog nam buttons ¬
			--	"OK" with icon caution default button "OK"
			
			
			
			
			try -- dimensions
				tell im
					set w to its width as string
					set h to its height as string
					set dims to w & "x" & h
				end tell
			on error errTexttwo number errNumtwo
				display dialog "Cannot get the dimensions " & errNumtwo & errTexttwo
			end try
			
			
			try
				tell im
					set dat to (its date as string)
					
				end tell
			on error errTexttwo number errNumtwo
				display dialog "Cannot get the date: " & errNumtwo & errTexttwo
				
			end try
			
			
			try
				set AppleScript's text item delimiters to {" "}
				tell im
					set thekeywords to its keywords as text
				end tell
			on error errTexttwo number errNumtwo
				display dialog "Cannot get the keywords: " & errNumtwo & errTexttwo
			end try
			set AppleScript's text item delimiters to savedTextItemDelimiters
			
			set thisline to "Filename: " & file_name & tabu & " Type: " & type_media & tabu & " Title: " & nam & tabu & " dimensions:" & dims & tabu & "Date:" & dat & tabu & "Keywords: " & thekeywords & nl & nl
			write thisline to outputFile
			
		end tell -- Photos
		
	end repeat
	
	write nl & "Done -- written " & item_count & " items" & nl to outputFile
	
	close access outputFile
	
end tell

tell application "TextEdit"
	activate
	open outputFile
end tell

set AppleScript's text item delimiters to savedTextItemDelimiters
return outputFile


Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.