Add text to name and save to web?

I’m fairly new to AppleScript but, with the help of a few other folk I’ve succeeded in writing a script which works to:

Choose a folder full of photos
Make 2 new folders within that folder
Take the photos and copy them into the 2 new folders
Perform a Photoshop action on all files in each of the folder

So, I end up with 2 new versions of all the photos and also keep the originals.

This is the script:
set original_folder to (choose folder with prompt “Choose Folder containing the files”)
tell application “Finder”
if not (exists folder “Webpics” of original_folder) then make new folder at original_folder with properties {name:“Webpics”}
if not (exists folder “Webthumbs” of original_folder) then make new folder at original_folder with properties {name:“Webthumbs”}
set copies_folder to folder “Webpics” of original_folder
set copies2_folder to folder “Webthumbs” of original_folder
duplicate every file of original_folder to copies_folder
set the_files to every file of copies_folder
repeat with each_file in the_files
set each_file to each_file as alias
my do_photoshop_stuff_to(each_file)
end repeat
duplicate every file of copies_folder to copies2_folder
set webthumbs_folder to folder “Webthumbs” of original_folder
set the_files to every file of webthumbs_folder
repeat with each_file in the_files
set each_file to each_file as alias
my do_photoshop_stuff2_to(each_file)
end repeat
end tell

on do_photoshop_stuff_to(this_file)
tell application “Adobe Photoshop CS”
open this_file
do action “Masterpic to Webpic” from “Set 1.atn”
close current document saving yes
end tell
end do_photoshop_stuff_to

on do_photoshop_stuff2_to(this_file)
tell application “Adobe Photoshop CS”
open this_file
do action “Webpic to Webthumb” from “Set 1.atn”
close current document saving yes
end tell
end do_photoshop_stuff2_to

There are 2 extra things which I’ve been trying to add but seem to be failing…

1: I’d like to be able to add the letter w to the end of the file name of all of the photos which end up in the webpics folder and the letter t to the end of all the photos which end up in the webthumbs folder.

2: I’d also like to “save for web” the photos in both the webpics and webthumbs folders. I’ve currently got an ImageReady droplet which I use but I’d love to be able to script this in too.

I’ve searched through all the renaming posts and “save for web” posts on the forum here and tried versions of all of them but can’t get any of them to work…I’ve also tried using the script together with Automator but again haven’t quite got there.

So, ultimately I’d end up with something that starts with:

Choose Folder (e.g. “XYZ” containing e.g. file123.jpg)

and ends up with:
Folder “XYZ”
containing original file123.jpg
and containing folder “webpics” with optimised file123w.jpg (which has been through my Photoshop action and saved for web)
and also containing folder “webthumbs” with optimised file123wt.jpg (which has been through my other Photoshop action and saved for web)

Hope this all makes sense. I’ve spent the day trying to sort it out myself but I just don’t know enough about Scripting so if anyone out there can help I’d be hugely grateful. I’m on OS 10.4.4 and Photoshop CS.

Cheers, Eddy

Eddy whats involved in your actions “Masterpic to Webpic” & “Webpic to Webthumb” only I am working up some photoshop scripts for similar stuff. Instead of duplicating the files mine goes like this:

finder to photoshop for the repete loop
do photoshop stuff
Save as with copying to here? as “file name” & “XXX”
do more photoshop stuff
Save as with copying to here? as “file name” & “ZZZ”

The with copying option will create all the duplication for you with renaming. This depends on what’s in the actions though in my case it’s just 3 different resizes after saving the original as layered .psd.

Hi Mark,

My 2 actions resize the image and add a watermark.

I’ve tried your way but can’t seem to get it to work. I have to admit that I’m not exactly an expert on scripting so I’m probably gettting the wording wrong.

Does your script save the copy of the file back into its original folder?

Cheers, Eddy

Here is what code I have so far. This script will prompt you to pick a folder of images. Then it will create a folder at your desktop within that 4 sub folders for the different file sizes required plus the layered file used to create the final images from leaving the original files where they were. You should be able to remove some of this and be left with what you want. The majority of this is to handle the scaling a clipped image to best fit the final image on a white background or crop an unclipped image to fit trimmed around centre.

set inputFolder to choose folder

tell application "Finder"
	set filesList to files in inputFolder
	set MyWebImages to make new folder at desktop with properties {name:"Mark's Web Images"}
	set current view of container window of result to list view
	set MyWeb1200 to make new folder at MyWebImages with properties {name:"1200x1200 Layered Images"}
	set MyWeb400 to make new folder at MyWebImages with properties {name:"400x400 Images"}
	set MyWeb200 to make new folder at MyWebImages with properties {name:"200x200 Images"}
	set MyWeb100 to make new folder at MyWebImages with properties {name:"100x100 Images"}
