11 Replies Latest reply on Mar 20, 2012 9:05 PM by Dan Rodney

    [JS] How Do You Move Guides to Another Page?

    Dan Rodney Adobe Community Professional

      I'm having a problem moving guides to a master page (or back to a document page). I move a graphic line, but not a guide. Guides must be different, but I'm not sure how.

       

      Below is a test script which:

      1. Moves a graphic line (with a specific script label) onto a master page. That part works.

      2. Tries to move a guide (with a specific script label) onto a master page. It's written the same way as the graphic line, but it doesn't work.

       

      What am I missing? FYI: I'd like this to be able to work in CS3 through CS5.

      I'd like to keep the original position of the guide, but as long as you can help me figure out how to get it onto the proper page, I should be able set it's position.

       

      Thanks in advance,

      Dan

       

       

      var myFoundLines = app.activeDocument.graphicLines ;
      var myFoundGuides = app.activeDocument.guides ;
      
      for ( i = 0; i < myFoundLines.length; i++ ) {
         if ( myFoundLines[i].label == "mySpecialLine")  {
            var myLine = myFoundLines[i]
            myLine.move( app.activeDocument.masterSpreads.item(0).pages.item(0) )  // move to the master page 
            // I can reset the line's position here
         }  
      }
      
      
      for ( i = 0; i < myFoundGuides.length; i++ ) {
         if ( myFoundGuides[i].label == "mySpecialGuide")  {
            var myGuide = myFoundGuides[i]
            myGuide.move( app.activeDocument.masterSpreads.item(0).pages.item(0) )  // move to the master page (not working)
         }  
      }
      
        • 1. Re: [JS] How Do You Move Guides to Another Page?
          Dan Rodney Adobe Community Professional

          Reading the scripting reference for Guides, it doesn't mention pages like it does for pageItems. Can it be that you can't move guides between pages? Do I have to delete the guide from one page and recreate a brand new guide on another, resetting all of its options?

          • 2. Re: [JS] How Do You Move Guides to Another Page?
            Harbs. Level 6

            Looks like it.

             

            FWIW, it's not a big deal to copy over the options:

             

            myPage.guides.add(undefined,myGuide.properties);

            myGuide.remove();

             

            (Just make sure your ruler guides are set to "rulers per page" if you're copying from page 1...)

             

            Harbs

            1 person found this helpful
            • 3. Re: [JS] How Do You Move Guides to Another Page?
              Laubender Adobe Community Professional & MVP

              @Dan – as your experimentation and referencing the documentation shows, yes:

               

              there are limited options for moving guides around. Also duplicate() is limited in comparison to other page objects like graphic lines, rectangles etc.pp.

               

              Storing the guide's properties and adding a new one on another page with the stored properties would be the only option; so it seems at least…

               

              With duplicate() I did a few tests. Strange enough is the error handling:

               

              //Prerequisite: one brand new doublesided document with 1 page
              var d = app.documents[0];
              var firstGuide = d.pages[0].guides.add({
              
                  orientation:HorizontalOrVertical.VERTICAL,
                  location:100
              
                  });
              
              //What's wrong with this target?
              //the duplicate() method permits no pages.
              //Your dup guide will land on the same page as the original:
              
              var dupGuide1 = firstGuide.duplicate(d.masterSpreads[0].pages[0]);
              dupGuide1.location = 110;
              
              
              //Strange enough, throws NO error (pages[99] should be NOT possible at all)!
              //Your dup guide will land on the same page as the original:
              
              var dupGuide2 = firstGuide.duplicate(d.masterSpreads[0].pages[99]);
              dupGuide2.location = 120;
              
              
              //Throws an error, because there is only one regular master spread:
              
              try{
              var dupGuide3 = firstGuide.duplicate(d.masterSpreads[1].pages[0]);
              dupGuide3.location = 130;
              }catch(e){alert(e.message)};
              

               

               

              Uwe

              1 person found this helpful
              • 4. Re: [JS] How Do You Move Guides to Another Page?
                csm_phil Level 4

                Hi Dan Rodney,

                 

                Can you please try the below js code and i have use my tricks. Harbs and Laubender is great developer we all are know.

                 

                 

                var myDoc = app.activeDocument ;
                 try{
                      var myFoundGuides = myDoc.guides;
                     for (var i=myFoundGuides.length-1; i>=0; i--){
                          var myLine = myFoundGuides[i];
                          //alert(myLine.parent.parent);
                          if(myLine.parent.parent.constructor.name !="MasterSpread"){
                               if (myLine.label == "mySpecialGuide"){
                                    app.select(myLine);
                                    app.cut();// if you like copy use this or else u need to cut this guide use app.cut();
                                    var myPage = app.layoutWindows[0].activeSpread = myDoc.masterSpreads.item(0); // if you need specific masterSpreads of page 0 or 1  u change it.
                                    app.pasteInPlace(myPage);
                                    }
                               }
                          }
                     }catch(e){alert(e)};
                

                 

                 

                thx

                csm_phil

                • 5. Re: [JS] How Do You Move Guides to Another Page?
                  Dan Rodney Adobe Community Professional

                  @csm_phil Your script does work, but I have a lot of guides to move. I'd think the performance of changing the layoutWindow that many times wouldn't be as good as what Harbs is suggesting.

                  • 6. Re: [JS] How Do You Move Guides to Another Page?
                    csm_phil Level 4

                    Hi Dan,

                     

                    Thank You for ur comments, any way u r request has been solved.

                     

                    thx

                    csm_phil

                    • 7. Re: [JS] How Do You Move Guides to Another Page?
                      [Jongware] Most Valuable Participant

                      Here's a method: per each of your Found guides, add any ol' guide to the master page, then set its properties to be the same as the found one.

                       

                      There are two catches you might want to know about. First of all, this is not a 'move' but a 'copy' operation. If you want a 'move', you need to delete the original guide after copying its properties. But if you do so, the contents and length of the array 'myFoundGuides' changes, so your iterating variable 'i' will point to something else then you're expecting. The common workaround for that is to process your found list from last to first item, so any deletions, or other changes, do not affect the not-yet-done items.

                       

                      The second catch is a bit more complicated one. Variable 'myFoundGuides' is a direct pointer to the active doc's guides -- all of them. That means that also if you add a guide, the internal document guides' list is adjusted, and since 'myFoundGuides' points to this list, your new guide will also appear in it -- even though you think you are processing "just the ones you just found". How to solve it depends on what you do with this list; usually I don't care for any side-effects but in this case it was, uh, "interesting".

                       

                      I wrote a quick script the way I thought it ought to work, and for sake of testing I added 1 (one) guide to my document and ran my script. It didn't do nothing (I was expecting it to, with just a plain un-labeled guide -- always test!). Then I added the script label "mySpecialGuide" to that one guide, and ran the script again. It looped infinitally, adding guides by the thousands to my poor master page (and exactly on top of eachother as well), until I was able to stop the madness by hitting Escape.

                       

                      The length of 'myFoundGuides' was 1 -- the original guide. With index #0 and the appropriate label, this guide got copied and pasted onto the master page, including all of its properties. Then there were two guides, and the one at consideration, at index #1 (never mind if it was the original one or the copy) had a label "mySpecialGuide", so it got copied and pasted on the master page. Then there were three guides, and the one at the next used index -- #2 -- had a label "mySpecialGuide" .. etc. etc. etc. etc.

                      Changing the label to something else on the copy solved this

                       

                      The below script works, but mind you are still interfering with the complete collection of guides, so you need to re-write it anyway to work from back-to-front.

                       

                      var myFoundGuides = app.activeDocument.guides;
                      for (i=0; i<myFoundGuides.length; i++ )
                      {
                         if ( myFoundGuides[i].label == "mySpecialGuide")
                         {
                            myGuide = app.activeDocument.masterSpreads.item(0).pages.item(0).guides.add();
                            myGuide.properties = myFoundGuides[i].properties;
                            myGuide.label = 'hah!';
                         }
                      }
                      

                       

                      I cannot say for sure this works in CS3; it does in CS4.

                      • 8. Re: [JS] How Do You Move Guides to Another Page?
                        Harbs. Level 6

                        No reason to complicate things...

                         

                        var myFoundGuides = app.activeDocument.guides.everyItem().getElements();

                         

                        gives you a static array of guides...

                        • 9. Re: [JS] How Do You Move Guides to Another Page?
                          [Jongware] Most Valuable Participant

                          Hi Harbs,

                           

                          does that solve the problem of deleting/adding guides as well as the magic self-repeating properties I got?

                           

                          (Sorry, I was typing this *very* long answer before I noticed you already proposed just about the same -- in much shorter proze as well.)

                          • 10. Re: [JS] How Do You Move Guides to Another Page?
                            Laubender Adobe Community Professional & MVP

                            @Jongware – if in doubt you could address the guides by their ID.

                             

                            As a starting point for reference we could use an array with ID numbers:

                             

                            var myOriginalGuidesID = app.activeDocument.guides.everyItem().id;
                            for (i=0; i<myOriginalGuidesID.length; i++ )
                            {
                               if ( app.activeDocument.guides.itemByID(myOriginalGuidesID[i]).label == "mySpecialGuide")
                               {
                                  myGuide = app.activeDocument.masterSpreads.item(0).pages.item(0).guides.add();
                                  myGuide.properties = app.activeDocument.guides.itemByID(myOriginalGuidesID[i]).properties;
                                  //myGuide.label = 'hah!';
                               }
                            }
                            

                             

                            Uwe

                            • 11. Re: [JS] How Do You Move Guides to Another Page?
                              Dan Rodney Adobe Community Professional

                              Thanks everyone. It seems like I'll be able to get something working based on all of your comments. I'll report back once I have some time to test all of this out. In the meantime, thanks again! You've been so helpful once again.