12 Replies Latest reply on Feb 27, 2014 6:24 AM by PatrickNo

    [CS6] [JS] How to select Objects by Label

    PatrickNo

      Hi all,

       

      Im just a newbie to InDesign Scripting and got a simple question.

      I got some TextFrames with multiple imagepaths all seperated in lines in there and want to convert them to the actual images.

      I can do this with the following JavaScript:

       

      Placeholder = app.selection[0];
      var imagepath = app.selection[0].contents;
      Placeholder.place (File(imagepath), false);
      

       

      The only problem I have here is that this just works with one path. If there are more paths included it won't work.

      So I decided to seperate the TextFrame to one TextFrame for one path. This could be accomplished with the following script:

       

      var myDocument = app.activeDocument;
      app.selection[0].label = "MyTestLabel";
      myParagraphs = app.selection[0];
      var ii = 1;
          for (var one = 0; one <1; one++){
              var gB1 = app.selection[0].geometricBounds[0];
              var gB2 = app.selection[0].geometricBounds[2];
              var gB3 = app.selection[0].geometricBounds[1];
              var gB4 = app.selection[0].geometricBounds[3];
          }
      for (var i = 0; i < 6; i++) {
          myParagraphs = app.selection[0].paragraphs[i].contents;
          var myTextframe = myDocument.textFrames.add();
          gB1 = gB1+40;
          gB2 = gB2+40;
          myTextframe.geometricBounds = [gB1,gB3,gB2,gB4];
          myTextframe.contents = myParagraphs;
          myTextframe.label = "Path"+ii;
          ii++;
      }
      

       

       

      This script got some problems because I just merged it together from multiple sources and tried to modify it until it works. So I got some questions:

      First I want to replace the parameter of the second for loop with a variable. I thought it is paragraphs.length but that doesn't work. This is important because the Paragraph length of each TextFrame is different.

       

      Then you may noticed I assigned labels to each new created TextFrame. They are Path1, Path2.. etc.

      How can I select objects like my TextFrames by label? I searched a lot about this question it seems that this functionality has changed since CS5 if I understood everything right.

      I hope you can help me there, because then I can automatically seperate the paths and turn them into the actual images.

      Maybe you guys know an easier way without selecting certain objects.

       

      I would really appreciate your answers.

      Thanks in advance!

        • 1. Re: [CS6] [JS] How to select Objects by Label
          Jump_Over Level 5

          Hi,

           

          You came through a wrong door, I am afraid.

          The solution could be as easy as you are able to determine which part of your text is a PATH and ONLY PATH.

          Place an example of  text with your imagepaths included. It shouldn't be a problem to find an easy way...

           

          Jarek

          • 2. Re: [CS6] [JS] How to select Objects by Label
            PatrickNo Level 1

            Hey there Jarek,

             

            every paragraph is an exact imagepath. It looks like this:

             

             

            /Users/Patrick/Desktop/Test/picture_01.jpg
            /Users/Patrick/Desktop/Test/picture_02.jpg
            /Users/Patrick/Desktop/Test/picture_03.jpg
            /Users/Patrick/Desktop/Test/picture_04.jpg
            /Users/Patrick/Desktop/Test/picture_05.jpg
            /Users/Patrick/Desktop/Test/picture_06.jpg
            

             

             

            Can you show me how this could be done?

            • 3. Re: [CS6] [JS] How to select Objects by Label
              Jump_Over Level 5

              Hi,

               

              Is your goal to place them as anchored objects inside a parent textFrame?

              How big those pictures can be? Alike images or alike icons?

               

              Jarek

              • 4. Re: [CS6] [JS] How to select Objects by Label
                Jump_Over Level 5

                Hi,

                 

                if size of images is not a problem run this:

                var mTarget = app.selection[0];
                
                if (!mTarget || !mTarget.hasOwnProperty ("parentStory")) 
                     {alert ("select a textFrame or a part of text\rand try again"); exit();}
                mTarget = mTarget.parentStory.paragraphs.everyItem().getElements();
                
                var 
                    len = mTarget.length,
                    mPlaceholder,
                    mFile,
                    mImagePath;
                
                while (len-->0) {    
                    mPlaceholder = mTarget[len].words.everyItem().texts[0];
                    mImagePath = mPlaceholder.contents;
                    mFile = File(mImagePath);
                    if (mFile.exists) mPlaceholder.place (mFile, false);
                    }
                

                 

                Jarek

                1 person found this helpful
                • 5. Re: [CS6] [JS] How to select Objects by Label
                  PatrickNo Level 1

                  Thank you for this nice script. It works really great!

                   

                  Actually my intention was to place every image as an independent object but I found out that anchoring all pictures in one textFrame is much more useful for me.

                  I tried some things manually and worked out what I still need for the best result. First i resized the images because they are way to big if I don't control the size. Then I seperated the Images with 3 columns. 2 Pictures each column. Lastly I applied an object style to the images and a paragraph style to the whole textFrame. It looks like this:

                   

                  temp.png

                   

                   

                  So I really need the opportunity to change the size of the pictures. It would be even better if I can get a dialog box which asks me to set one width for every picture. This makes the script more dynamic to certain cases.

                  Then I want to set the number of columns. It would also be nice to choose them in the same dialog box as I mentioned before.

                  After that I want some easy changes: Get the width of one image, multiply that by the number of colums + some spacing and set that as the width of the textFrame.

                  The width of the image should also be the height from the textFrame. After that I want to do the command Fit Frame to Content.

                  Lastly I want to apply a predefined paragraph style to the textFrame and object style to the images.

                   

                  This may sound a bit much, I hope you understand everything.

                  Thanks in advance!

                  • 6. Re: [CS6] [JS] How to select Objects by Label
                    Jump_Over Level 5

                    Hi,

                     

                    Yeap, I've got your point pretty well.

                    I hope you understand a difference between "help me, pls" and "do something, pls"

                     

                    Thanks,

                    Jarek

                    • 7. Re: [CS6] [JS] How to select Objects by Label
                      PatrickNo Level 1

                      Hey,

                       

                      Im sure I know what the difference is. My problem is that I got a little bit knowledge but no real experience. That makes the whole thing really hard.

                      I found a site with the Object Model of CS6 (http://jongware.mit.edu/idcs6js/). It's nice to have a list of everything that can be used. My problem is that I am not able to connect the dots to one working script in an easy way. So thats why im asking for help.

                      • 8. Re: [CS6] [JS] How to select Objects by Label
                        PatrickNo Level 1

                        Hey,

                         

                        is there any chance that you could help me once again?

                         

                        Thanks

                        Patrick

                        • 9. Re: [CS6] [JS] How to select Objects by Label
                          Jump_Over Level 5

                          Hi Patrick,

                           

                          In case of resizing images here is some code which set imageBoxes width equal to textColumn.width

                          (variable mW):

                           

                          currHUnit = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
                          currVUnit = app.activeDocument.viewPreferences.verticalMeasurementUnits;
                          app.activeDocument.viewPreferences.verticalMeasurementUnits = app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;
                          //
                          var mTarget = app.selection[0];
                          
                          if (!mTarget || !mTarget.hasOwnProperty ("parentStory")) 
                               {alert ("select a textFrame or a part of text\rand try again"); exit();}
                          mTarget = mTarget.parentStory.paragraphs.everyItem().getElements();
                          
                          var 
                              len = mTarget.length,
                              mFrame = mTarget[0].parentStory.textContainers[0],
                              mW = mFrame.textFramePreferences.textColumnFixedWidth,
                              mPlaceholder,
                              mFile, mImage, currRec,
                              mImagePath;
                          
                          while (len-->0) {    
                              mPlaceholder = mTarget[len].words.everyItem().getElements()[0].texts[0];
                              mImagePath = mPlaceholder.contents;
                              mFile = File(mImagePath);
                              if (mFile.exists) {
                                  mImage = mPlaceholder.place (mFile, false)[0];
                                  currRec = mImage.parent;
                                  currRec.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [mW,10]);
                                  currRec.fit(FitOptions.FILL_PROPORTIONALLY);
                                  currRec.fit(FitOptions.FRAME_TO_CONTENT);
                                  }
                              }
                          //
                          app.activeDocument.viewPreferences.verticalMeasurementUnits  = currVUnit;
                          app.activeDocument.viewPreferences.horizontalMeasurementUnits = currHUnit;
                          

                           

                          Count of columns should be set as a textFrame property.

                          Space before/after could be set as a paragraphStyle property ( only if leading = Leading.AUTO )

                           

                          Jarek

                          • 10. Re: [CS6] [JS] How to select Objects by Label
                            PatrickNo Level 1

                            Hey Jarek,

                             

                            thanks again! It works just great! The code itself seems to be easy but I just don't know how to get there. So it would be great to have a source with all those identifiers classes etc. and examples to see it in action.

                            So maybe I am able to accomplish the rest myself.

                             

                            Thanks

                            Patrick

                            • 11. Re: [CS6] [JS] How to select Objects by Label
                              Jump_Over Level 5

                              Hi,

                               

                              No better source than http://jongware.mit.edu/idcs6js/ already found by you

                               

                              Script plays with classes from Basic Suite, Layout Suite, Text Suite.

                              Each class has properties ==> the one you can read or modify,

                              and methods ==> the one you can use to do something with object.

                               

                              Some explanation and examples could be found here:

                              http://www.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/scripting/InDesign_Scrip tingGuide_JS.pdf

                               

                              Jarek

                              • 12. Re: [CS6] [JS] How to select Objects by Label
                                PatrickNo Level 1

                                Hey,

                                 

                                thanks I will absolutly read that

                                When I am done writing some script I'll let you know here!

                                 

                                Patrick