multiple files to be Saved with different name

Hi,

I want to open these two files in Photoshop together, perform a task and then re-save them as different filename, keeping the first part intact.

Original files:
PP Native Cover_3151238_Back Cover.jpg
PP Native Cover_3151238_Front Cover.jpg

: where the first part of the filename before the “Back Cover” and “Front Cover” is not the same always.
i.e. “PP Native Cover_3151238”, it could be any number or name.

Target filename:
PP Native Cover_3151238_Kindle Back Cover.jpg
PP Native Cover_3151238_Kindle Front Cover.jpg

How can I insert the word “Kindle” in between the filename while saving from photoshop, or
changing the “Back Cover” with “Kindle Back Cover” and “Front Cover” with “Kindle Front Cover” respectively.

The Back cover should be renamed as “Kindle Back Cover” and;
the Front cover should be renamed as “Kindle Front Cover”

for your reference; these below commands will needs to be executed on both the files in Photoshop CS6

Please advise.

It seems that my question was very hard to achieve.

Can anyone tell me to rename a file by inserting a new word in the middle in the existing filename. For example:

“PP Native Cover_3151238_Back Cover.jpg” will become “PP Native Cover_3151238_Kindle Back Cover.jpg”
“PP Native Cover_3151238_Front Cover.jpg” will become “PP Native Cover_3151238_Kinle Front Cover.jpg”

The beginning and end part will remain the same, only the word “Kindle” needs to be inserted.

Though I’m not a script writer, but I’ll do some hit and trial if got some clue.

I appreciate your suggestions.

The following script will only select files with format “PP Native Cover_nnnnnnn_Back Cover.jpg” and “PP Native Cover_nnnnnnn_Front Cover.jpg” where nnnnnnn can be any number and then rename them. Other files in the chosen folder will be unharmed.

set theFolder to POSIX path of (choose folder)

set theFiles to paragraphs of (do shell script "ls " & quoted form of theFolder & " | egrep -i '^PP Native Cover_[0-9]{7}_(Back|Front) Cover.jpg$'")

repeat with thisFile in theFiles
	tell contents of thisFile to set newName to text 1 thru 24 & "Kindle " & text 25 thru -1
	set oldFilePath to quoted form of (theFolder & contents of thisFile)
	set newFilePath to quoted form of (theFolder & newName)
	do shell script "mv " & oldFilePath & space & newFilePath
end repeat

Thanks Bazzie but the text before: “_Back Cover.jpg” and “_Front Cover.jpg” is not static. It could have numbers, characters or a combination of all these. It could be anything and of any length.

So what I have is the last part of the filename, which is common everytime.

“xxxxxxxxx xxx_xxxxx xxxxx_Back Cover.jpg”

“xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxx_Front Cover.jpg”

set theFolder to POSIX path of (choose folder)

set theFiles to paragraphs of (do shell script "ls " & quoted form of theFolder & " | egrep -i '.*_(Back|Front) Cover.jpg$' || true")

repeat with thisFile in theFiles
	if contents of thisFile ends with "Back Cover.jpg" then
		tell contents of thisFile to set newName to text 1 thru -15 & "Kindle " & text -14 thru -1
	else -- file ends with "Front Cover.jpg"
		tell contents of thisFile to set newName to text 1 thru -16 & "Kindle " & text -15 thru -1
	end if
	set oldFilePath to quoted form of (theFolder & contents of thisFile)
	set newFilePath to quoted form of (theFolder & newName)
	do shell script "mv " & oldFilePath & space & newFilePath
end repeat

Hi Bazzie,

Thanks for the suggestions. They perfectly rename the files however, it seems that I failed to incorporate the script to accomplish the task I want to. I am not a script writer but tries to manipulate the things as per my requirement.

Here is the scenario:

There are two JPGs in a folder as mentioned earlier. Now I would like to open those two JPGs in Adobe Photoshop CS6/CC, perform a task and then Save-As with a new name with Kindle in between the filename:

Let say the script would be something like:

  1. Open both the files using drag & drop on the AppleScript Droplet or using the “set theLocation to (target of front window)”

  2. Perform a common photoshop task on each file one by one i.e.

     tell application "Adobe Photoshop CS6"
     	change mode to RGB
     	resize image height 1000 as pixels
     	save oldFile in file NewFile as JPEG with options jpgOptions with copying
     end tell
    
  3. save the Back cover file as “xxxxxxxxxxx_Kindle Back Cover.jpg”

  4. save the Front cover file as “xxxxxxxxxxx_Kindle Front Cover.jpg”

