6 Replies Latest reply on Jun 3, 2015 11:47 PM by sethkhouri

    Label Script modification

    sethkhouri

      Hi

       

      I have been looking at the Label script that came with InDesign CS3 and i have made a few small modifications, but I am stumped on how i can remove the dropdown menus for Apply Style and Apply Swatch, I want it to just use 1 swatch and 1 style that i have made?

       

      can someone please help me with that?

       

      Regards

       

      Seth

        • 1. Re: Label Script modification
          Loic Aigon Level 2

          Hi Seth,

           

          You should share the code but basically you could comment anything related to those dropdown lists and set a reference to the the styles rather than using results from dropdowns.

           

          Loic

          • 2. Re: Label Script modification
            sethkhouri Level 1

            Hi Loic

             

            Thanks for getting back to me. This is the very first time i have attempted something like this, so your help is greatly appreciated I have a couple things that I am not understanding.

             

            I have added at the start a function to create a paragraph style and new swatch, these are the ones that i want the rest of the script to use instead of indesign the available styles and swatches.

             

            the other thing is I need to be able to create an image code (line 195) the image code is made up of part of the file name, the page number and a counter (it counts the graphics) i would like this count to start from the top left then to the right from top to bottom, I am assuming that isnt as easy as it sounds because i think i would need all the X & Y locations? then the counter adds in a number starting at one, but i want to pad the number out with "000" so image 1 would be 001 image 10 would be 010 etc, for this i was trying to use this but it didn't work myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' + app.activeWindow.activePage.name+'_'+myCounter.pad('0', 4);

             

            can you help me understand where i am going wrong?

             

            thank you

             

            //LabelGraphics.jsx (Modified)

            //An InDesign CS3 JavaScript

            /* 

            @@@BUILDINFO@@@ "LabelGraphics.jsx" 1.1.0 6-June-2008

            */

            //Adds labels to the graphics in the active document.

            /* This sample script has been modified. The modications are listed here: option added to create a label from clipboard data,

              dropdown menu added to apply a swatch to the caption frame. This script can now apply lables to just the selected graphics

              if you have one or more images selected. Otherwise it will apply the lables to all graphics in the document. The hieght of the

              caption frame is determined by the amount of text, and paragraph style applied. the width of the caption frame is

              determined by the width of the graphic it is under. Setting the caption height in the dialog box has been removed, as it is no

              longer needed.

              */

            main();

             

             

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

            function main(){

            // Make sure the swatch "ImageGreen" exists

              try {

                app.activeDocument.colors.add({name:"ImageLabel", space:ColorSpace.CMYK, colorValue:[0,0,100,0], colorModel:ColorModel.PROCESS});

              } catch (_) { }

              LabelYellow = app.activeDocument.swatches.item("ImageLabel");

                // Make sure the paragraph style "ImageSize" exists

              try {

                app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial\tRegular", pointSize:9, justification: Justification.CENTER_ALIGN});

              } catch (_) { }

              reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");

            }

             

             

            firstStart();

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

            function firstStart(){

              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:["Image Name","Image File Name", "Image File Path", "XMP Description", "XMP Author","Paste from clipboard"], selectedIndex:0});

              }

              }

              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:3});

              }

              }

              //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:8});

              }

              }

             

              }

             

             

             

             

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

              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 = 01; myCounter < myGraphics.length; myCounter++){

              try{

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

              }

              catch(e){};

              }

              }

            }

             

             

             

             

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

            function myAddLabel(myDocument, myGraphic, myCounter, 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("Coded Layout");

              try{

              myLabelLayer.name;

              }

              catch (myError){

              myLabelLayer = myDocument.layers.add({name:"Coded Layout"});

              }

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

              switch(myLabelType){

            //File name

              case 0:

              myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' + app.activeWindow.activePage.name+'_'+myCounter;

              break;

                    //Image name

              case 1:

              myLabel = myLink.name;

              break;

              //Image File path

              case 2:

              myLabel = myLink.filePath;

              break;

              //XMP description

              case 3:

              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

             

             

                        }

                    }

            • 3. Re: Label Script modification
              Loic Aigon Level 2

              Comment lines 83 to 92

               

              replace line 103

              var mySwatch = mySwatchNames[mySwatchDropdown.selectedIndex];

              with

              var mySwatch = app.activeDocument.swatches.item("ImageLabel");

              That should do the trick

              • 4. Re: Label Script modification
                sethkhouri Level 1

                Hi

                 

                Thank you very much, this world a treat.

                 

                one last thing if i may ask. Line 195 where I am trying to make the file name I am using this

                myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' + app.activeWindow.activePage.name+'_'+myCounter;

                And i want to be able to add "000" to the number, so i am using .pad('0', 4); at the end but that isnt working.

                 

                any ideas on that ?

                 

                thank you again,


                • 5. Re: Label Script modification
                  Vamitul Level 4

                  javascript does not have a standard pad function, so you will have to make your own:

                   

                  function padToFour(number) {
                    if (number<=9999) { number = ("000"+number).slice(-4); }
                    return number;
                  }
                  

                   

                   

                  (sidenote, i wonder who wrote the original sample scripts for adobe)

                  • 6. Re: Label Script modification
                    sethkhouri Level 1

                    Thank you for this, that worked perfectly,