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

Auto link text layer with the vector layer below it

Participant ,
Sep 10, 2024 Sep 10, 2024

Copy link to clipboard

Copied

Hello everyone! As title, I need a fast way to make all the text layers in a group link with their below shape. The layers will be sorted like this:

Zipser31550168t845_1-1725976696696.png

Zipser31550168t845_2-1725976761336.png

There are many files like this and I cannot complete in few days. Anyone could help, I really thank you so very much!

 

 

TOPICS
Actions and scripting , Windows

Views

341

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 1 Correct answer

Community Expert , Sep 11, 2024 Sep 11, 2024

@Zipser31550168t845 

 

Try this new script, it no longer uses layer name matching and now uses layer order. The onus is on you that the order of layers in both groups is correct.

 

As a bonus, the layers in the ANIMAL SHAPE group will be renamed to match the layer names in the TEXT group. You're welcome!  :]

 

/*
Link Layers in 2 Specified Layer Groups By Layer Order.jsx
v1.0 - 12th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/auto-link-text-layer-
...

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 10, 2024 Sep 10, 2024

Copy link to clipboard

Copied

Does each file consistently only have two groups: TEXT and ANIMAL SHAPE? If not, do the groups always have the same names? If not, are they always in the same relative layer stack position?

 

Does each group consistently only have 3 layers? Or does this vary?

 

Does the layer stacking order position of the first text layer in the TEXT group always match the same layer stacking order position in the ANIMAL SHAPE group and layer?

 

Are the text layer names/content consistently the same name as the animal shape name? Such as APE and Ape 1? Forgetting the case differences and the space and digit at the end of the shape layer name.

 

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
Participant ,
Sep 10, 2024 Sep 10, 2024

Copy link to clipboard

Copied

Thanks for your detailed asking! These are my answers:

- The files will not only have 2 groups, it may have more groups like these. But the "animal shape" and "text" will always stand above of the 'background', other will not be important:

Zipser31550168t845_0-1726032677697.png

Zipser31550168t845_1-1726032740795.png

 

- Each group has various layers (not only 3, sometimes it only has one, sometimes it has 10), but there're always a text above of a vector layer.

- Yes, text layer in the TEXT group always match the same layer stacking order position in the ANIMAL SHAPE group and layer.

- The name of text layer will not same as the shape name

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 ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

@Zipser31550168t845 

 

Try the following script on a few different files, it doesn't batch process it just works on the open document:

 

/*
Link Layers in 2 Specified Layer Groups By Name.jsx
v1.0 - 12th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/auto-link-text-layer-with-the-vector-layer-below-it/td-p/14852001
*/

#target photoshop

try {

    if (app.documents.length) {

        app.activeDocument.suspendHistory("Link Layers in 2 Specified Layer Groups By Name.jsx", "main()");

        function main() {

            // Get the "TEXT" and "ANIMAL SHAPE" groups
            var textGroup = getGroupByName("TEXT");
            var animalGroup = getGroupByName("ANIMAL SHAPE");

            // Conditional check for the nominated groups
            if (textGroup.layers.length == animalGroup.layers.length) {

                // Loop over the "TEXT" group layers
                for (var i = 0; i < textGroup.layers.length; i++) {

                    // Set the "TEXT" group layer variables
                    var textLayer = textGroup.layers[i];
                    var textLayerFirstWord = getLayerFirstWord(textLayer.name);
                    var textLayerVisibility = textLayer.visible;

                    // Conditional check for the "TEXT" group layer name
                    if (textLayerFirstWord) {

                        // Loop over the "ANIMAL SHAPE" group layers
                        for (var j = 0; j < animalGroup.layers.length; j++) {
                            // Set the "ANIMAL SHAPE" group layer variables
                            var animalLayer = animalGroup.layers[j];
                            var animalLayerFirstWord = getLayerFirstWord(animalLayer.name);
                            var animalLayerVisibility = animalLayer.visible;

                            // Conditional match for each group's layer name
                            if (textLayerFirstWord === animalLayerFirstWord) {
                                // Link the matching layers in each group
                                textLayer.link(animalLayer);
                                // Set the layer visibility
                                textLayer.visible = textLayerVisibility;
                                animalLayer.visible = animalLayerVisibility;
                                break;
                            }

                            // End of script notification
                            //app.beep();
                            //alert("All matching layers have been linked!");
                        }
                    }
                }
            } else {
                alert("The number of layers in the nominated groups don't match!")
            }


            ///// FUNCTIONS /////

            // Get the group by name
            function getGroupByName(groupName) {
                return app.activeDocument.layerSets.getByName(groupName);
            }

            // Case insensitive regex to get the first word of the layer name
            function getLayerFirstWord(layerName) {
                // Basic regex
                //var firstWord = layerName.match(/^\w+/i);
                // Robust regex
                var firstWord = layerName.match(/^[^\s]+/i);
                return firstWord ? firstWord[0].toLowerCase() : null;
            }
        }

    } else {
        alert("A document must be open to run this script!");
    }

} catch (err) {
    alert("Error!" + "\r" + err + "\r" + 'Line: ' + err.line);
}

 