Given below is my old script which I successfully altered to create two different versions for the Kindle. But rather than repeating the process, I would like to create the two Kindle versions from the existing JPGs.

This script uses a PDF to perform a task (altered to create Kindle versions):


on open theItems
	-- set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	repeat with thefile in theItems
		tell application "System Events"
			set {name extension:fileExtension, container:parentFolder} to thefile
			set parentFolder to path of parentFolder
		end tell
		if fileExtension is "pdf" then
			set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
			set pageWidth to pageWidth as real
			
			tell application "Adobe Photoshop CS6"
				set display dialogs to never
				set myOptions to {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
				set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
				
				open file (thefile as text) as PDF with options myOptions
				set mystate to current history state of current document
				
				set docRef to current document
				set docName to name of docRef
				
				tell application "Finder"
					set JpgFolderName to "Hi-Res JPEG_" & docName
					set JpgFolder to (make new folder at parentFolder with properties {name:JpgFolderName})
					
					set BackC to (JpgFolder as string) & docName & "_Back Cover"
					set FrontC to (JpgFolder as string) & docName & "_Front Cover"
					set KindleBackC to (JpgFolder as string) & docName & "_Kindle Back Cover"
					set KindleFrontC to (JpgFolder as string) & docName & "_Kindle Front Cover"
				end tell
				
				resize canvas document 1 width pageWidth as inches anchor position middle left
				save docRef in file BackC as JPEG with options jpgOptions with copying
				tell current document
					change mode to RGB
					resize image height 1000 as pixels
					save docRef in file KindleBackC as JPEG with options jpgOptions with copying
				end tell
				set current history state of current document to mystate
				
				resize canvas document 1 width pageWidth as inches anchor position middle right
				save docRef in file FrontC as JPEG with options jpgOptions with copying
				tell current document
					change mode to RGB
					resize image height 1000 as pixels
					save docRef in file KindleFrontC as JPEG with options jpgOptions with copying
				end tell
				
				
				close docRef without saving
				
			end tell
		end if
	end repeat
end open

The original script which creates only the two JPGs:


on open theItems
	-- set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	repeat with thefile in theItems
		tell application "System Events"
			set {name extension:fileExtension, container:parentFolder} to thefile
			set parentFolder to path of parentFolder
		end tell
		if fileExtension is "pdf" then
			set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
			set pageWidth to pageWidth as real
			
			tell application "Adobe Photoshop CS6"
				set display dialogs to never
				set myOptions to {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
				set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
				
				open file (thefile as text) as PDF with options myOptions
				set mystate to current history state of current document
				
				set docRef to current document
				set docName to name of docRef
				
				tell application "Finder"
					set JpgFolderName to "Hi-Res JPEG_" & docName
					set JpgFolder to (make new folder at parentFolder with properties {name:JpgFolderName})
					
					set BackC to (JpgFolder as string) & docName & "_Back Cover"
					set FrontC to (JpgFolder as string) & docName & "_Front Cover"
				end tell
				
				resize canvas document 1 width pageWidth as inches anchor position middle left
				--set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
				save docRef in file BackC as JPEG with options jpgOptions with copying
				set current history state of current document to mystate
				
				resize canvas document 1 width pageWidth as inches anchor position middle right
				--set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
				save docRef in file FrontC as JPEG with options jpgOptions with copying
				close docRef without saving
				
			end tell
		end if
	end repeat
end open

any suggestions to create two more Kindle version JPGs from the already created two JPGs.
i.e. re-saving the JPGs with different filename after performing a photoshop task.

Thanks in advance… :slight_smile:

Hi. If you can put events into a logical order”as you’ve done in your question”then you can write a script, and we are all just manipulating things per our requirements. :slight_smile:

The code you have already saves one file, and DJ showed you how to edit the name using a character range. You just need to instruct PS to save as many times as you want files. The calls to System Events and Finder are unnecessary.


property saveLocus : (path to desktop) as text --a destination path


tell application "Adobe Photoshop CS3"
	tell document 1
		--insert code to do whatever with the opened PS file
		--
		--test the name's ending:
		tell name as text
			if it ends with "Back Cover.jpg" then
				set newName to text 1 thru -15
			else -- file ends with "Front Cover.jpg"
				set newName to text 1 thru -16
			end if
		end tell
		--save it to your location(s) with a modified name:
		save it in saveLocus & newName & "Kindle.jpg" as JPEG with options {class:JPEG save options} with copying
		save it in saveLocus & newName & "whatever.jpg" as JPEG with options {class:JPEG save options} with copying
		close saving no
	end tell
end tell

Hi Marc,

Thanks for the suggestions. However, when I tried your code, it is only saving the Back Cover jpg file with two different filenames, which is however, not needed. It seems that I failed to explain myself.

Here is the scenario:

  1. There are two JPG files somewhere in my server or Hard Disk. There names are something like:
    a. PP Native Cover_3151238_Back Cover.jpg
    b. PP Native Cover_3151238_Front Cover.jpg

the only fix part is the last part i.e. “Back Cover.jpg” and “Front Cover.jpg”

  1. I would like to open these two files together or one by one in Photoshop CS6 using the AppleScript, perform a task in photoshop and then saving these two JPGs with a new middle name in between the filenames, for example:
    the “…Back Cover.jpg” will need to be saved as “…Kindle Back Cover.jpg” and “…Front Cover.jpg” will need to be saved as “…Kindle Front Cover.jpg”:

    a. PP Native Cover_3151238_Kindle Back Cover.jpg
    b. PP Native Cover_3151238_Kindle Front Cover.jpg

Hope I am clear this time.

Marc, I have tried to incorporate your code in my script. This script successfully opens the Back Cover file, perform the tasks in photoshop but failed while resaving it.

The error I got is:

The modified AppleScipt is:

tell application "Finder"
	set saveLocus to (target of front window)
	set BackJPG to (1st item of saveLocus whose name ends with "Back Cover.jpg")
	set FrontJPG to (1st item of saveLocus whose name ends with "Front Cover.jpg")
	
	tell application "Adobe Photoshop CS6"
		set display dialogs to never
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		
		-- opening the "...Back Cover.jpg" file, performing a task in photoshop and then saving it as "...Kindle Back Cover.jpg"
		open file (BackJPG as text)
		tell document 1
			change mode to RGB
			resize image width 625 as pixels
			tell name as text
				set newBack to text 1 thru -15
			end tell
			save it in saveLocus & newBack & "Kindle.jpg" as JPEG with options jpgOptions with copying
			close saving no
		end tell
		
		-- opening the "...Front Cover.jpg" file, performing a task in photoshop and then saving it as "...Kindle Front Cover.jpg"
		open file (FrontJPG as text)
		tell document 1
			change mode to RGB
			resize image width 625 as pixels
			tell name as text
				set newFront to text 1 thru -16
			end tell
			save it in saveLocus & newFront & "Kindle.jpg" as JPEG with options jpgOptions with copying
			close saving no
		end tell
		
	end tell
end tell

Could you please rectify the errors in this script so that it can successfully open the first file, perform the photoshop task, save as with a different filename and then open the second file and so on…

The error is because you used the Finder; I advised against that because it was unnecessary, but PS also can’t understand its reference form. I coerced it to text. Read the event log and compare the difference in the first few lines.


tell application "Finder"
	set saveLocus to (target of front window) as text
	set BackJPG to (1st item of folder saveLocus whose name ends with "Back Cover.jpg") as text
	set FrontJPG to (1st item of folder saveLocus whose name ends with "Front Cover.jpg") as text
end tell

tell application "Adobe Photoshop CS3"
	set display dialogs to never
	set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
	-- opening the "...Back Cover.jpg" file, performing a task in photoshop and then saving it as "...Kindle Back Cover.jpg"
	open file (BackJPG)
	tell document 1
		change mode to RGB
		resize image width 625 pixels
		tell name as text
			set scavengedText to text 1 thru -15
		end tell
		save it in saveLocus & scavengedText & "Kindle Back Cover.jpg" as JPEG with options jpgOptions with copying
		close saving no
	end tell
	
	-- opening the "...Front Cover.jpg" file, performing a task in photoshop and then saving it as "...Kindle Front Cover.jpg"
	open file (FrontJPG as text)
	tell document 1
		change mode to RGB
		resize image width 625 pixels
		tell name as text
			set scavengedText to text 1 thru -16
		end tell
		save it in saveLocus & scavengedText & "Kindle Front Cover.jpg" as JPEG with options jpgOptions with copying
		close saving no
	end tell
end tell

Just FYI: The dictionary indicates that you follow the width specifier with a real number and a unit value”not “as pixels.” This may or may not work for you; it doesn’t on my setup.

        resize image width 625 pixels --proper syntax, but errors for me 

        resize image width 6 --this works, but is in inches

Hi Marc,

You deserve a party… :slight_smile: Many thanks. It is working exactly as it is needed.

This works fine with Photoshop CS6:


resize image width 625 as pixels

Thanks again.