Highlighted

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

New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

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

Topics

Actions and scripting

Views

75

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

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

Topics

Actions and scripting

Views

76

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

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 );
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 14, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
JJMack LATEST
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

If you are using templates and want to do some text replacing.  Have you looked at or considered Photoshop Data Driven graphics feature. 

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 14, 2020

Copy link to clipboard

Copied

You can create a window with a text field and set visibility to zero, or read from a dialog.

 

function layerUpdate(){
if(documents.length > 0){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
try{
var docRef = activeDocument;
preferences.rulerUnits = Units.POINTS;
for(var i = 0; i < docRef.artLayers.length; i++){
var LayerRef = docRef.artLayers[i];
if(LayerRef.kind == LayerKind.TEXT){
var TextRef = LayerRef.textItem;
var layerText = TextRef.contents;

var Replacement = 'new text';
var newText = layerText.replace('old text', Replacement);
}
}
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
catch(e){
}
}
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...