• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change all text layers content to X

Explorer ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

Hello, it's me again! This time I want to find a script/action that can replace all text layer contents to X, or add XX before each text layer content. For the first question, each letter will become an X, regardless of uppercase or lowercase letters. If it's a latin character, of course I can do this by find & replace 26 times letter by letter. The thing is that I have to do this with traditional Chinese characters, they don't have much in common with find & replace.

For the 2nd question, I've been looking for related scripts and tricks, but there doesn't seem to be a satisfactory answer. I found this post on Stack and followed it, but no answer since then:

https://superuser.com/questions/1590207/advanced-find-and-replace-text-in-photoshop 

I don't know if my questions are possible, so thanks a lot and sorry if they bothers you, as I really don't know much about scripts.

Have a nice day!

TOPICS
Actions and scripting , Windows

Views

4.1K

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
community guidelines

correct answers 2 Correct answers

Community Expert , Feb 23, 2023 Feb 23, 2023
quote

Thank you for your help! I tried with a text layer and this works exactly how I needed. So how do I apply this script to the entire text layer in the file? 


By @Le27921535n6i8

 

Try this 1.1 update:

 

for (var i = 0; i < activeDocument.layers.length; i++) {
    try {
        activeDocument.activeLayer = activeDocument.layers[i];
        if (activeDocument.activeLayer.kind === LayerKind.TEXT && (/^X/i.test(activeDocument.activeLayer.textItem.contents) === false)) {
            activeDocument.ac
...

Votes

Translate

Translate
Community Expert , Feb 24, 2023 Feb 24, 2023
quote

Thank you by all means! Maybe this is enough for me, then I will try to ungroup the text layers first. Your help will change my life so much! I don't know how to thank you enough, so may the best things come to you in the future!


By @Le27921535n6i8

 

You're welcome!

 

No need to ungroup! The following script will process all text layers in all top-level layers, top-level layer groups and nested layer groups with a single undo history step:

 

/*
Add prefix to all top-level text layers and layer
...

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

Can you post a sample PSD with text layers, or at least a cropped screenshot of the layers panel so that the layer structure can be determined?

Votes

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
community guidelines
Explorer ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

Thank you for replying! This is an example of my layer palette, but my files usually don't arrange them in order. Anyway, what they all have in common is just needing to add X, so replacing all the content layers is fine to me:

Le27921535n6i8_1-1677131538732.png

 

 

Votes

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
community guidelines
Community Expert ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

The following script will add "X " before the start of every new line, in every text layer, in all open docs:

 

for (var i = 0; i < documents.length; i++) {
    activeDocument = documents[i];

    var doc = activeDocument;
    for (var j = 0; j < doc.layers.length; j++) {
        try {
            doc.activeLayer = doc.layers[j];
            if (doc.activeLayer.kind == LayerKind.TEXT) {
                doc.activeLayer.textItem.contents = doc.activeLayer.textItem.contents.replace(/^/gm, 'X ');
            }
        } catch (e) {}
    }
}

 

To replace existing roman characters, try replacing:

 

doc.activeLayer.textItem.contents = doc.activeLayer.textItem.contents.replace(/^/gm, 'X ');

 

With:

 

doc.activeLayer.textItem.contents = doc.activeLayer.textItem.contents.replace(/[a-z]/gm, 'X');

 

Note, not all text attributes may be retained, for example in a text area, a second line of text coloured differently than the first line would inherit the colour of the first line. Perhaps somebody else can suggest how to retain font formatting such as different colours, different fonts, different sizes etc.

 

I have tested with Chinese font/characters and Roman characters.

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Votes

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
community guidelines
Explorer ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

Thank you very much! I tried your script, it work perfectly with new documents, but it doesn't seem to work with saved files. Even I successfully applied in a new random file, but after clicking Save and trying again the script doesn't work with that file anymore. Is that an error or a limitation of photoshop?

Votes

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
community guidelines
Community Expert ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

You didn't mention nested layer groups, that would need different code added.

 

If that screenshot is a beforehand image, what would the final result look like?

Votes

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
community guidelines
Explorer ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Oh, so it turns out! I'm terribly sorry for my shortcoming, my screenshot is a beforehand one. And also it's my mistake, actually your script was working fine with non-contained layers, after saving I didn't notice I had put the layers in the group so that's it ^^. Please help me to fix it my savier, thank you a lot!

Votes

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
community guidelines
Community Expert ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Hmm... There is nothing in the code that should make a difference between an unsaved and saved document.

 

Let's try to simplify the code. The following will only work on a single selected text layer, one layer at a time, only in the current document:

 

try {
    if (activeDocument.activeLayer.kind === LayerKind.TEXT) {
        activeDocument.activeLayer.textItem.contents = activeDocument.activeLayer.textItem.contents.replace(/\r*$/, "").replace(/^/gm, 'X ');
    }
} catch (e) {
    alert("Error!" + "\r" + e + ' ' + e.line);
}

 

 

Votes

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
community guidelines
Explorer ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Thank you for your help! I tried with a text layer and this works exactly how I needed. So how do I apply this script to the entire text layer in the file? 

Votes

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
community guidelines
Community Expert ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

quote

Thank you for your help! I tried with a text layer and this works exactly how I needed. So how do I apply this script to the entire text layer in the file? 


By @Le27921535n6i8

 

Try this 1.1 update:

 

for (var i = 0; i < activeDocument.layers.length; i++) {
    try {
        activeDocument.activeLayer = activeDocument.layers[i];
        if (activeDocument.activeLayer.kind === LayerKind.TEXT && (/^X/i.test(activeDocument.activeLayer.textItem.contents) === false)) {
            activeDocument.activeLayer.textItem.contents = activeDocument.activeLayer.textItem.contents.replace(/\r*$/, "").replace(/^/gm, 'X ');
        }
    } catch (e) {
        alert("Error!" + "\r" + e + ' ' + e.line);
    }
}

 

 

Votes

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
community guidelines
Explorer ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Thank you for all your efforts to help me! This time I use your script it only applies on a  selected layer, but it adds 5 x's:

Le27921535n6i8_0-1677205996199.png

 

Votes

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
community guidelines
Community Expert ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Please attach the before layered PSD file for testing. You can resize it smaller if needed, I just need to test with a copy to work out what is going on.

Votes

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
community guidelines
Explorer ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Okay Sir, I resized it so this is a smaller version of my test file. I created 2 text inside the group and 2 outside so it's easier to compare:

https://drive.google.com/file/d/1NSh7ONmdp8p6_dRsM6oVJ_z0ZTRwzy5X/view?usp=sharing

Votes

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
community guidelines
Community Expert ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

@Le27921535n6i8 – I have updated the previous code to a 1.1 version, it will now check if there is already an X at the beginning of the text and if there is it will not add an X another time. At the moment the code is only changing top-level text layers and is not looking inside layer groups.

Votes

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
community guidelines
Explorer ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

Thank you by all means! Maybe this is enough for me, then I will try to ungroup the text layers first. Your help will change my life so much! I don't know how to thank you enough, so may the best things come to you in the future!

Votes

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
community guidelines
Community Expert ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

LATEST
quote

Thank you by all means! Maybe this is enough for me, then I will try to ungroup the text layers first. Your help will change my life so much! I don't know how to thank you enough, so may the best things come to you in the future!


By @Le27921535n6i8

 

You're welcome!

 

No need to ungroup! The following script will process all text layers in all top-level layers, top-level layer groups and nested layer groups with a single undo history step:

 

/*
Add prefix to all top-level text layers and layer groups and nested layer groups.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/change-all-text-layers-content-to-x/m-p/13604007
v1.0 - 24th February 2023, Stephen Marsh
*/

#target photoshop

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
    } else {
        processAllLayersAndSets(activeDocument);
    }

    function processAllLayersAndSets(obj) {
        for (var al = obj.layers.length - 1; 0 <= al; al--) {
            activeDocument.activeLayer = obj.layers[al];
            addPrefixToTextLayers();
        }
        
        for (var ls = obj.layerSets.length - 1; 0 <= ls; ls--) {
            processAllLayersAndSets(obj.layerSets[ls]);
            addPrefixToTextLayers();
        }
    }

    function addPrefixToTextLayers() {
        try {
            if (activeDocument.activeLayer.kind === LayerKind.TEXT && (/^X/i.test(activeDocument.activeLayer.textItem.contents) === false)) {
                activeDocument.activeLayer.textItem.contents = activeDocument.activeLayer.textItem.contents.replace(/\r*$/, "").replace(/^/gm, 'X ');
            }
        } catch (e) {
            alert("Error!" + "\r" + e + ' ' + e.line);
        }
    }
}

activeDocument.suspendHistory('Add Prefix to Text Layers...', 'main()');

 

Votes

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
community guidelines
Community Expert ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Hello, it's me again! This time I want to find a script/action

By @Le27921535n6i8

 

 

You do know, I hope, that most of the time our brilliant scripters aren't "finding" scripts for you — most of the time they are writing them for you! 😊

 

Votes

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
community guidelines
Explorer ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Yes! They are truly amazing angels!

Votes

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
community guidelines