12 Replies Latest reply on Apr 13, 2016 3:55 AM by Suzan.V

    lock position

    Suzan.V Level 1

      Hi all,

       

      i just want to know the code for (lock position) action (Ctrl+L) to all text frames and objects, not lock layer

       

      i tried both below codes but it only lock layers and i want to lock positions

       

      app.activeDocument.textFrames.everyItem().locked = true; 

       

      app.documents[0].layers.everyItem().locked = true;

       

      Thanks

        • 1. Re: lock position
          b91823603 Level 3

          Hi Suzan,

           

          You could try this,

           

          var pgItems = app.activeDocument.allPageItems;

          for(var i=0;i<pgItems.length;i++)

          {

              if(pgItems[i] instanceof TextFrame)

              pgItems[i].locked = true;

          }

          • 2. Re: lock position
            tpk1982 Level 4

            b91823603,


            he asked "all text frames and objects", so no need of instance checking


            var pgItems = app.activeDocument.allPageItems;

            for(var i=0;i<pgItems.length;i++)

            {

                pgItems[i].locked = true;

            }

            • 3. Re: lock position
              [Jongware] Most Valuable Participant

              ? This worked for me:

               

              app.activeDocument.textFrames.everyItem().locked = true;

               

              It locked every text frame in the entire document, including ones on master pages and on the pasteboard. It did nothing to layers.

              I did not try

               

              app.documents[0].layers.everyItem().locked = true;

               

              because, well, that would lock layers and you don't want that. (So why try?)

              • 4. Re: lock position
                Suzan.V Level 1

                i got this error and nothing is locked yet !!

                 

                1.png

                • 5. Re: lock position
                  tpk1982 Level 4

                  use this,

                   

                  app.activeDocument.pageItems.everyItem().locked = true

                  • 6. Re: lock position
                    Laubender Adobe Community Professional & MVP

                    [Jongware] wrote:

                     

                    ? This worked for me:

                     

                    app.activeDocument.textFrames.everyItem().locked = true;

                     

                    It locked every text frame in the entire document, including ones on master pages and on the pasteboard. It did nothing to layers.

                    I did not try

                     

                    app.documents[0].layers.everyItem().locked = true;

                     

                    because, well, that would lock layers and you don't want that. (So why try?)

                     

                    Hi Jongware,

                     

                    just a note:

                    There is a distinction between using the allPageItems array or pageItems.everyItem() for locking the items.
                    In case the user has set the preferences for locking objects on the page to still selectable, I'd use the allPageItems array.
                    That would include all nested or anchored pageItems to the locked state.

                     

                    Uwe

                    • 7. Re: lock position
                      Laubender Adobe Community Professional & MVP

                      Hi Suzan,

                      yes, there might be situations where you cannot apply a locked state to a pageItem.
                      We have to find out what exactly is provoking this.

                       

                      Anchored pageItems in overset text perhaps?
                      Anchored pageItems in tables?
                      Graphic frames in graphic cells?

                       

                      Best would be to tell us more about your document you are running the script at.

                       

                      Preventing the error and not locking pageItems, that are not lockable in the "current state" is easy.
                      We could also write a protocol what kind of items that would be:

                       

                      var allPageItemsArray = app.documents[0].allPageItems;
                      var length = allPageItemsArray.length;
                      
                      for(var n=0;n<length;n++)
                      {
                          try{
                          allPageItemsArray[n].locked = true ;
                          }catch(e)
                          {
                              // Write a protocol to the JavaScript Console:
                              $.writeln
                              (
                                  // index number
                                  n+"\t"+
                                  // what it is exactly:
                                  allPageItemsArray[n].getElements()[0]+"\t"+
                                  // Null, undefined or the parent page
                                  allPageItemsArray[n].parentPage+"\t"+
                                  // The parent of the page item in the hierarchy
                                  // E.g. A character would indicate an anchored object
                                  allPageItemsArray[n].parent+"\t"+
                                  // Could be anything:
                                  allPageItemsArray[n].parent.parent
                              );
                          }
                      }
                      

                       

                      I just tested this snippet on a document that is containing a multi-state object (MSO) and the log is looking like that:

                       

                      1    [object Group]    [object Page]    [object MultiStateObject]    [object Spread]
                      2    [object Rectangle]    [object Page]    [object Group]    [object MultiStateObject]
                      

                       

                      One of the reasons, that the script is not working as expected is:
                      The part of a state, that is a group inside of an MSO cannot be locked.
                      All contents of an MSO cannot be locked individually. It is automatically locked, if the whole MSO is locked.
                      Therefore a gray lock symbol is visible in the layers panel:

                       

                      LockedMSO-LayersPanel.png

                       

                      I guess it would be the same with buttons on a page.

                       

                      Regards,

                      Uwe

                      • 8. Re: lock position
                        Laubender Adobe Community Professional & MVP

                        But the most simple reason is a placed graphic.

                        The container of that graphic can be locked, but not the graphic itself.
                        And this is part of the allPageItems array.

                         

                        Uwe

                        • 9. Re: lock position
                          Suzan.V Level 1

                          Dear ( tpk1982  & Laubender )

                           

                          I'm so grateful for your help and both codes are working perfectly, but i don't know correct answer to whom

                           

                          anyway thanks a lot and please let me know mark answered to whom

                          • 10. Re: lock position
                            Laubender Adobe Community Professional & MVP

                            Hi Suzan,

                            the difference between using:

                             

                            app.documents[0].allPageItems
                            /*etc.pp.*/
                            
                            

                             

                            and:

                             

                            app.documents[0].pageItems.everyItem().locked == true;
                            
                            

                             

                            is very important.

                             

                            The latter is locking all page items on the "surface".
                            Not the anchored ones for example.

                             

                            So, if a user sets the preferences for locked items to selectable, he could move anchored items.
                            Even if the parent text frame is locked!

                             

                            Using allPageItems with a try/catch is also locking anchored objects.
                            It's this situation (from my German InDesign) using the "surface" pageItems:

                             

                            AnchoredItems-Not-Locked.png

                             

                            against this one that is using allPageItems :

                             

                            AnchoredItems-Locked.png

                             

                            And as you can see here, I have the text frame selected, because I set the preferences accordingly.
                            In the scenario of the first screen shot I could change the position of the anchored rectangle.


                            In the second scenario I would not be able to do that.

                            (At least until I go to the Layers Panel and unlock the object or set all objects on the spread unlocked with a menu command)

                             

                            Regards,

                            Uwe

                             

                            // EDIT: Some additional text to describe the two situations more clearly. At least I hope so…

                            • 11. Re: lock position
                              tpk1982 Level 4

                              Suzan, Laubender always rocks with prediction of future problem.. he is a great mentor in adobe forum.. please give the credit to him

                              • 12. Re: lock position
                                Suzan.V Level 1

                                Done, and really your support is highly appreaicated