6 Replies Latest reply on Oct 21, 2014 11:22 AM by beto correa

    How to group a text frame to graphic frame trough scripting?

    vjunior

      I'm using LabelGraphics.jsx (modified) but I would like to add one more feature to the scripting. The script add a text frame to the graphics selected, but I need most of the time group the text frame to the graphic. How to do that? Maybe I can add a check box to dialog box in order to group.

       

      I'm just starting to learn scripting.

       

      Thanks.

        • 1. Re: How to group a text frame to graphic frame trough scripting?
          [Jongware] Most Valuable Participant

          You'll have to disable a part of the original script, because you cannot group objects on different layers. The original script creates a new layer and uses that as a reference in

           

          myFrame.parent.textFrames.add(myLabelLayer, ...

           

          Delete the italic variable and replace it with 'undefined'. (A more secure way would be to use the layer the original graphic is on!)

          Also delete the "Create label layer" stuff higher up in the "myAddLabel" function, because creating a new layer seems to automatically make it the default.

          If you run the script now, you will see the label frame gets created on your active document layer.

           

          Next: adding a check box.

          The script begins with defining the dialog box. At the bottom of the defining function (the "with(myDialog.dialogColumns.add()" braced part), add this

           

          //Group Checkbox

          with(dialogRows.add()){

          var myGroupCheckbox = checkboxControls.add({staticLabel:"Group"});

          }

          Run the script and verify there is a new a checkbox at the bottom. It doesn't do anything yet, that's the next part.

           

          To grab the checked/unchecked state of the checkbox, add this line in the "if(myResult == true)" block before the dialog destroy line:

           

          var myDoGroup = myGroupCheckbox.checkedState;

          "myDoGroup" will now hold a boolean value indicating whether to group or not. All variables are passed as parameters into the functions that do the actual work, so add 'myDoGroup' in the function call:
          myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyle,myDoGroup);
          and in the function declaration:
          function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyleName,myDoGroup){
          .. and this function, in turn, calls a function per graphic to do the work, so add it there as well:
          myAddLabel(myDocument, myGraphics[myCounter], myLabelType, myLabelHeight, myLabelOffset, myLabelStyle,myDoGroup);
          and (sigh) in the function declaration of that function:
          function myAddLabel(myDocument, myGraphic, myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, myDoGroup){
          At this point, the 'myDoGroup' parameter has finally arrived where you need it. The "myAddLabel" function creates the new frame, and this is what you want to group with the graphic.
          A little nit-pick: "myGraphic" in the function declaration does not point to the frame of the image, but to the containing image. A very important distinction, because you need to group the two frames together, not their graphic and text contents.
          Grouping is easy; well, if you know how it is. Add this piece of code to the very bottom of the function "myAddLabel":
          if (myDoGroup)
          {
            var newgroup = new Array;
            newgroup.push(myGraphic.parent);
            newgroup.push(myTextFrame);
            app.activeDocument.groups.add(newgroup);
          }
          .. and that's all there is to it.
          As I said, a possible refinement could be to create the new label layer, but only use it if the 'myDoGroup' boolean is false. If it is true, put the new text frame on the same layer as the graphic frame is, or else the grouping will cause an error.
          I'm just starting to learn scripting.
          Welcome!

          • 2. Re: How to group a text frame to graphic frame trough scripting?
            Olav Martin Kvern Level 3

            Hi jongware,

             

            First, thanks for helping!

             

            Next, I've never had a problem grouping items on different layers via scripting. I'm wondering if you have an example that shows the problem...?

             

            This, for example, works, though the rectangles are all on different layers:

             

            var myX = 72;
            var myX1 = myX;
            var myWidth = 72;
            var myX2 = myX1 + myWidth;
            var myY1 = 72;
            var myY2 = 144;
            var myDocument = app.documents.add();
            for(var myCounter = 0; myCounter < 5; myCounter++){
            myLayer = myDocument.layers.add();
            myRectangle = myDocument.pages.item(0).rectangles.add({itemLayer:myLayer, geometricBounds:[myY1, myX1, myY2, myX2]});
            myX1 = myX + (myWidth*myCounter);
            myX2 = myX1 + myWidth;
            }
            var myGroup = myDocument.pages.item(0).groups.add(myDocument.pages.item(0).rectangles);

            Thanks,

             

            Ole

            • 3. Re: How to group a text frame to graphic frame trough scripting?
              [Jongware] Most Valuable Participant
              I've never had a problem grouping items on different layers via scripting. ...

               

              Eh. Oh.

              I'm sure I had some problems with grouping before where putting items on the same layer solved it ... Perhaps (in hindsight) it had nothing to do with the layers at all.

               

              So the OP can scratch all the stuff about the layer handling in above script adjustments, and skip right to the add-group stuff.

              • 4. Re: How to group a text frame to graphic frame trough scripting?
                vjunior Level 1

                Thanks to everybody,

                 

                I almost finish the changes but I need one more piece of information. How I check and define a variable with the name of the Layer active?

                 

                When done I will post the script for everyone.

                • 5. Re: How to group a text frame to graphic frame trough scripting?
                  vjunior-UijgP4

                  Thanks again...

                   

                  I'm finally finish the modifications, I will post online the script for everyone interested.

                   

                  The script do everything the original used to plus: Now you can choose to create or not a new layer for the labels and if the label will be grouped to the pictures.

                   

                  This little script help me understand how Indesign works.

                   

                  Thanks again.

                  • 6. Re: How to group a text frame to graphic frame trough scripting?
                    beto correa Level 1

                    Hi.

                     

                    I need to change frame positioning from bottom left to right side with rotation 90 degrees based on bottom. It's possible?

                     

                    Would you like help me?