Photoshop CS6 - Metadata read and use to rename images

Hi All,

Working on a script to extract the creation date from Metadata of an image and photoshop then save the image with the creation date in the name.

-- Script to rename files based on creation date and time. 

tell application "Adobe Photoshop CS6"
	set myimage to document 1
	tell myimage
		tell info
			set rawimagecreated_date to creation date as text
			set myimagecreated_date to rawimagecreated_date as text
			-- return myimagecreated_date
		end tell
		set rawimagename to the name of myimage as text
		set myimagename to rawimagename as text
		display dialog rawimagename as text
		-- display dialog myimagecreated_date as text
		NewName to (myimagecreated_date + myimagename) as integer
		display dialog NewName
	end tell
	set savedir to "Data:Holidays:Photoshop scripting:Resaved images:" as alias
	save myimage in (savedir & NewName) as JPEG
end tell

When running the script I get an error:
error “Can’t make "IMG_7125.jpg" into type number.” number -1700 from “IMG_7125.jpg” to number

I thought the script had coerced the date to text and this shouldn’t be an issue.

Plan is to set this to a ‘repeat with’ script when I’ve got the functionality sorted to process a bunch of images in a folder.

Original file name : IGM_1234.jpg
Resaved file name : 20230624_IMG1234.jpg

Warm Regards,

Matt M.
Macbook Pro 2013
Mac OS 10.14
Applescript 2.7
Adobe CS6 suite

Found I could get this to work as required by getting the “Finder” to concatenate the original image name and the creation date in together

Final script as follows:

-- Set folders for source images and save directory

tell application "Finder"
	set myfolder to (choose folder) as alias
	set mysavedfolder to (choose folder) as alias
	set myfiles to (every item of folder myfolder) as alias list
end tell

repeat with aFile in myfiles
	tell application "Finder"
		set aFileName to "" as string
		set aFileName to (name of aFile) as string
		-- display dialog aFileName giving up after 2
	end tell
	-- Get creation date from image
	tell application "Adobe Photoshop CS6"
		open aFile
		tell document 1
			tell info
				set rawimagecreated_date to creation date as string
			end tell
		end tell
	end tell
	-- Concatenate filename and creation date
	tell application "Finder"
		-- display dialog mysavedfolder giving up after 1
		-- display dialog rawimagecreated_date giving up after 1
		-- display dialog aFileName giving up after 1
		set NewFilePath to (mysavedfolder & rawimagecreated_date & " " & aFileName) as string
		-- display dialog NewFilePath giving up after 1
	end tell
	tell application "Adobe Photoshop CS6"
		save document 1 in file (NewFilePath) as JPEG
		close document 1
	end tell
end repeat

Probably there’s a lot cleaner way to achieve this outcome (happy for suggestions) but this worked for me.

If you have the time and inclination to work out the syntax, ExifTool by Phil Harvey is a powerful command-line utility that will do all the above without the overhead of opening, saving and closing every single image in Photoshop. The link above is to the section of Phil’s site that describes renaming and moving files.

ExifTool Terminal commands can be incorporated into AppleScript using “do shell script”.

You don’t need Photoshop to achieve this kind of task.
Here is a solution using AppleScriptObjC:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set sourceFolder to (choose folder)
if sourceFolder = false then return -- abort if user cancels

-- build folders urls
set sourceFolder to (POSIX path of sourceFolder)
set targetFolder to (text 1 thru -2 of sourceFolder) & "-dated"
set sourceURL to current application's NSURL's fileURLWithPath:(POSIX path of sourceFolder)
set targetURL to current application's NSURL's fileURLWithPath:(POSIX path of targetFolder)

-- create destination folder only if it does not exists
set theFileManager to current application's NSFileManager's |defaultManager|()
set {theResult, theError} to theFileManager's createDirectoryAtURL:targetURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
if not (theResult as boolean) then my displayError:theError

-- get all files in source folder
set allURLs to theFileManager's contentsOfDirectoryAtURL:sourceURL includingPropertiesForKeys:{} options:6 |error|:(missing value)

repeat with anURL in allURLs
	set theMDItem to (current application's NSMetadataItem's alloc()'s initWithURL:anURL) -- get metadata
	if ((theMDItem's valueForAttribute:"kMDItemContentTypeTree")'s containsObject:"public.image") then -- consider only image files
		-- get image creation date (same as exif)
		set aDate to (theMDItem's valueForAttribute:"kMDItemContentCreationDate")
		-- format the date
		set dateFormat to current application's NSDateFormatter's new()
		(dateFormat's setDateFormat:"yyyyMMdd")
		set aDate to (dateFormat's stringFromDate:aDate) as text
		-- build destination url
		set aName to anURL's lastPathComponent()
		set aTarget to (targetURL's URLByAppendingPathComponent:("" & aDate & "_" & aName))
		-- delete previous file 
		if (aTarget's checkResourceIsReachableAndReturnError:(missing value)) then (theFileManager's removeItemAtURL:aTarget |error|:(missing value))
		-- copy & rename file
		(theFileManager's copyItemAtURL:anURL toURL:aTarget |error|:(missing value))
	end if
end repeat
1 Like