Skip to main content
Participating Frequently
August 14, 2020
Question

A script that finds and replaces text in multiple layers based on the clipboard

  • August 14, 2020
  • 3 replies
  • 1560 views

Dear Shoppers,

 

I am looking for a script that can select all text files in my Photoshop documents and replace a string in each text layer by the contents in my clipboard. E.g.:

 

artikelnummer: nummer

item number: nummer

numer artykulu: nummer

(6 languages total)

 

In this case the word nummer should be replaced by my text based clipboard content. Is this possible with scripting, and if so, has a script like this been made before?

 

Kind regards,

Dennis

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
August 14, 2020

Not sure about accessing the clipboard directly, but what about pasting the clipboard content into a prompt to set a variable, then use that variable for the replace?

JJMack
Community Expert
Community Expert
August 14, 2020

That is why I wrote some other source of replacement text.  A simple prompt or text file would most likely be easier to use then dealing with the clipboard.

JJMack
Participating Frequently
August 14, 2020

This seems like a good idea. I have built a template file for my editing (with various layer comps, these images will be product images for different webshops). Every single item needed for these images is already built into this template. Therefore I'm looking for something that can open my selected smart object layer (this can be done with actions), replace certain text with either the clipboard contents or replace it by the name of something, like a layer name, in that smart object. I don't know how I would be able to add something to an existing smart object and use that layer name as my replacing text.

JJMack
Community Expert
Community Expert
August 14, 2020

I  believe you meant text layers not text files as you wrote.

 

You can write a script to edit text layers and  the script could get the replacement text from the clipboard  or some other source. But there is a lot more to a text layer than content.   The replacement need to work with the other texts layers settings.  If you search this forum you may find some example scripts for changing text layers.

 

 

 

 

JJMack
Participating Frequently
August 14, 2020

Hi JJMack,

 

That is correct, I meant text layers. So far I have managed to get to this point together with a friend:

 

var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
try {
doc.activeLayer = doc.layers[i];
// Find = nummer, Replace = Cat
// g = global, i = case insensitve
if (app.activeDocument.activeLayer.kind == LayerKind.TEXT) {
doc.activeLayer.textItem.contents = doc.activeLayer.textItem.contents.replace(/\bnummer\b/gi, app.activeDocument.paste());
}
} catch (e) {}
}

 

app.activeDocument.Paste() doesn't seem to paste text in the "Change to:" text box, but rather seems to assume it's an image in the clipboard.

JJMack
Community Expert
Community Expert
August 14, 2020

You would need to paste into the text tool edit mode.Not paste into the document. How to get the text out of the clipboard into a script variable for text content can be a problem. I do not know a the solution. 

JJMack
Participating Frequently
August 14, 2020

This is what I have come up with so far, but it doesn't seem to work...

 

var doc = app.activeDocument;
for(var i=0;i<doc.layers.length;i++){
try{
di=(docRef.name).indexOf(".");
fname = (docRef.name).substr(0, di);
//use extension if set
if ( ShowExtension == "y" )
{
fname = docRef.name
}

doc.activeLayer = doc.layers[i];
// Find = Dog, Replace = Cat
replace("nummer", "fname", true, true, false, false, false); // Change the 3rd boolean to true for case sensitive match
}
catch(e){}
}


function replace(find, replace, checkAll, forward, caseSensitive, wholeWord, ignoreAccents) {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putProperty( stringIDToTypeID( "property" ), stringIDToTypeID( "replace" ));
reference.putEnumerated( stringIDToTypeID( "textLayer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "allEnum" ));
descriptor.putReference( charIDToTypeID( "null" ), reference );
descriptor2.putString( stringIDToTypeID( "find" ), find );
descriptor2.putString( stringIDToTypeID( "replace" ), replace );
descriptor2.putBoolean( stringIDToTypeID( "checkAll" ), checkAll ); // Boolean #1
descriptor2.putBoolean( stringIDToTypeID( "forward" ), forward ); // Boolean #2
descriptor2.putBoolean( stringIDToTypeID( "caseSensitive" ), caseSensitive ); // Boolean #3
descriptor2.putBoolean( stringIDToTypeID( "wholeWord" ), wholeWord ); // Boolean #4
descriptor2.putBoolean( stringIDToTypeID( "ignoreAccents" ), ignoreAccents ); // Boolean #5
descriptor.putObject( stringIDToTypeID( "using" ), stringIDToTypeID( "findReplace" ), descriptor2 );
executeAction( stringIDToTypeID( "replace" ), descriptor, DialogModes.NO );
}