13 Replies Latest reply on Jul 13, 2017 3:53 PM by Laubender

    Reset findTextPreferences.appliedParagraphStyle?

    TᴀW Adobe Community Professional & MVP

      Hi,

       

      Does anyone know how to reset the findTextPreferences.appliedParagraphStyle value?

       

      A script I'm working on uses InDesign's findTextPreferences to search for a paragraph style, e.g.:

       

      app.findTextPreferences.appliedParagraphStyle = myDoc.allParagraphStyles[3]; // just an example

       

      Once the script finishes, I would like to reset the findTextPreferences back to what it was, so the user doesn't have the hassle of doing it.

       

      However, storing the paragraphStyle in a variable doesn't work, e.g.:

       

      myOldParaStyle = app.findTextPreferences.appliedParagraphStyle;

       

      because myOldParaStyle now contains a non-unique String object! (Tested in CS6). I never realised this before.

       

      For instance, say in your document you have a paragraph style called "SubheadA", and you have a paragraphStyleGroup called "Footnotes" and inside that group you also have a paragraph style called "SubheadA" -- i.e. you have two identically names paragraph styles in your document, the only difference being that one is in a group called "Footnotes."

       

      In both cases, myOldParaStyle will contain the String "SubheadA" -- so there's no way of knowing which paragraph style was actually selected.

       

      In a more regular case (where there are not 2 similarly-names paragraph styles in the document), you still can't know exacltly where myOldParaStyle is, because if it is in some nested paragraphStyleGroup you're going to have to loop through them all to find one called that.

       

      In brief: Does anyone know a fail-safe way of restoring the user's appliedParagraphStyle in the Find dialog, after a script changes those settings?

       

      Alternatively: Does anyone know a way of scripting findText() without actually affecting the Find/Change UI dialog?

       

      Many thanks,

      Ariel

       

      PS I would add that the following "almost" works, except for a handful of properties (including, unfortunately, the paragraph style):

       

      myOldProperties = app.findTextPreferences.properties;

      // do some findText() stuff with modified properties...

      // Now restore them:

      app.findTextPreferences.properties = myOldProperties;

       

      This is really quick and easy, and seems to work for 95% of properties -- almost a perfect solution! But when it comes to a few basics, like findTextPreferences.appliedParagraphStyle it lets us down. And there doesn't seem to be a way to do it manually either!

        • 1. Re: Reset findTextPreferences.appliedParagraphStyle?
          Laubender Adobe Community Professional & MVP

          @[Ariel] – you could work with the unique ID number of the paragraph style to identify it.

          That is working beyond paragraph style groups.

           

          var myDoc = app.documents[0];
          var myID =  myDoc.allParagraphStyles[3].id;
          var myParaStyle = myDoc.paragraphStyles.itemByID(myID);
          
          app.findTextPreferences.appliedParagraphStyle = myParaStyle;
          

           

          Uwe

          • 2. Re: Reset findTextPreferences.appliedParagraphStyle?
            TᴀW Adobe Community Professional & MVP

            Uwe,

             

            Thanks for chipping in. But the question I asked was the other way round:

             

            How do you get from findTextPreferences.appliedParagraphStyle to an actual paragraphStyle object?

             

            myParaStyle = app.findTextPreferences.appliedParagraphStyle.id

             

            returns "undefined", because

             

            app.findTextPreferences.appliedParagraphStyle.constructor.name

             

            returns

             

            String

             

            !

             

            Ariel

            • 3. Re: Reset findTextPreferences.appliedParagraphStyle?
              Laubender Adobe Community Professional & MVP

              Ok. I misunderstood. Now it's clear to me. Let's see:

               

              1. "undefined" is the default for an appliedParagraphStyle in findTextPreferences, if the search is not restricted to a paragraph style

               

              2. If the search is restricted to a specific paragraph style we get a string in return, istead of a paragraph style object.

               

              What to do?
              We could temporarily rename all paragraph styles adding their unique ID number and ask again.

               

              Uwe

              • 4. Re: Reset findTextPreferences.appliedParagraphStyle?
                Laubender Adobe Community Professional & MVP

                My idea is not working.

                findTextPreferences will not pick up the new name.
                Sorry.

                 

                Uwe

                • 5. Re: Reset findTextPreferences.appliedParagraphStyle?
                  Laubender Adobe Community Professional & MVP

                  So. we could list all paragraph styles (should be a short list) that have the same name according to the string that findTextPreferences returns.

                   

                  We now could build some new text frames, each with a single character added and formatted with one of the paragraph styles in the list.

                   

                  Now we do a TEXT search on these text frames and sort out the right paragraph style with its unique ID and store it for further use.

                   

                  The result for a single search is either of length 0 or of length 1 (the right one).

                   

                  Seems awkward, but should work…

                   

                  Uwe

                  • 6. Re: Reset findTextPreferences.appliedParagraphStyle?
                    TᴀW Adobe Community Professional & MVP

                    Hi Uwe,

                     

                    Sorry -- I was being jived right out of the forums. Couldn't respond by email, couldn't log in via web.*

                     

                    Thanks for your post. Even if it did pick it up, I tried it on a doc with 55 paragraph styles and it's taking over 13 seconds! (And afterwards I'll have to rename them all again = 26 seconds!). Any other ideas?

                     

                    Here 's the test script:

                     

                    renumber();

                    function renumber(){

                        myDoc = app.activeDocument;

                        myStyles = app.activeDocument.allParagraphStyles;

                        for (i = 2; i<myStyles.length; i++){

                            myStyles[i].name= myStyles[i].name+"ID"+myStyles[i].id;

                        }

                    }

                     

                    Thanks,

                    Ariel

                     

                    * PS Just tried on Chrome and it's working. So something's up with Internet Explorer 10. Oh well...

                    • 7. Re: Reset findTextPreferences.appliedParagraphStyle?
                      TᴀW Adobe Community Professional & MVP

                      That's a doable workaround, but it is pretty awkward. Also, it would need to be repeated with the character style property.

                       

                      Actually, I'm not even sure if it's doable -- we have to assume that the findTextPreferences has other settings applied. So the characters we add to the newly created textFrame would have to have all the findTextPreferences properties applied to it, PLUS each possible paragraph style. Otherwise, when we try to find it, we won't (if it does not match all specified Find properties).

                       

                      But anyway, it's too slow.

                       

                      Seems like Adobe was cutting corners here. There's no earthly reason that app.findTextPreferences.appliedParagraphStyle should return a string instead of a paragraph style, or, if a string, at least a string qualified with the group name!

                       

                      Is there any way of avoiding the Find/Change UI to be altered?

                       

                      In my case, I just need to find text with a paragraph style -- so perhaps it's simpler to implement a custom "find text with paragraphStyle X" applied. The thing is, it's going to be much, much slower than findText()...

                       

                      Ariel

                      • 8. Re: Reset findTextPreferences.appliedParagraphStyle?
                        Laubender Adobe Community Professional & MVP

                        Another idea would be to save the query and load it back again using InDesign's built in functions. But it seems there is no dedicated menu action available for this, thus a dead end here again.

                         

                        Uwe

                        1 person found this helpful
                        • 9. Re: Reset findTextPreferences.appliedParagraphStyle?
                          TᴀW Adobe Community Professional & MVP

                          I think we have something here! We must not underestimate the DOM, for we have been given:

                           

                          Application.saveFindChangeQuery (queryName: string , searchMode: SearchModes )

                          Adobe InDesign CS6 (8.0) Object Model

                          Saves the specified find/change query.

                          queryName: Data Type: string

                          The query to save.

                          searchMode: Data Type: SearchModes

                          The search mode.

                           

                          This looks very hopeful. I'm going to check this out in detail.

                           

                          Excellent Uwe, thank you!

                           

                          Ariel

                          • 10. Re: Reset findTextPreferences.appliedParagraphStyle?
                            TᴀW Adobe Community Professional & MVP

                            So, saving a findChange query takes around 1/1000 sec on my computer, and loading it takes about 4 times longer. So that's a feasible workaround -- better than anything else I think.

                             

                            But, this question is still open: Is there any way to run findText() and change app.findTextPreferences inside a script WITHOUT affecting the Find/Change UI at all? Is there some way to bypass or disconnect a script find/change from the UI find/change?

                             

                            Thanks,

                            Ariel

                            • 11. Re: Reset findTextPreferences.appliedParagraphStyle?
                              Laubender Adobe Community Professional & MVP

                              @[Ariel] – thank you for pointing this out. Of course using app.saveFindChangeQuery() is the answer for one part of your problems.

                               

                              The other one about using the app.findTextPreferences (or anything around that) without afftecting the UI, I have no positive answer. It's tied directly to the UI. Unfortunately to the app and not to the document class (that would be the workaround).

                               

                              Thinking out of that box: If there are two different InDesign installations available, you could swap the task to the other InDesign app, coming back to your original later. An "esoteric" solution…

                               

                              Uwe

                              • 12. Re: Reset findTextPreferences.appliedParagraphStyle?
                                Ivo Lima

                                Hi,

                                 

                                I use this code for find text that use a style inside a goup style:

                                 

                                app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.itemByName("lists").paragraphStyles.itemByName("l ist_abc");

                                • 13. Re: Reset findTextPreferences.appliedParagraphStyle?
                                  Laubender Adobe Community Professional & MVP

                                  Hi Ivo,

                                  Ariel's initial question was:

                                   

                                  "In brief: Does anyone know a fail-safe way of restoring the user's appliedParagraphStyle in the Find dialog, after a script changes those settings?"

                                   

                                  The answer was in reply #9.

                                   

                                  app.saveFindChangeQuery()

                                  Adobe InDesign CS6 (8.0) Object Model JS: Application

                                   

                                  do your script, then:

                                  app.loadFindChangeQuery()

                                  Adobe InDesign CS6 (8.0) Object Model JS: Application

                                   

                                  followed by:

                                  app.deleteFindChangeQuery()

                                  ( to leave no traces… )

                                  Adobe InDesign CS6 (8.0) Object Model JS: Application

                                   

                                  I cite Ariel again:

                                  "But, this question is still open: Is there any way to run findText() and change app.findTextPreferences inside a script WITHOUT affecting the Find/Change UI at all? Is there some way to bypass or disconnect a script find/change from the UI find/change?"

                                   

                                  The answer for this still is:

                                  No. That's not possible.

                                   

                                  Regards,

                                  Uwe