6 Replies Latest reply on Aug 14, 2011 5:30 AM by beh_gras_beh

    ObjectStyle with ParagraphStyle

    beh_gras_beh Level 1

      Dear people,

       

      Is there a way to find a ParagraphStyle and give it a ObjectStyle trough scripting.

       

      So,

      1. find ParagraphStyle "Paragraph01" in textframe,

      2. give textframe ObjectStyle "ObjectStyle1"

       

       

      Already thanks for the help!

       

      Greatings from Holland!

        • 1. Re: ObjectStyle with ParagraphStyle
          John Hawkinson Level 5

          Sure. You can loop over all TextFrames in the document, then loop over all their constitutent pargraphs and see if it matches your style, and if so apply the object style to the TextFrame.

          1 person found this helpful
          • 2. Re: ObjectStyle with ParagraphStyle
            beh_gras_beh Level 1

            Alright thanks,

             

            Didn't know if it was possible, I will puzzle the script togheter.

            • 3. Re: ObjectStyle with ParagraphStyle
              beh_gras_beh Level 1

              I'm still doing things wrong.

              The objectstyle is not aplied to the textframe with a paragraphstyle.

              Do you know what's wrong?

               

              main();

              function main(){

                  if (app.documents.length!=0){

                      myStyleDialog();

                      }

               

                  else {

                      alert ("Please open a document and try again.");

                      exit(0);

                  }

              }

               

               

               

              function myStyleDialog(){

              var myLabelWidth = 120;

              var myParaStyleNames = myGetParagraphStyleNames();

              var myObjStyleNames = myGetObjectStyleNames();

              var myDialog = app.dialogs.add({name:"Choose Styles"});

              with(myDialog.dialogColumns.add()){

                  with(borderPanels.add()){

                      with(dialogColumns.add()){

                          with(dialogRows.add()){

                              with(dialogColumns.add()){

                                  staticTexts.add({staticLabel:"Paragraph Styles:", minWidth:myLabelWidth});

                              }

                              with(dialogColumns.add()){

                                  myParaStyleDropdown = dropdowns.add({stringList:myParaStyleNames, selectedIndex:0});

                                  }

                              }

                                  with(dialogRows.add()){

                                      with(dialogColumns.add()){

                                          staticTexts.add({staticLabel:"Object Styles:", minWidth:myLabelWidth});

                                          }

                                      with(dialogColumns.add()){

                                          myObjectStyleDropdown = dropdowns.add({stringList:myObjStyleNames, selectedIndex:0});

                          }

                      }

                  }

              }

                  myReturn = myDialog.show();

                  if(myReturn == true){

                       myParagraphStyle = myParaStyleNames[myParaStyleDropdown.selectedIndex];

                       myObjectStyle = myObjStyleNames[myObjectStyleDropdown.selectedIndex];

                       myDialog.destroy();

                       }

                    else {

                    myDialog.destroy();

                    }  

                  }

              }

              function myGetParagraphStyleNames(){

                  var myStyleNames = app.documents.item(0).paragraphStyles.everyItem().name;

                  return myStyleNames;

              }   

              function myGetObjectStyleNames(){

                  var myObjStyleNames = app.documents.item(0).objectStyles.everyItem().name;

                  return myObjStyleNames;

              }

               

               

               

              var myDoc = app.documents[0];

              var myLib = app.libraries[0];

              app.findTextPreferences = NothingEnum.nothing;

              app.changeTextPreferences = NothingEnum.nothing;

              app.findTextPreferences = null;

              app.findTextPreferences.appliedParagraphStyle=myParagraphStyle;

               

               

               

              myFind = myDoc.findText(true);

               

              var myCount=0;

               

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

               

              {

               

              myText = app.findTextPreferences.appliedParagraphStyle=myParagraphStyle;

               

              myText.contents=myFind[i].contents;

               

              myText.appliedObjectStyle = myDoc.objectStyles.item(myObjectStyle);

               

               

              }

              • 4. Re: ObjectStyle with ParagraphStyle
                [Jongware] Most Valuable Participant

                (Please use the Javascript Syntax Highlighting from the tool bar to make your code more readable -- click the ">>" icon, then choose "Syntax highlighting", then "Java". (Yes I know, "Java is NOT Javascript", but Jive doesn't know and doesn't care.))

                 

                This is plain Wrong:

                 

                for (i=0; i<myFind.length; i++)
                
                { myText = app.findTextPreferences.appliedParagraphStyle=myParagraphStyle;
                 myText.contents=myFind[i].contents;
                 myText.appliedObjectStyle = myDoc.objectStyles.item(myObjectStyle);
                }
                
                

                 

                Why are you setting the variable myText to point to a Paragraph style? Why are you changing the 'contents' of this Paragraph Style to the found text contents? Why are you attempting to set an Object Style to a Paragraph style? Neither of these three operations make any sense at all.

                 

                Throw these 3 lines away, and use something like this:

                 

                myFind[i].parentTextFrames[0].appliedObjectStyle = myDoc.objectStyles.item(myObjectStyle);
                
                

                 

                Untested but sure to edge closer to your intentions.

                • 5. Re: ObjectStyle with ParagraphStyle
                  John Hawkinson Level 5

                  Well, I think Jongware has you on the right track, so you're probably done here.

                   

                  I'll note that your approach is definitely not what I recommended. Perhaps it is better to use the .findText() function to pick out the paragraphs -- it's probably a higher performance way to search the document. I had suggested looping over the textframes. One minor disadvantage of your approach is that if there are multiple paragraphs with sought-after style in the same frame, it will set the object style multiple times. This is probably not a big deal, and it's probably still faster than what I proposed.

                   

                  I would suggest, though, that when you're trying to figure out how to do something, building a fancy UI to do it is the last thing you need to do. The operative part of your script is really less than ten lines long. But most of your script is focusing on UI issues. That ... makes it take a lot more time to read though your script and see what is going on (which means it is harder to help you!), and if you spent a lot of time working on the UI, well, then it has to be frustrating for you as well to not have good results.

                   

                  In general, I think you are better off hard-coding the styles you are searching for and getting the actual guts of the script working, and then bolting the UI on later. Both from a functional perspective (you'll get something that works and you just have to keep it working), and also in terms of making it easier to help you.

                  1 person found this helpful
                  • 6. Re: ObjectStyle with ParagraphStyle
                    beh_gras_beh Level 1

                     

                    @Jongware

                     

                    Thank you very much for you’re explanation about the JavaScript Syntax Highlighting.

                    John explained it to me earlier.

                    I did understand him only I didn't exactly see how I could do the Highlighting.

                     

                    Than, of course, I have to do the thank-you-thing, because you solved the problem very well!

                     

                    @John

                    Also thank you for you're time and effort to reply my questions.

                    The thirst thing I will sort out.

                     

                    Second, Jongware and you noticed that it wasn't making any sense what the code did, and that's exactly what you pointed out to me.

                    I'm trying very hard to learn JavaScript and doing that in my spare time what I have left.

                    My work is trying to give me the time to learn some JavaScript but the standard work is always way to much so there is no time for it.

                    Than there comes that I have to script things for allot of other users who don't even noticed there is a script panel in InDesign.

                    And that’s why I want it to make it very simple and putting way to much time to make the UI right.

                    So you’re right, it was late, most of the time was the UI and with my sleepy head I didn't know what I was doing.

                     

                    Again, thank you very much for the help.

                    I have allot of reading and testing to do!