8 Replies Latest reply on Aug 1, 2015 7:57 AM by W_J_T

    Show/Hide guides via JavaScript?

    victorcarbajo

      Show/Hide guides via JavaScript?

       

      With this script I can delete all guides in all open documents:

       

      for (d = 0; d < app.documents.length; d++) {

        var targetDocument = app.documents[d];

        activeDocument = targetDocument;

        var doc = app.activeDocument;

        for (var i = doc.pathItems.length-1; i >= 0; i--) {

        var p = doc.pathItems[i];

        if (p.guides == true) {

        p.remove();

        }

        }

      }

       

      But, how can I modify this script for show/hide guides?

       

      Thanks.

        • 1. Re: Show/Hide guides via JavaScript?
          W_J_T Level 4

          victorcarbajo wrote:  Show/Hide guides via JavaScript? How can I modify this script for show/hide guides?


          The following should work:

           

          // Replace:
          p.remove();
          // With the following:
          p.hidden = !p.hidden;
          
          

           

          Or, if you have CS6 or above you could consider trying executeMenuCommand:

           

          // CS6 and above you can try... (not tested)
          app.executeMenuCommand('showguide');
          
          • 2. Re: Show/Hide guides via JavaScript?
            victorcarbajo Level 1

            Thanks W_J_T for your response.

             

            "app.executeMenuCommand('showguide');" works correctly, but "p.hidden = !p.hidden;" or "p.hidden = true" deletes (?!) the guides.

             

            Which is the "app.executeMenuCommand" for hidden guides?

             

            Thanks.

            • 3. Re: Show/Hide guides via JavaScript?
              W_J_T Level 4

              victorcarbajo wrote:

               

              "p.hidden = !p.hidden;" or "p.hidden = true" deletes (?!) the guides.

               

              I just tested it with the code you posted, it works fine for me. Did you delete the line: p.remove() and replace it with the one I posted above? p.hidden = !p.hidden;

               

              As for the executeMenuCommand I would assume each time that its called it would toggle between the present state, but not sure, I don't have CS6+ to test it.

              • 4. Re: Show/Hide guides via JavaScript?
                victorcarbajo Level 1

                Thanks W_J_T,


                Effectively, the executeMenuCommand toggle between the show/hide state.


                In my case (I use Ilustrador CC 2015), this code deletes the guides:


                for (d = 0; d < app.documents.length; d++) {

                  var targetDocument = app.documents[d];

                  activeDocument = targetDocument;

                  var doc = app.activeDocument;

                  for (var i = doc.pathItems.length-1; i >= 0; i--) {

                  var p = doc.pathItems[i];

                  if (p.guides == true) {

                  p.hidden = !p.hidden;

                  }

                  }

                }

                • 5. Re: Show/Hide guides via JavaScript?
                  W_J_T Level 4

                  You're welcome, glad the executeMenuCommand was helpful to your efforts.

                  victorcarbajo wrote: In my case (I use Ilustrador CC 2015), this code deletes the guides:


                  I do not have CC to test it in, but you can try restarting Illustrator, then wrapping the code in a function to be called. See example below:


                  function tG() {
                      var doc, p;
                      for (var d = 0; d < app.documents.length; d++) {
                          doc = app.documents[d];
                          for (var i = doc.pathItems.length - 1; i >= 0; i--) {
                              p = doc.pathItems[i];
                              if (p.guides == true) {
                                  p.hidden = !p.hidden;
                              }
                          }
                      }
                  }
                  tG();
                  


                  Aside from those two things, * plus cleaning up the code you posted a little, I'm not sure why it would not be compliant in CC and instead be deleting the guides? That sounds rather strange.

                  • 6. Re: Show/Hide guides via JavaScript?
                    CarlosCanto Adobe Community Professional & MVP

                    your code does work in CC

                    • 7. Re: Show/Hide guides via JavaScript?
                      victorcarbajo Level 1

                      I don't know what happens in my Illustrator, but your code deletes the guide lines. I restart several times with the same result.

                       

                      I use your other code for hide/show guides:

                       

                      for (d = 0; d < app.documents.length; d++) {

                        var targetDocument = app.documents[d];

                        activeDocument = targetDocument;

                        app.executeMenuCommand('showguide');

                      }

                       

                      With best regards.

                      • 8. Re: Show/Hide guides via JavaScript?
                        W_J_T Level 4

                        Thanks for checking/confirming CarlosCanto.