scale and rename images

Hi,

I’m new to macscripter and I consider myself still new to AppleScript (never really studied it, but played around with lots of trial and error until my mac did what I intended to do :slight_smile:

I am working on a script to batch scale and rename images - and I am stuck somewhere along the way, so I would appreciate some help.

What I would like to end up with:
– a droplet to drop all images that should be processed

  • first dialog asks for the project title (rename file(s) if something is entered here, otherwise don’t)
  • second dialog asks for max size of image(s)
  • original files should be overwritten (just to keep it fast and simple)
  • renaming with exif data (JJMMDD) would be great, but i fear that’s not my league yet

global proj_name, maxSize

on open (itemList)
	
	tell application "System Events"
		activate
		
		-- PROJECT NAME
		set dialog1 to display dialog "enter project name" default answer "" with title "project name" with icon alias (((path to me) as string) & "Contents:Resources:droplet.icns")
		set proj_name to text returned of dialog1
		if proj_name = "" then set proj_name to "XXX"
		
		-- max size
		set dialog2 to display dialog "enter max size in px" with title "scale images" with icon 1 default answer "1600" buttons {"cancel", "don't scale", "scale"} default button "scale"
		set maxSize to text returned of dialog2 as real
		
	end tell
	
	-- scale images
	repeat with myfile in itemList
		tell application "Image Events"
			launch
			
			set allDates to (value of first metadata tag of img whose name is "creation")
			set myDate to allDates as text
			set myyear to (text 3 thru -16 of myDate) as string
			set mymonth to (text 6 thru -13 of myDate) as string
			set myday to (text 9 thru -10 of myDate) as string
			set myhour to (text 12 thru -7 of myDate) as string
			set myminute to (text 15 thru -4 of myDate) as string
			set mysec to (text 18 thru -1 of myDate) as string
			set new_Date to myyear & mymonth & myday & "-" & myhour & myminute & mysec
			
			if button returned of dialog2 is "scale" then
				set imageFile to open myfile
				set typ to imageFile's file type
				scale imageFile to size maxSize
				tell application "Finder"
					set file_name to proj_name & " " & new_Date
					set file_location to (container of myfile as string)
					set new_item to (file_location & file_name)
					save myfile in new_item as typ
				end tell
				
			end if
		end tell
	end repeat
	
	display dialog "done"
	
end open


on run
	
	display dialog "still testing"
	
end run

kip

Model: iMac
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 11

Hi kip. Welcome to MacScripter.

I think this modification to your script does what you want. Note that it changes the original files. It also assumes that the dropped items are image files and that they all have “creation” metadata. For safety, it would need checks to ensure that it only attempts to process items having these criteria.


-- global proj_name, maxSize

on open (itemList)
	
	activate
	
	-- PROJECT NAME
	set dialog1 to display dialog "enter project name" default answer "" with title "project name" with icon alias (((path to me) as string) & "Contents:Resources:droplet.icns")
	set proj_name to text returned of dialog1
	if proj_name = "" then set proj_name to "XXX"
	
	-- max size
	set dialog2 to display dialog "enter max size in px" with title "scale images" with icon 1 default answer "1600" buttons {"cancel", "don't scale", "scale"} default button "scale"
	set maxSize to text returned of dialog2 as integer
	set scaling to (button returned of dialog2 is "scale")
	
	tell application "Image Events" to launch
	
	-- scale images
	repeat with myfile in itemList
		tell application "Image Events"
			-- Open the file and get the image's "creation" metadata (assumed here to exist).
			set img to (open myfile)
			set myDate to value of metadata tag "creation" of img
			-- If scaling, scale the image and resave (to the original file here).
			if (scaling) then
				scale img to size maxSize
				save img
			end if
			-- Close the image.
			close img
		end tell
		
		-- Work out a new file name from the "creation" metadata and the file's existing extension
		tell application "System Events" to set extn to name extension of myfile
		set newName to replaceText(myDate, ":", "")
		set newName to replaceText(newName, space, "-")
		set newName to proj_name & space & newName & "." & extn
		-- Rename the file accordingly.
		tell application "System Events" to set myfile's name to newName
	end repeat
	
	-- Once all the files are processed, quit Image Events.
	tell application "Image Events" to quit
	
	display dialog "done"
	
end open

on replaceText(txt, find, replace)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set textItems to txt's text items
	set AppleScript's text item delimiters to replace
	set newText to textItems as text
	set AppleScript's text item delimiters to astid
	return newText
end replaceText

on run
	
	display dialog "still testing"
	open {choose file}
end run

Hi Nigel,

whow - excellent work! Thanks a lot for your assistance!
I have been trying and testing for some time before I joined the forum.
Now that’s something that works - and something to learn from :slight_smile:

I’ll try to add a try / error part that uses the creation date of the file, if there is no date in the metadata provided.

kip