If this works as expected, you can record the script into an Action. You can then use the File > Automate > Batch command to run the script via the action over multiple files. Alternatively, the script can be modified for batch processing.

 

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
Participant ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Thank you very much for helping me, so enthusiastic! I've tried the script, it worked great with text has same name with the shape below it, but not affect if they had different name. Most time we have the layers like this:

Zipser31550168t845_0-1726111459346.png

If it's impossible for making a code for this kind of layer, I think I would rename the shapes as the text above it, but uh... It would be another problem when I have to rename a lot of layers haha!

Do you have other idea?

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 ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

@Zipser31550168t845 

 

Ah, when you previously wrote "The name of text layer will not same as the shape name” – I was hoping that you were only referring to the space + digit at the end of the layer name.

 

I'll look at changing this over to use the layer stacking order...

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 ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

quote

It would be another problem when I have to rename a lot of layers haha!


By @Zipser31550168t845

 

Yes, I understand... But it really would have been better if the layers were correctly named in the first place as they were created. It's helpful for both humans and software!  :]

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 ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

@Zipser31550168t845 

 

Try this new script, it no longer uses layer name matching and now uses layer order. The onus is on you that the order of layers in both groups is correct.

 

As a bonus, the layers in the ANIMAL SHAPE group will be renamed to match the layer names in the TEXT group. You're welcome!  :]

 

/*
Link Layers in 2 Specified Layer Groups By Layer Order.jsx
v1.0 - 12th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/auto-link-text-layer-with-the-vector-layer-below-it/td-p/14852001
*/

#target photoshop

try {

    if (app.documents.length) {

        app.activeDocument.suspendHistory("Link Layers in 2 Specified Layer Groups By Layer Order.jsx", "main()");

        function main() {

            // Get the "TEXT" and "ANIMAL SHAPE" groups
            var textGroup = getGroupByName("TEXT");
            var animalGroup = getGroupByName("ANIMAL SHAPE");

            // Conditional check for the nominated groups
            if (textGroup.layers.length == animalGroup.layers.length) {
                // Loop through each layer in both groups
                var numberOfLayers = Math.min(textGroup.layers.length, animalGroup.layers.length);
                for (var i = 0; i < numberOfLayers; i++) {
                    // Set the "TEXT" group layer variables
                    var textLayer = textGroup.layers[i];
                    var textLayerVisibility = textLayer.visible;
                    // Set the "ANIMAL SHAPE" group layer variables
                    var animalLayer = animalGroup.layers[i];
                    var animalLayerVisibility = animalLayer.visible;
                    // Link the layers in each group using the layer order
                    textLayer.link(animalLayer);
                    // Set the layer visibility and rename the animal layer names to match the text layers
                    textLayer.visible = textLayerVisibility;
                    var theName = textLayer.name.toLowerCase();
                    var toSentenceCase = theName.charAt(0).toUpperCase() + theName.slice(1);
                    animalLayer.name = toSentenceCase + " 1";
                    animalLayer.visible = animalLayerVisibility;
                }

                // End of script notification
                //app.beep();
                //alert("The layers have been linked using the layer order!");
            } else {
                alert("The number of layers in the nominated groups don't match!")
            }


            ///// FUNCTIONS /////

            // Get the group by name
            function getGroupByName(groupName) {
                return app.activeDocument.layerSets.getByName(groupName);
            }

        }

    } else {
        alert("A document must be open to run this script!");
    }

} catch (err) {
    alert("Error!" + "\r" + err + "\r" + 'Line: ' + err.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
Participant ,
Sep 12, 2024 Sep 12, 2024

Copy link to clipboard

Copied

Thank you so much! This exceeded my expectations!

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 ,
Sep 12, 2024 Sep 12, 2024

Copy link to clipboard

Copied

LATEST

@Zipser31550168t845 – You’re welcome!

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