Skip to main content
jimm7329411
Known Participant
February 13, 2020
Question

Rename Photoshop document using text from a text layer

  • February 13, 2020
  • 2 replies
  • 2203 views

How can I rename or save the Photoshop document name using a text later within the document?

I have data coming in from Excel to Photoshop using variables and I need to use that data to rename

the document as well.  I thought I had this working at one point but maybe I was doing it a different way.

Is there a way to do it with Scripting?  How could this be done?

Thank you!!!

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
February 14, 2020

Another option is to use the cross-platform ExifTool CLI utility – however, it all depends on how many text layers there are and in which position the target text layer falls etc.

 

This example assumes a single text layer and uses the text layer name:

 

 

exiftool '-FileName<${XMP-photoshop:TextLayerName}.%e' '/Users/user/Mac OS Path to/Folder-Of-Files-To-Rename'

 

 

This example assumes a single text layer and uses the text layer content:

 

 

exiftool '-FileName<${XMP-photoshop:TextLayerText}.%e' '/Users/user/Mac OS Path to/Folder-Of-Files-To-Rename'

 

 

Windows users would swap the ' single straight quote marks for " double straight quote marks and use an appropriate file system syntax.

 

Whether using a script or ExifTool command line code, it is probably a good idea to add-in extra code to save the current filename to the XMP-xmpMM:PreservedFileName metadata, just in case you wish to undo the rename and get back to the original filename. One can always use Adobe Bridge Batch Rename Tool to bulk populate the preserved filename metadata into each file, before renaming using another method.

jimm7329411
Known Participant
February 14, 2020

Thanks everyone!  That helps so much!  This works except that when this script is ran in a batch process a dialog box pops up on each photo asking whether to save it or not.  I tried to take the "catch" out but it doesn't like that.

 

Also... I don't know if I should make a new post for this but I'll throw this in here too.

I have a little code here to check the orientation of the active document. Then do an action to rotate it 90 degrees if it is landscape.  Two questions on this...

 

1- How would I do this without using the action?  So rotating it with the script?

2- How would it change if I wanted to check and rotate the active layer? 

 

Here is the code for rotating the document.

 

#target photoshop
 
var doc = app.activeDocument;
if(doc.width.value>doc.height.value){//Landscape
doAction("Rotate-90","Automations");
}

 

Thank you!!!

JJMack
Community Expert
Community Expert
February 14, 2020

If ruler units is set to percent

if(doc.width.value>doc.height.value){//Landscape doAction("Rotate-90","Automations");}
 
will never rotate for width and height will both be 100%
JJMack
Legend
February 13, 2020

This takes the contents of the topmost text layer and renames the file based on that. You'll want to further refine it as this is prety basic.

 

#target photoshop
renameFromTextLayer();
function renameFromTextLayer(){
try{
if(documents.length > 0){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var docRef = activeDocument;
var layerText = '';
for(var i = 0; i < docRef.artLayers.length; i++){
var LayerRef = docRef.artLayers[i];
if(LayerRef.kind == LayerKind.TEXT){
layerText = LayerRef.textItem.contents;
break;
}
}
var psFile = File(docRef.fullName);
var psFileName = psFile.name.split('.');
if(psFileName.length > 1){
layerText = layerText + '.' + psFileName[psFileName.length - 1];
}
docRef.close();
psFile.rename(layerText);
}
app.displayDialogs = originalDialogMode;
}
catch(e){
alert(e + e.line);
app.displayDialogs = originalDialogMode;
}
}

JJMack
Community Expert
Community Expert
February 13, 2020

You can also add code to open the renamed file for it not posible to rename an open document the open document is first closed then its backing file is renamed. I added the open of the renamed file here

#target photoshop
renameFromTextLayer();
function renameFromTextLayer(){
	try{
		if(documents.length > 0){
			var originalDialogMode = app.displayDialogs;
			app.displayDialogs = DialogModes.ERROR;
			var docRef = activeDocument;
			var layerText = '';
			for(var i = 0; i < docRef.artLayers.length; i++){
				var LayerRef = docRef.artLayers[i];
				if( layerText== '' & LayerRef.kind == LayerKind.TEXT){
					layerText = LayerRef.textItem.contents;
					break;
				}
			}
		if(layerText != ''){	
			var path = docRef.path  + "/";		
			var psFile = File(docRef.fullName);
			var psFileName = psFile.name.split('.');
			if(psFileName.length > 1){
				layerText = layerText + '.' + psFileName[psFileName.length - 1];
			}
			var path = docRef.path  + "/";
			docRef.close();
			psFile.rename(layerText);
			open(File(path + layerText));
		}
		else {alert("Not text found")}
	}
	app.displayDialogs = originalDialogMode;
	}
	catch(e){
		alert(e + e.line);
		app.displayDialogs = originalDialogMode;
	}
}

 

 

 

JJMack
Legend
February 14, 2020

You would also want to check for an empty text layer, and strip any illegal characters like "/" from the new name, and check to see if a file with that name already exists, and that the file extension is correct. Scripting means you build your own guardrails as you go.