12 Replies Latest reply on May 14, 2014 11:36 PM by c.pfaffenbichler

    Scripting text layers

    jamesmacc

      Hey ho,

      Basically what I'm hoping to do is idiot proof a document.

      I have a template saved, which is opened as edited as needed - it is the text layers I'm more concerned about here.

      The other guys in the lab struggle to use windows never mind Photoshop, so what I'm hoping to do write a script capable of changing the text inside an multiple existing text layers, then record an action of opening the script and put the actions to button mode - so as not to confuse the cavemen.

       

      I hope to have a pop-up input field, and the inputs should change the existing text within the specified layers. Nothing about the text should change (Font, Style, Colour etc ..). I think in total there are 8 layers, in which the text needs to be changed upon opening.

       

      any input would be much appreciated guys,

       

      cheers

        • 1. Re: Scripting text layers
          JJMack Most Valuable Participant

          There is a scripting forum you can write a script to process all layer in a document an retrieve text and prompt the user with the text so the can change it.  However text can be multi lines formatted etc and the user mat enter more text then will fit  and may mess up formatting.  To handle all may be quite complex

          • 2. Re: Scripting text layers
            jamesmacc Level 1

            surely there must be a way to select the "layer 1" then replace the text within the layer with the input from the prompt box. The text represents different information and are in layers to suit. eg, reference number, date, technician name etc etc ... The text layers are not merged, flattened or connected in any way to other layers within the document.

            • 3. Re: Scripting text layers
              c.pfaffenbichler Level 9

              How can the Layers be identified (name, order in the Layers Panel, …)?

              • 4. Re: Scripting text layers
                c.pfaffenbichler Level 9

                In this example the type layers are identified by name ad would have to be on the top level.

                // 2014, use it at your own risk;
                #target photoshop
                ////////////////////////////////////
                if (app.documents.length > 0) {
                var myDocument = app.activeDocument;
                //////////////////////////////////////////
                ////// create dialog for customer-entry //////
                var dlg = new Window('dialog', "Enter the texts", [500,300,820,850]);
                dlg.textA = dlg.add('edittext', [12,15,308,35], "", {multiline:false});
                dlg.textA.active = true;
                dlg.textB = dlg.add('edittext', [12,45,308,65], "", {multiline:false});
                //////////////////////////////
                // build- and cancel-button;
                dlg.buildBtn = dlg.add('button', [168,470,307,500], 'OK', {name:'ok'});
                dlg.cancelBtn = dlg.add('button', [13,470,153,500], 'Cancel', {name:'cancel'});
                dlg.center();
                var myReturn = dlg.show ();
                // check fpr availability of the used fonts;
                if (myReturn == true) {
                replaceTheText (myDocument.layers.getByName("text1"), dlg.textA.text);
                replaceTheText (myDocument.layers.getByName("text2"), dlg.textB.text);
                };
                };
                //////////////////////////////
                function replaceTheText (theLayer, theString) {
                theLayer.textItem.contents = theString
                };
                
                

                 

                Edit: And the possibilities for something to go wrong are there indeed.

                So one can hope for the best, wrap the possibly problematic stuff in try-clauses or include checks.

                • 5. Re: Scripting text layers
                  jamesmacc Level 1

                  sheer amazingness thanks very much, just changed "text1" & "2" to layer names and it works. So if I want to increase this to 4 layers, and have desciptions adjacent to each input field how would I go about that ?

                   

                  • 6. Re: Scripting text layers
                    c.pfaffenbichler Level 9

                    You can add additional lines like

                    dlg.textB = dlg.add('edittext', [12,45,308,65], "", {multiline:false});

                    but you need to give each a distinct name (instead of »textB«) so that you can later on evaluate the text with another added line like

                    replaceTheText (myDocument.layers.getByName("text2"), dlg.textB.text); 

                    in which you also need to amend the argument in the brackets after getByName.

                    • 7. Re: Scripting text layers
                      jamesmacc Level 1

                      Thanks I had a play around and got it working ! now, I take it that the numeric values ..

                      dlg.textA = dlg.add('edittext', [12,15,308,35], "", {multiline:false});

                      .. are co-ordinates ? I continued the pattern between your first and second values and this seems to be the case. One last thing though if you could, I want to enter static text above each input box, I have tried and I just don't know what code to put in to achieve this.

                       

                      cheers

                      • 8. Re: Scripting text layers
                        c.pfaffenbichler Level 9

                        I want to enter static text above each input box

                        Sorry, you had mentioned that.

                        Lines like

                        dlg.titleA = dlg.add('statictext', [12,5,308,25], "text a");

                        should work.

                        You’ll have to amend the Array that, as you concluded, describes the position within the object’s parent »container«. (edited)

                        • 9. Re: Scripting text layers
                          Level 1

                          Thank you

                          • 10. Re: Scripting text layers
                            jamesmacc Level 1

                            that did the trick, thanks for your help much appreciated

                            • 11. Re: Scripting text layers
                              jamesmacc Level 1

                              actually ... a while ago you gave me a script that would allow me to enter Lab values and this would change the foreground colour. I'm now thinging that I could cannibalize that script and involve it within the this one, and then automatically fill a layer. how would I go about this  ?

                               

                              thank