end tell

tell application "Adobe Photoshop CS"
	set display dialogs to never
end tell

repeat with aFile in filesList
	
	set fileIndex to 0
	
	tell application "Finder"
		set theFile to aFile as alias
		set theFileName to name of theFile
	end tell
	
	tell application "Adobe Photoshop CS"
		activate
		
		set ruler units of settings to pixel units
		
		open theFile
		
		set docRef to the current document
		set docName to name of docRef
		set docBaseName to getBaseName(docName) of me
		
		tell the current document
			
			if (mode of docRef is not RGB) then
				change mode docRef to RGB
			end if
			if (bits per channel of docRef is sixteen) then
				set bits per channel of docRef to eight
			end if
			
			delete (every channel whose kind is not component channel)
			
			delete (every path item whose kind is not clipping)
			
			if (count of art layers) > 1 or (count of layer sets) > 0 or ((count of art layers) is 1 and not background layer of layer 1) then flatten
			
			if exists (path items whose kind is clipping) then
				set thepath to (name of every path item whose kind is clipping)
				set thepath to item 1 of thepath
				
				set background layer of layer 1 to false
				
				tell path item thepath
					create selection feather amount 0 with antialiasing
				end tell
				invert selection
				set current layer to layer "layer 0" of docRef
				clear
				trim basing trim on top left pixel
				set docHeight to height of docRef
				set docWidth to width of docRef
				if (docHeight > docWidth) then resize image height 1140 resolution 72
				if (docWidth > docHeight) then resize image width 1140 resolution 72
				resize canvas width 1200 height 1200
				adjust layer "layer 0" using automatic levels
				adjust layer "layer 0" using automatic contrast
				filter current layer using unsharp mask with options {amount:130, radius:3, threshold:10}
				make new art layer at end of docRef with properties {name:"Background Colour", blend mode:normal}
				set current layer to layer "Background Colour" of docRef
				select all
				fill selection with contents {class:RGB color, red:255, green:255, blue:255}
				delete every path item
			else
				set docHeight to height of docRef
				set docWidth to width of docRef
				if (docHeight > docWidth) then resize image width 1200 resolution 72
				if (docWidth > docHeight) then resize image height 1200 resolution 72
				adjust layer "Background" using automatic levels
				adjust layer "Background" using automatic contrast
				filter layer "Background" using unsharp mask with options {amount:130, radius:3, threshold:10}
				resize canvas width 1200 height 1200
			end if
			
			set newFileName to (MyWeb1200 as string) & docBaseName & "_1200"
			
			save docRef in file newFileName as Photoshop format with options {save layers:true} appending lowercase extension with copying
			
			resize image height 400 resolution 72 resample method bicubic sharper
			
			set newFileName to (MyWeb400 as string) & docBaseName & "_400"
			
			save docRef in file newFileName as JPEG with options {quality:9} appending lowercase extension with copying
			
			resize image height 200 resolution 72 resample method bicubic sharper
			
			set newFileName to (MyWeb200 as string) & docBaseName & "_200"
			save docRef in file newFileName as JPEG with options {quality:9} appending lowercase extension with copying
			
			resize image height 100 resolution 72 resample method bicubic sharper
			
			set newFileName to (MyWeb100 as string) & docBaseName & "_100"
			save docRef in file newFileName as JPEG with options {quality:9} appending lowercase extension with copying
		end tell
		close current document without saving
	end tell
end repeat


-- Returns the document name without extension (if present)
on getBaseName(fName)
	set baseName to fName
	repeat with idx from 1 to (length of fName)
		if (item idx of fName = ".") then
			set baseName to (items 1 thru (idx - 1) of fName) as string
			exit repeat
		end if
	end repeat
	return baseName
end getBaseName

Hi Eddy,

If you want to add the “w” or “t”, then somehting like this works:

set the_file to choose file – the jpeg, extension .jpg
set the_name to name of (info for the_file)
if the_name ends with “.jpg” then
set the_name to (text 1 thru -5 of the_name) & “w” & “.jpg”
end if
return the_name

So in your script, in this seciton:

repeat with each_file in the_files
set each_file to each_file as alias
my do_photoshop_stuff_to(each_file)
– add here “w” or “t”
end repeat

add:

set the_name to name of (info for each_file)
if the_name ends with “.jpg” then
set the_name to (text 1 thru -5 of the_name) & “w” & “.jpg”
end if
set name of each_file to the_name

Note that you can’t use that last statement alone and the target must be the Finder. Your script has everything wrapped in the tell Finder block so it’s ok. Remember to change “w” to “t” in the other repeat loop.

gl,