9 Replies Latest reply on Oct 21, 2014 5:26 AM by beto correa

    LabelGraphic Script - Offset Modification

    lewis_cooper Level 1

      Hi eveyrone,

       

      I've scanned through the forum looking for answer to this already without any luck.

       

      I'd like to use the LabelScript for some artwork that I'm producing but would like to modify it so that the text frame is positioned to either the left or right of the graphic as opposed to placing the text frame below the graphic.

       

      I've had a pop at modifying the script myself without any success so I'm hoping one of you guys can help. I assumed it would be the myLabelOffset part but my attempts at tweakage have failed!

       

      Cheers,

      Lewis

        • 1. Re: LabelGraphic Script - Offset Modification
          lewis_cooper Level 1

          Hmmm, this is proving to be a bit of a challenge. I'm not sure if I specified this before by the way, but I'm trying to modify the LabelGraphic script supplied by Adobe.

           

          I thought perhaps it would be a simple case of moving the " + myLabelOffset" part to being after the "var myX2' bit. But what seems to happen is that the width of the text frame is inherited from the width of the picture frame that contains my graphic, and when the box is 'offset' to the right the width of the text frame will not go wider than the picture frames right edge.

           

                                   var myFrame = myGraphic.parent;
                                   var myX1 = myFrame.geometricBounds[1]; 
                                   var myY1 = myFrame.geometricBounds[2] + myLabelOffset; 
                                   var myX2 = myFrame.geometricBounds[3]; 
                                   var myY2 = myY1 + myLabelHeight;
                                   var myTextFrame = myFrame.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2], contents:myLabel}); 
                                   myTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset; 
                                   myTextFrame.parentStory.texts.item(0).appliedParagraphStyle = myLabelStyle;                    
                              }
                              function myDisplayDialog(myObjectList){
                                   //Make certain that user interaction (display of dialogs, etc.) is turned on.
                                   app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                                   var myLabelWidth = 100;
                                   var myStyleNames = myGetParagraphStyleNames(app.documents.item(0));
                                   var myLayerNames = myGetLayerNames(app.documents.item(0));
                                   var myDialog = app.dialogs.add({name:"LabelGraphics"});
                                   with(myDialog.dialogColumns.add()){
          

           

          If anyone can help, I'd be very grateful!

          • 2. Re: LabelGraphic Script - Offset Modification
            [Jongware] Most Valuable Participant

            Does this old thread help you? http://forums.adobe.com/thread/454255 -- the script shows how to create a rotated box along an existing item.

            • 3. Re: LabelGraphic Script - Offset Modification
              lewis_cooper Level 1

              Thanks Jongware, I thought I'd run the script on that page to get a better idea of what exactly it does but it asked for the straddle line and then did nothing. I'm using CS4 so not sure if that has anything to do with it.

               

              Although, even if it did work I'm not sure that I'd know what to take from that script in order to alter the InDesign sample script. I have tried but I think I'm seriously lacking the foundations needed to comprehend what I'm modifying. I was hoping that it would simply be a case of switching the offset to follow an X co-ordinate instead of a Y.

               

              If I pasted the InDesign sample script here, would that help explaining what I'd need to modify?

               

              Many thanks as always Jongware!

              • 4. Re: LabelGraphic Script - Offset Modification
                [Jongware] Most Valuable Participant
                ..it asked for the straddle line and then did nothing ..

                 

                a versioning problem -- it tries to set a point size the old way. You can find a replacement line a few posts later.

                 

                Don't bother posting that long script, it comes with the installation.

                I was assuming you wanted the label rotated along one edge (that's what my version does), but now I'm not too sure anymore. The original label script creates a box immediately below the graphic, and you want it offset to the left or right?

                • 5. Re: LabelGraphic Script - Offset Modification
                  lewis_cooper Level 1

                  Well essentially, what I wanted to achieve was exactly what the installed script does but have more choice as to where the box ends up. If you look at the JPG I've uploaded you'll see that there are 4 different items of clothing, all overlapping each other. The problem I have is that the text boxes often get hidden under/by overlapping picture frames. In an ideal world I'd want to have each box positioned where the red X's are, but ultimately I think I just need them to be them to be positioned along some specified x coordinates relative to the picture frames centre which I can then use a positive or negative value to decide whether it goes left or right of it. If that makes sense?

                   

                  If its possible to choose an object style instead of a paragraph style that would be extremely helpful too.

                   

                  Many thanks!

                  • 6. Re: LabelGraphic Script - Offset Modification
                    [Jongware] Most Valuable Participant

                    Curses onto Adobe for writing the best DTP app of the century, and then outsourcing their forum to some cheapskate java writers! I just typed a long answer (and yes: it answered your question) and got logged out with a scoffing "There has been an error".

                     

                    Where was I?

                     

                    I glossed over the LabelGraphics script, and these lines (137..140) determine the size and position of the label in the form of a bounding box:

                     

                    myX1 = myFrame.geometricBounds[1];
                    myY1 = myFrame.geometricBounds[2] + myLabelOffset;
                    myX2 = myFrame.geometricBounds[3];
                    myY2 = myY1 + myLabelHeight;

                    As this is a bounding box (rather than coordinates and size), you must add or subtract your offset to myX1 and to myX2:

                     

                    myX1 = myFrame.geometricBounds[1]+24;
                    myY1 = myFrame.geometricBounds[2] + myLabelOffset;
                    myX2 = myFrame.geometricBounds[3]+24;
                    myY2 = myY1 + myLabelHeight;

                    -- this will move the frame 24 units in your current measurement system to the right. Beware: if it's in inches, it'll move a couple of yards. In points, it's not so much.

                    Unfortunately, you cannot simply enter

                     

                    myX1 = myFrame.geometricBounds[1]+"1cm";

                     

                    as This Is Not Allowed. (JavaScript has a measurement conversion function, but I haven't been able to write something useful with that.)

                     

                    If you want a prompt before the distance, add these

                     

                      with(dialogRows.add()){
                       with(dialogColumns.add()){
                        staticTexts.add({staticLabel:"Offset", minWidth:myLabelWidth});
                       }
                       with(dialogColumns.add()){
                        var myXOffsetField = measurementEditboxes.add({editValue:0});
                       }
                      }
                    ...
                    var myXOffset = myXOffsetField.editValue;
                    ...
                    
                     myX1 = myFrame.geometricBounds[1] + myXOffset;
                     myY1 = myFrame.geometricBounds[2] + myLabelOffset; 
                     myX2 = myFrame.geometricBounds[3] + myXOffset;
                    


                    at appropriate positions.

                    1 person found this helpful
                    • 7. Re: LabelGraphic Script - Offset Modification
                      lewis_cooper Level 1

                      Thanks again for this. I've run into a little difficulty with this but I've got it to a point where it can pretty much do what I need. I'd like to spend more time on perfecting and better understanding this but the deadline is fast approaching!

                       

                      One last thing that be really helpful is if you could tell me how I can add placeholder copy to my labels. I've figured out how to remove the file suffix, now I just need to have a forced return added after the file name followed by X. XXX.

                       

                      I think it needs to go here:

                       

                                //File name remove (/\..+$/, '') if you want suffix to appear
                                case 0:
                                     myLabel = myLink.name.replace(/\..+$/, '');
                                     break;
                      

                      • 8. Re: LabelGraphic Script - Offset Modification
                        [Jongware] Most Valuable Participant

                        That isn't too hard. Add this after the myLabel line:

                         

                        myLabel += "\rX. XXX";

                         

                        The '\r' inserts a hard return.

                        • 9. Re: LabelGraphic Script - Offset Modification
                          beto correa Level 1

                          HI.
                          I'm using LabelGraphics script.

                          I need to change the frame positioning from left bottom to right bottom with rotation angle 90 degrees.
                          Follow the lines that most be modified. I think.

                          Would you help me? Please

                           

                          var myFrame = myGraphic.parent;
                          myX1
                          = myFrame.geometricBounds[1];
                          myY1
                          = myFrame.geometricBounds[2] + myLabelOffset;
                          myX2
                          = myFrame.geometricBounds[3];
                          myY2
                          = myY1 + myLabelHeight;

                          if (myPasteFromClipboard ==true)
                          {
                          try{
                          var myTextFrame = myFrame.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2]});
                          myTextFrame
                          .insertionPoints.item(0).select();
                          app
                          .paste();
                          }


                          Anyway that's entire script

                           

                          main();

                           

                           

                          //=============================================================\\

                          function main(){

                            if(app.documents.length != 0){

                            if(app.documents.item(0).allGraphics.length != 0){

                            myDisplayDialog();

                            }

                            else{

                            alert("Document contains no graphics.");

                            }

                            }

                            else{

                            alert("Please open a document and try again.");

                            }

                          }

                           

                           

                           

                           

                          //=============================================================\\

                          function myDisplayDialog(){

                            var myLabelWidth = 100;

                            var myStyleNames = myGetParagraphStyleNames();

                            var mySwatchNames = myGetSwatchNames();

                            var myDialog = app.dialogs.add({name:"LabelGraphics"});

                            with(myDialog.dialogColumns.add()){

                            //Label type

                            with(dialogRows.add()){

                            with(dialogColumns.add()){

                            staticTexts.add({staticLabel:"Label Type", minWidth:myLabelWidth});

                            }

                            with(dialogColumns.add()){

                            var myLabelTypeDropdown = dropdowns.add({stringList:["File name", "File path", "XMP description", "XMP author","Paste from clipboard"], selectedIndex:4});

                            }

                            }

                            with(dialogRows.add()){

                            with(dialogColumns.add()){

                            staticTexts.add({staticLabel:"Label Offset", minWidth:myLabelWidth});

                            }

                            with(dialogColumns.add()){

                            var myLabelOffsetField = measurementEditboxes.add({editValue:0});

                            }

                            }

                            //Style to apply

                            with(dialogRows.add()){

                            with(dialogColumns.add()){

                            staticTexts.add({staticLabel:"Label Style", minWidth:myLabelWidth});

                            }

                            with(dialogColumns.add()){

                            var myLabelStyleDropdown = dropdowns.add({stringList:myStyleNames, selectedIndex:0});

                            }

                            }

                            //Swatch to apply

                            with(dialogRows.add()){

                            with(dialogColumns.add()){

                            staticTexts.add({staticLabel:"Apply swatch", minWidth:myLabelWidth});

                            }

                            with(dialogColumns.add()){

                            var mySwatchDropdown = dropdowns.add({stringList:mySwatchNames, selectedIndex:0});

                            }

                            }

                           

                            }

                           

                           

                           

                           

                          //=============================================================\\

                            var myResult = myDialog.show();

                            if(myResult == true){

                            var myLabelType = myLabelTypeDropdown.selectedIndex;

                            var myLabelHeight = 24; // A generic label height that will be adjusted later

                            myPasteFailure = false;

                            var myLabelOffset = myLabelOffsetField.editValue;

                            var myLabelStyle = myStyleNames[myLabelStyleDropdown.selectedIndex];

                            var mySwatch = mySwatchNames[mySwatchDropdown.selectedIndex];

                            myDialog.destroy();

                            var myOldXUnits = app.documents.item(0).viewPreferences.horizontalMeasurementUnits;

                            var myOldYUnits = app.documents.item(0).viewPreferences.verticalMeasurementUnits;

                            app.documents.item(0).viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

                            app.documents.item(0).viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

                           

                           

                            try{

                            myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch);

                            }

                            catch(e){

                            alert("Unable to add lables. " + e);

                            }

                           

                            try{

                            resizeOverset() ;

                            }

                            catch(e){

                            alert("Unable to correct overset text. " + e);

                            }

                           

                            if (myPasteFailure == true){

                            alert("Unable to paste from clipboard.");

                            }

                           

                            app.documents.item(0).viewPreferences.horizontalMeasurementUnits = myOldXUnits;

                            app.documents.item(0).viewPreferences.verticalMeasurementUnits = myOldYUnits;

                            }

                            else{

                            myDialog.destroy();

                            }

                          }

                           

                           

                           

                           

                          //=============================================================\\

                          function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyleName, mySwatchName){

                            var myDocument = app.documents.item(0);

                            myStoriesArray = new Array();

                           

                            if (app.selection.length == 0) // If nothing is selected apply caption to all graphics in the document

                            {

                            var myConfirmation = confirm("Add captions to all images in the document?", false, "LabelGraphics.jsx" );

                            if (myConfirmation == true)

                            {

                            var myGraphics = myDocument.allGraphics;

                            }

                            }

                            else

                            { // If graphics are selected, just add captions to the selected items, as long as they are rectangles(image frames)

                            var myConfirmation = true;

                            var mySelections = app.selection;

                            myGraphics = new Array();

                           

                            for(i = 0; i < mySelections.length; i++){

                            if(mySelections[i] == "[object Rectangle]"){   //Check to make sure selection only includes rectangles

                            myGraphics.push(mySelections[i].allGraphics[0]);

                            }

                            else{

                            //alert("Objects other than graphics were selected!");

                            //Nothing happens if you don't select at least one graphic

                            }

                            }

                            }

                           

                            myLabelStyle = myDocument.paragraphStyles.item(myLabelStyleName);

                            mySwatch = myDocument.swatches.item(mySwatchName);

                           

                            if (myConfirmation == true){

                            for(var myCounter = 0; myCounter < myGraphics.length; myCounter++){

                            try{

                            myAddLabel(myDocument, myGraphics[myCounter], myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch, myStoriesArray);

                            }

                            catch(e){};

                            }

                            }

                          }

                           

                           

                           

                           

                          //=============================================================\\

                          function myAddLabel(myDocument, myGraphic, myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch, myStoriesArray){

                            var myLabel;

                            var myLink = myGraphic.itemLink;

                            var myPasteFromClipboard = false;

                            //Create the label layer if it does not already exist.

                            var myLabelLayer = myDocument.layers.item("labels");

                            try{

                            myLabelLayer.name;

                            }

                            catch (myError){

                            myLabelLayer = myDocument.layers.add({name:"0_CREDITOS"});

                            }

                            //Label type defines the text that goes in the label.

                            switch(myLabelType){

                            //File name

                            case 0:

                            myLabel = myLink.name;

                            break;

                            //File path

                            case 1:

                            myLabel = myLink.filePath;

                            break;

                            //XMP description

                            case 2:

                            try{

                            myLabel = myLink.linkXmp.description;

                            }

                            catch(myError){

                            myLabel = "No description available.";

                            }

                            break;

                            //XMP author

                            case 3:

                            try{

                            myLabel = myLink.linkXmp.author

                            }

                            catch(myError){

                            myLabel = "No author available.";

                            }

                            break;

                            //Paste from the clipboard

                            case 4:

                            try{

                            myPasteFromClipboard = true;

                            }

                            catch(myError){

                            myLabel = "No clipboard data available.";

                            }

                            break;

                            }

                            var myFrame = myGraphic.parent;

                            myX1 = myFrame.geometricBounds[1];

                            myY1 = myFrame.geometricBounds[2] + myLabelOffset;

                            myX2 = myFrame.geometricBounds[3];

                            myY2 = myY1 + myLabelHeight;

                           

                            if (myPasteFromClipboard ==true)

                            {

                            try{

                            var myTextFrame = myFrame.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2]});

                            myTextFrame.insertionPoints.item(0).select();

                            app.paste();

                            }

                            catch(e){

                            myTextFrame.remove();

                            myPasteFailure = true;

                            }

                            }

                            else{

                            var myTextFrame = myFrame.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2], contents:myLabel});

                            }

                           

                            myTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;

                            myTextFrame.paragraphs.item(0).appliedParagraphStyle = myLabelStyle;

                           

                           

                           

                            myTextFrame.fillColor = mySwatch;

                            myFrameParentStory = myTextFrame.parentStory;

                            myStoriesArray.push(myFrameParentStory);

                           

                          }

                           

                           

                           

                           

                           

                           

                          //=============================================================\\

                          function myGetParagraphStyleNames(){

                            var myStyleNames = app.documents.item(0).paragraphStyles.everyItem().name;

                            return myStyleNames;

                          }

                           

                           

                          function myGetSwatchNames(){

                            var mySwatchNames = app.documents.item(0).swatches.everyItem().name;

                            return mySwatchNames;

                          }

                           

                           

                          function resizeOverset() {

                                      for (var j = myStoriesArray.length - 1; j >= 0; j--) {

                                              myLastFrame = myStoriesArray[j].texts[0].parentTextFrames[myStoriesArray[j].texts[0].parentTextFrames.l ength - 1];

                                             

                            myNewY2 = myLastFrame.geometricBounds[3]; //get the width of the text frame before doing fit()

                           

                            myLastFrame.fit(FitOptions.FRAME_TO_CONTENT);

                           

                            myNewY1 = myLastFrame.geometricBounds[1];

                            myNewX1 = myLastFrame.geometricBounds[2];

                            myNewX2 = myLastFrame.geometricBounds[0];

                           

                           

                            myLastFrame.geometricBounds = [myNewX1, myNewY1, myNewX2, myNewY2]; // reset the width to before fit() was ran

                           

                           

                                      }

                                  }

                           

                          Thanks

                           

                          Luiz Roberto