23 Replies Latest reply on Jul 19, 2018 6:12 PM by Peter Kahrel

    Script not working in CC2018

    graphicsguy85

      I have the script below, but when I run it, nothing happens and I can't understand why.  It literally worked about 5 days ago and now after i updated yesterday its not working.  Please Help.  I am trying to apply a cell style based on cell content.

       

       

      var myDoc = app.activeDocument

      app.findTextPreferences = app.changeTextPreferences = null

      app.findTextPreferences.findWhat = "1"

       

      var myFound = myDoc.findText()

       

      for(i=0; i<myFound.length; i++)

      {

          if(myFound[i].parent.constructor.name == "Cell")

          {

        

         myFound[i].parent.appliedCellStyle = "Green"

       

          }

      }

        • 1. Re: Script not working in CC2018
          Laubender Adobe Community Professional & MVP

          Hi,

          best make it a habit to end every single statement with a: ;

          Like that:

           

          var myDoc = app.activeDocument;
          app.findTextPreferences = app.changeTextPreferences = null;
          app.findTextPreferences.findWhat = "1";
          
          var myFound = myDoc.findText();
          
          for(var i=0; i<myFound.length; i++)
          {
              if(myFound[i].parent.constructor.name == "Cell")
              {
          
                 myFound[i].parent.appliedCellStyle = "Green";
          
              };
          };
          

           

          I see no principle problem in the code.

           

          What can go wrong then?

          An error could be thrown if the cell style is hosted by a cell style group.

           

          Regards,
          Uwe

           

          EDIT: To make that clear. Tested the code above with InDesign CC 2018.1 13.1.0.76 on Windows 10.

          No problem found if the cell style named "Green" is in the root of the Cell Styles Panel.

          • 2. Re: Script not working in CC2018
            Steve Werner Adobe Community Professional & MVP

            Moving to InDesign Scripting forum

            • 3. Re: Script not working in CC2018
              Loic.Aigon Adobe Community Professional

              I second Laubender, the called cell style may be invalid as it.

              Set a breakpoint and look at its status.

              • 4. Re: Script not working in CC2018
                graphicsguy85 Level 1

                when it runs it applies the text style to what I want it to but it doesn’t apply the cell style, it basically ignores it

                • 5. Re: Script not working in CC2018
                  graphicsguy85 Level 1

                  I have it in the cell styles panel, Di i need it to be anywhere else?

                  • 6. Re: Script not working in CC2018
                    Laubender Adobe Community Professional & MVP

                    Hi graphicsguy85 ,

                    call the cell style in a different way and test if it is valid:

                     

                    var cellStyle = app.documents[0].cellStyle.itemByName("Green");
                    if( !cellStyle.isValid ){ /*do something here*/ };
                    

                     

                    What can be done if the cell style is not valid?
                    Look after it in a nested structure of the Cell Styles Panel or add a new one if it still cannot be found.

                     

                    Do something here can be written this way:

                     

                    var allCellStylesOfDoc = app.documents[0].allCellStyles; // Returns array of cell styles
                    for(var n=0;n<allCellStylesOfDoc.length;n++)
                    {
                         if(allCellStylesOfDoc[n].name == "Green")
                         { cellStyle = allCellStylesOfDoc[n] ; break }
                    }
                    
                    if( !cellStyle.isValid )
                    {
                         app.documents[0].cellStyles.add
                         (
                              {
                                   name : "Green" ,
                                   /* 
                                        add other properties here. Look after read/write values you want to see.
                                        I inserted only pseudo value VALUE below.
                                        Look them up here: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CellStyle.html 
                    
                                        appliedParagraphStyle : VALUE,
                                        basedOn : VALUE ,
                                        bottomEdgeStrokeColor : VALUE ,
                                        …
                                        verticalJustification : VALUE
                                   */
                              }
                         )
                    };
                    

                     

                    Then work on with the rest of your script.

                    At the point you apply the cell style use the variable name cellStyle instead of the name "Green".

                     

                    Regards,
                    Uwe

                    • 7. Re: Script not working in CC2018
                      graphicsguy85 Level 1

                      I apologize I am still very new at this.  The entire reason for the script is to be able to run it on the attached file, and it automatically fill in the color based on the cell input.  are you able to make the script according the comment above with your new script?Screen Shot 2018-06-14 at 7.37.12 PM.png

                      • 8. Re: Script not working in CC2018
                        Laubender Adobe Community Professional & MVP

                        Hi,

                        you cannot attach a file to a forum post.

                        If you want to share it, just use a service like Dropbox and post the link.

                         

                        Best,
                        Uwe

                        • 9. Re: Script not working in CC2018
                          Liphou Level 2

                          Hi

                           

                          Actually I just tested the script no problem, a file will be welcome to help you.

                           

                           

                          Effectivement je viens de tester le script pas de problème, un fichier serai le bien venu pour vous aidée .

                          • 10. Re: Script not working in CC2018
                            graphicsguy85 Level 1

                            below is a link to file I am trying to use.  I am trying to turn lets just say the cells with "D" or 1* to a green background. It did work a few days ago but now won't work for some reason.  My computer is running High Sierra 10.13.4 , Indesign 13.1

                             

                            Dropbox - Table Script test.indd

                            • 11. Re: Script not working in CC2018
                              Liphou Level 2

                              it still works but the style is not forced "Green +".

                              //----

                              ça fonction toujours mais le style n'est pas forcé "Green+".

                               

                              myFound[i].parent.fillColor = "C=75 M=5 Y=100 K=0";
                              
                              • 12. Re: Script not working in CC2018
                                graphicsguy85 Level 1

                                where should I put that line of code? Does that replace the cell style line?

                                • 13. Re: Script not working in CC2018
                                  Liphou Level 2

                                  I do not force the style (I do not know if it's possible on a Cell Style) but place the color.
                                  Attention, there is no control over the existence of the runner (it must be there)

                                  /--------

                                  Je ne force pas le style (je ne sais pas si c'est possible sur un Style de cellule) mais place la couleur.
                                  Attention, il n'y a pas de contrôle sur l'existence de la coureur (elle doit être là)

                                   

                                   

                                  for(i=0; i<myFound.length; i++) {
                                  
                                      if(myFound[i].parent.constructor.name == "Cell") {
                                                  myFound[i].parent.appliedCellStyle = "Green";
                                                  myFound[i].parent.fillColor = "C=75 M=5 Y=100 K=0";
                                      }
                                  
                                  }//
                                  
                                  1 person found this helpful
                                  • 14. Re: Script not working in CC2018
                                    Laubender Adobe Community Professional & MVP

                                    Hi Liphou ,

                                    after seeing into the sample document I'd prefer a GREP find to isolate cell contents like "D" vs "DP".

                                    And you can use method clearCellStyleOverrides() on a cell.

                                     

                                    Code below is using all this:

                                     

                                    app.doScript
                                    (
                                    
                                        main,
                                        ScriptLanguage.JAVASCRIPT,
                                        [],
                                        UndoModes.ENTIRE_SCRIPT,
                                        "Apply Cell Style to found Cell | Uwe | SCRIPT"
                                    
                                    );
                                    
                                    function main()
                                    {
                                       
                                        var myDoc = app.activeDocument; 
                                        app.findGrepPreferences = app.changeGrepPreferences = null;
                                       
                                        app.findGrepPreferences.findWhat = "^D$";
                                         
                                        var myFound = myDoc.findGrep();
                                       
                                        app.findGrepPreferences = app.changeGrepPreferences = null;
                                         
                                        for(var i=0; i<myFound.length; i++) 
                                        { 
                                            if(myFound[i].parent.constructor.name == "Cell") 
                                            { 
                                                var cell = myFound[i].parent;
                                                cell.clearCellStyleOverrides( true );
                                                cell.appliedCellStyle = "Green";
                                            }; 
                                        };
                                       
                                    };
                                    

                                     

                                    FWIW: The script's action can be undone in one go.

                                     

                                    Regards,
                                    Uwe

                                    3 people found this helpful
                                    • 15. Re: Script not working in CC2018
                                      Liphou Level 2

                                      Hi Laubender, and thank you

                                      Actually I noticed that it was more prudent to search in GREP for not locating another "D".
                                      Thank you for the command ".clearCellStyleOverrides (true)" (it will serve me again),
                                      I did not search further, because forced cell style, here, removed the double lines of cell cards.
                                      Very good example of code thank you (I keep)

                                       

                                       

                                      Effectivement j'avais remarqué qu'il étais plus prudent de faire une recherche en GREP pour ne pas localisé d'autre "D".

                                      Merci pour la commande ".clearCellStyleOverrides( true )" (elle me servira encore),

                                      je n'avais pas recherche plus loin, car forcé le style de cellule, ici, va supprimé les double lignes des carde de cellule.

                                       

                                      Très bonne exemple de code merci ( je garde)

                                       

                                      Philippe

                                      • 16. Re: Script not working in CC2018
                                        graphicsguy85 Level 1

                                        I will try this as soon as I can get back to my computer, thank you and everyone else who has been so extremely helpful. This will save me hours of work if this works.

                                        • 17. Re: Script not working in CC2018
                                          Peter Kahrel Adobe Community Professional & MVP

                                          The way that Mac and Windows deal with addressing styles differs slightly. To avoid problems with applying styles, reference the object, don't use a string. So instead of this:

                                           

                                          myFound[i].parent.appliedCellStyle = "Green";

                                           

                                          use this:

                                           

                                          myFound[i].parent.appliedCellStyle = app.documents[0].cellStyles.item ("Green");

                                           

                                          P.

                                          1 person found this helpful
                                          • 18. Re: Script not working in CC2018
                                            graphicsguy85 Level 1

                                            I tried this in Indesign 12.1.0.56 mac 10.10.5 and it works, I will try on CC 2018 when I get home.  Thank you again!  Wherever you are in the world. I appreciate your help.

                                            • 19. Re: Script not working in CC2018
                                              graphicsguy85 Level 1

                                              Am I able to search for numbers with special characters like 1* or 2*

                                              • 20. Re: Script not working in CC2018
                                                Peter Kahrel Adobe Community Professional & MVP

                                                You can look for whole numbers using this:

                                                 

                                                app.findGrepPreferences.findWhat = '\\d+';

                                                 

                                                To match numbers with decinals and thousands separators, use this:

                                                 

                                                app.findGrepPreferences.findWhat = '\\d[\\d.,]+';

                                                • 21. Re: Script not working in CC2018
                                                  Laubender Adobe Community Professional & MVP

                                                  graphicsguy85  wrote

                                                   

                                                  Am I able to search for numbers with special characters like 1* or 2*

                                                  Do you mean you are looking for 1* as contents of a cell ?

                                                  Then you could do this:

                                                   

                                                  app.findGrepPreferences.findWhat = "^1\\*$";
                                                  

                                                   

                                                  Regards,
                                                  Uwe

                                                  • 22. Re: Script not working in CC2018
                                                    graphicsguy85 Level 1

                                                    Tank you again for the help, would you be so kind to assist me a little further?

                                                     

                                                    How would I search for the following:         (1)

                                                                                                                         (G*)

                                                     

                                                    I am having trouble for it searching for the Parentheses

                                                    • 23. Re: Script not working in CC2018
                                                      Peter Kahrel Adobe Community Professional & MVP

                                                      Look for '\\(1\\)'

                                                       

                                                      Parentheses, brackets, braces, asterisks, and a clutch of other symbols have a special meaning in Grep, so you need to escape them, as it's called. And in strings you need to escape the backslash because it too is a special character in Grep.

                                                       

                                                      Go on the net and read up on Grep. It's worth it.

                                                       

                                                      P.