3 Replies Latest reply on Nov 3, 2016 7:49 AM by BradRE

    Processing loop of selection.

    BradRE

      Any help would be greatly appreciated!

       

      While manually selecting 3 frames, 1 graphic, and 2 text frames (the defining difference between the 2 text frames will be the unique paragraph style); I'm hoping to cut (or move/duplicate) the contents from "Photo Credit" to the other selected text frame which will be identified with the contents "Cutline" Paragraph Style, to the beginning of this paragraph with the addition of a hard return in order to maintain the style formatting. Then continue to do some resizing and other adjustments to the graphic frame. I've started with this portion of the script and I'm unable to get it to work. Taking out the "IF" appliedParagraphStyle == applied_style then app.cut() portion out of the loop, it will work on the proper frame, but in the loop it does not. Am I really messing up something in the loop through the array?

       

      var doc = app.activeDocument;
      var applied_style = doc.paragraphStyles.itemByName('Photo credit');
      
      var frame = doc.selection[0];
        for (var i=0,l=frame.length; i<l; i++) {
             if (frame[i].paragraphs.appliedParagraphStyle == applied_style)
             {
             frame[i].paragraphs.texts.select()
             app.cut();
             }
        }
      
        • 1. Re: Processing loop of selection.
          natrev Level 3

          Hi Bradre,

           

          Try this code..

           

           

          var doc = app.activeDocument;  
          var applied_style = doc.paragraphStyles.itemByName('Photo credit');  
          var frame = app.selection;  
          for (var i=0,l=frame.length; i<l; i++) {  
              try{
                 if (frame[i].paragraphs[0].appliedParagraphStyle === applied_style){  
                     frame[i].select();  
                     app.cut();  
                     exit(0);
                 }  
              }catch(er){}
          } 
          

           

           

          -yajiv

          • 2. Re: Processing loop of selection.
            Laubender Adobe Community Professional & MVP

            Hi Brad,

             

            for InDesign CS5.5 and above, the order of items in app.selection is the "clicking" order.

            You could put this to your advantage.

             

            app.selection[0] is the first selected object,

            app.selection[1] the second one is the one you added by holding the SHIFT key and clicked.

             

            To move or duplicate text from one frame to another you don't need to select text and cut or copy it.

             

            // Two text frames selected
            // by clicking at the one
            // then holding the shift key and do another click at the second one
            
            var s = app.selection;
            
            // First selected item:
            var source = s[0];
            // Second selected item:
            var target = s[1];
            
            // Add a paragraph sign at the end of the story of the target text frame
            // by assigning it as contents to the last insertion point:
            target.parentStory.insertionPoints[-1].contents = "\r";
            
            // To duplicate ALL formatted text from the source to the target's last insertion point use the method duplicate().
            // Method move() would work the same.
            source.parentStory.duplicate(LocationOptions.AFTER,target.parentStory.insertionPoints[-1]);
            

             

            Btw.: Line 18 would also work without addressing the last insertion point of the story:

             

            source.parentStory.duplicate(LocationOptions.AFTER,target.parentStory);
            

             

             

            See document object model (DOM) documentation here:

            Indesign JavaScript Help

             

            Or online here:

            Adobe InDesign CS6 (8.0) Object Model JS: Text

             

            For InDesign CC 2015.0 see also here:

            InDesign ExtendScript API (11.0)

             

            Regards,
            Uwe

            1 person found this helpful
            • 3. Re: Processing loop of selection.
              BradRE Level 1

              Updated

              Thank you both natrev & Laubender, your responses helped me along greatly. Is there any chance the same can be done with the graphic? Isolating either of the text frames is working fine but finding the graphic frame in a loop is eluding me so far, I've tried many different variations with no luck. I'm using CS5.5.

               

              Thank you again!

               

              var doc = app.activeDocument;
              var credit_style = doc.paragraphStyles.itemByName("credit");    
              var cutline_style = doc.paragraphStyles.itemByName("cutline");    
              var frame = app.selection;
              
              for (var i=0,l=frame.length; i<l; i++) {    
                  try{  
                     if (frame[i].paragraphs[0].appliedParagraphStyle === credit_style){    
              var credit_frame = frame[i];
                     }
                  if (frame[i].paragraphs[0].appliedParagraphStyle === cutline_style){    
              var cutline_frame = frame[i];
                     }
                 if ((frame[i].constructor.name == "Rectangle" && frame.images.length > 0)){
              var graphic_frame = frame[i];
                     }
              
              credit_frame.parentStory.insertionPoints[-1].contents = "\r";
              credit_frame.parentStory.duplicate(LocationOptions.BEFORE,cutline_frame.parentStory);
              credit_frame.remove();
              graphic_frame.geometricBounds = [0, 0, 50, 100];
                   }catch(er){}  
              }