5 Replies Latest reply on Nov 11, 2014 5:16 AM by Dirk Becker 

    Force show (not toggle) hidden characters and threads in script

    KnightD

      I have been working on a script to show all the extras (for the most part). I can turn on rulers and frame edges from the DOM, but can not Show Hidden Characters or Show Text Threads from the DOM. I've been working on using menuActions, but there two is a hitch. It seems that the menuActions are only available if the menu has been accessed during the current session? Not 100% on that. There is a good comment (#10) that touches on this a bit at Indiscripts :: How to Create your Own InDesign Menus.

          

      This method works, but again only if the menu has been previously accessed... so it's not reliable:

       

      // locale-independent (should work on non-english versions of InDesign)

      // Show Hidden Characters

      // FIX/TODO - Only seems to work if the Show/Hide Characters has been toggled within this session?!

      var showHiddenChars = app.menuActions.item("$ID/HideHiddenCharactersCmdStr"); //The 'hide' ID String only valid when characters are hidden

      var areCharsHidden = (showHiddenChars.isValid);

      if (areCharsHidden == true) {

          try {showHiddenChars.invoke()} catch (givenError){alert(givenError.toString())};

          }

      else {

              //alert("Hidden characters are already shown!")

          }

       

      I also have a less desirable version of Showing Hidden Characters, which relies on English name property of the Menu Action. It suffers from the same issue.

       

      //~     var acID = acID||

      //~             ((t=app.menuActions.item("$ID/ShowHiddenCharactersCmdStr")).isValid&&t.id)||

      //~             ((t=app.menuActions.item("$ID/HideHiddenCharactersCmdStr")).isValid&&t.id);

      //~     var showHiddenChars = app.menuActions.itemByID(acID);

      //~

      //~     //if the name property for the menuAction is currently "Hide Hidden Characters", the characters are hidden.

      //~     var areCharsHidden = (showHiddenChars.name == "Hide Hidden Characters");

      //~     if (areCharsHidden == true) {

      //~         try {showHiddenChars.invoke()} catch (givenError){};

      //~         }

      //~     else {

      //~         //alert("Hidden characters are already shown!")

      //~         }

       

      Any suggestion on how to force show characters (show threads works the same way) with out having to have had accessed a menu before hand?

      Thanks,

      Kevin

        • 1. Re: Force show (not toggle) hidden characters and threads in script
          cchimi Level 2

          Does app.textPreferences.showInvisible = true not work here?

          1 person found this helpful
          • 2. Re: Force show (not toggle) hidden characters and threads in script
            Dirk Becker  Level 4

            When you have found a menu action you can remember its ID and use menuActions.itemByID() later on to retrieve it. Most action IDs are hard wired, bound to their implementing plugin. Of course there is an exception for dynamically allocated actions, e.g. script actions.

             

            My first attempt following you is to retrieve the action name - that should trigger the internal method to update the whole action state including checked, enabled (not applicable in this case). Of course it failed, would someone please file a bug? The translated versions of strings that you'd use for the comparison are available via app.translateKeyString().

             

            Btw you are using the wrong string - cmd strings are used for command history as seen in the undo menu. Menu strings usually have an ampersand character to indicate the underline / Alt+Key combo in Windows.

             

            $.writeln(app.menuActions.itemByID(0x1d301).name==app.translateKeyString("$ID/Show Hidden Characters"));

            $.writeln(app.menuActions.itemByID(0x1d301).name==app.translateKeyString("$ID/Hide Hidden Characters"));

            app.translateKeyString("$ID/Show Hidden Characters")

            Ergebnis: Verborgene Zeic&hen einblenden

            app.translateKeyString("$ID/Hide Hidden Characters")

            Ergebnis: &Verborgene Zeichen ausblenden

             

            // failed test

            $.writeln(app.menuActions.itemByID(0x1d301).name);

            app.menuActions.itemByID(0x1d301).invoke();

            $.writeln(app.menuActions.itemByID(0x1d301).name);

            // output should change after invoke

            Verborgene Zeichen ausblenden

            Verborgene Zeichen ausblenden

             

            As you found command strings, I also had a look at the undo history - it remained empty.

            $.writeln(app.activeDocument.undoHistory.length);

            Probably the setting is not persisted in the document any more, someone should have removed the command strings.

             

            Then I realized that the output of the previous lines around the invoke had changed as it was expected originally. And I had not touched the menu since restart but I had activated the application.

            One can do that with scripting too - app.activate();

            A couple of restarts later this is definitely leading somewhere.


            Regarding the command, I also had a second look with a debug build of InDesign: the command is still executed, but there is a flag that binds its undo to the previous command - left as an exercise to verify.

             

            Hmm. It is stored in the persistent document preferences in a structure internally called kDocWorkspaceBoss / ITextOptions, thus should also be exposed for scripting. Back to scripting: set a breakpoint, browse thru tons of document sub-objects, and here we are:

            app.activeDocument.textPreferences.showInvisibles


            Happy programming,

            Dirk

             

            Edit: when comparing the strings, you also have to strip the extra ampersand.

            • 3. Re: Force show (not toggle) hidden characters and threads in script
              KnightD Level 1

              I thank you! That does work!

              • 4. Re: Force show (not toggle) hidden characters and threads in script
                KnightD Level 1

                Dirk, I use the itemByID() method in my second script, but was unsure if the ID numbers would change based on other users creating custom menu items... and also I'm fetching the ID based on what you are now telling me is the wrong string. I am usually using Windows, but in this instance am using Mac, so I wasn't sure if I would see the ampersand in the strings.

                You did a lot of digging and found more about the cause (not activating the app), so I thank you heartily. I am now getting the desired result and have a bit of insight into what I need to do for the nearly identical function I'm working on for showing hidden text threads (which still relies on menuAction).

                • 5. Re: Force show (not toggle) hidden characters and threads in script
                  Dirk Becker  Level 4

                  Kevin, that's why I shared a bit more than the final conclusion.

                   

                  For completeness sake, I just had a look at menu strings - those localizations come really handy! In the SDK there is a function to remove "accelerator" characters, and it has a comment that japanese menu strings also use other characters. It was not specified which, and the strip method is not exposed for scripting.