8 Replies Latest reply on Apr 1, 2013 11:23 AM by wimkees

    Go back to certain insertionpoint

    wimkees

      Hello,

       

      In a ID 5.5 javascript I can detect where my current insertionpoint is by using

       

      ip = app.selection[0].insertionPoints[0].index;

       

      After doing some textadditions (so my insertionpoint has moved) how can I jump back to that point?

       

      Any suggestions?

        • 1. Re: Go back to certain insertionpoint
          Laubender Adobe Community Professional & MVP

          @wimkees – your insertion point might be moving, but you still have your selection and its first insertion point to get the index from.

           

          So do not store the index (that is changing every time you are adding or removing text) into a variable.
          Just use the current index the moment you need it:

           

          //First insertion point of your text selection:
          var ip = app.selection[0].insertionPoints[0];
          
          //Current index number:
          var ipIndex = ip.index;
          
          //Add some text at the beginning of your story:
          ip.parentStory.insertionPoints[0].texts[0].contents = "NEW! ";
          
          //Former index number stored in a variable:
          $.writeln(ipIndex);
          
          //NOW get the NEW current index number:
          $.writeln(ip.index);
          

           

          Uwe

          • 2. Re: Go back to certain insertionpoint
            wimkees Level 1

            Hi Uwe,

             

            I've to explain my problem better:

             

            In a document I want to add at my insertionpoint somewhere in a threaded textframe a multipage PDF.

            The pages of the PDF must be placed as inline graphics.

             

            So my script is:

             

            Main();

             

            function Main()
            {

               
                if (app.selection[0] == null )
                    {
                    alert ("Place textcursor first");
                    return;
                    }   

             

                var myFile = File.openDialog("Select PDF-file", "");
               
                app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
               
                var currentframe = app.selection[0].parentTextFrames[0];
                var bounds = currentframe.geometricBounds;

            //starting with a frame break

                app.selection[0].contents = SpecialCharacters.frameBreak;

                var myDocument = app.activeDocument;
                var myPage = myDocument.pages.item(0);
                var myFrame = myPage.rectangles.add();  

             

            //now I detect the number of pages of the PDF

            for (var c = 1; ; c++)
                {
                    app.pdfPlacePreferences.pageNumber = c;
                    var newitem = myFrame.place(myFile)[0];
                    newpagenumber = newitem.pdfAttributes.pageNumber;
                    if (c>1 && newpagenumber>1)
                    {
                        maxP=newpagenumber;
                    }

                    if (c > 1 && newpagenumber == 1 )
                    {
                        myFrame.remove();
                        break;
                    }
                }

             

             

            //here I place a number of framebreaks according to the number of PDF pages

            for (var c = 1;c<=maxP ; c++)
                {
            app.selection[0].contents = SpecialCharacters.frameBreak;
            }

             

            //here I want to return to my first insertionpoint, place the first PDF-page, skip the following framebreak, add the next PDF-page etc.

             

             

            for (var c = 1;c<=maxP ; c++)
                {
                    app.pdfPlacePreferences.pageNumber = c;
                   
                    var myFrame = app.selection[0].rectangles.add();  
                    myFrame.geometricBounds = bounds;
                    myFrame.strokeWeight = 0;
             
                    var newitem = myFrame.place(myFile)[0];
                    myFrame.fit(FitOptions.proportionally);
                    myFrame.fit(FitOptions.frameToContent);

                    newpagenumber = newitem.pdfAttributes.pageNumber;

                    app.selection[0].contents = SpecialCharacters.frameBreak;
                }
            }

             

             

             

            If I execute the script now it tells me there's an invalid object for this request.

            Reason is (I think) there is no frame yet to put the rectangle in.

            • 3. Re: Go back to certain insertionpoint
              Laubender Adobe Community Professional & MVP

              @wimkees – I think there are several misconceptions here:

               

              You assume:

               

              1. that the text frame you want to place the PDF is

                   1.a is always the same size,

                   1.b never rotated,

                   1.c never skewed


              2. that the story will never be overset

               

              3. that the PDF that you want to place always has a crop box

               

              4. that the pages length of the PDF could be determined (not always possible)

               

              The following snippet is working to place page 1 of a PDF.
              It will not handle an overset story, it assumes that the crop box will work.

               

              Just to give you something that will actually work.
              Maybe you could wrap it with a loop through all your PDF pages.

              But all four points above must be tackled at a next stage of the script.

               

              The main idea here is:
              1. Insert two frame breaks in a row
              2. Go back one insertion point just between the two frame breaks

              3. Determine the size of the parent text frame at this point (that will tackle 1.a)

              4. Add the rectangle directly to this insertion point and size it at the same time

              5. Place the PDF to that rectangle

              6. Fit it to the rectangle like you want

               

              Remember, the following snippet is just a starting point:

               

              //PlaceFileAtInsertionPoint_1.0.jsx
              //Uwe Laubender
              /**
              * @@@BUILDINFO@@@ PlaceFileAtInsertionPoint_1.0.jsx !Version! Sun Mar 31 2013 11:57:48 GMT+0200
              */
              
              //DESCRIPTION:Places a file at the first insertion point of your selection
              
              app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
              
              var myFile = File.openDialog("Select PDF-file", "");
              
              //If there is NO file selected, stop the script: 
              if(myFile === null){exit();};
              
              //TO-DO:
              //Here at this point we should have checked, if the selected file is really a PDF file.
              
              
              //For testing (and other practical) purposes we should be able to undo the action in one go:
              
              app.doScript(_PlacePDF_AT_INSERTIONPOINT, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Place PDF at Insertion Point");
              
              
              function _PlacePDF_AT_INSERTIONPOINT(){
              
              var myOldPDFCrop = app.pdfPlacePreferences.pdfCrop;
              app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
              
              var ip = app.selection[0].insertionPoints[0];
              var ipIndex = ip.index;
              ip.texts[0].contents = SpecialCharacters.frameBreak;
              ip.texts[0].contents = SpecialCharacters.frameBreak;
              
              //InsertionPoint before:
              var ipToAddRectangle = ip.parentStory.insertionPoints[ipIndex+1];
              
              var parentTextFrameBounds = ip.parentStory.insertionPoints[ipIndex+1].parentTextFrames[0].geometricBounds;
              
              var myRectangle = ip.parentStory.insertionPoints[ipIndex+1].rectangles.add({
              
                  //Important:
                  strokeColor:"None",
                  strokeWeight:0,
              
                  //Less important:
                  fillColor:"None",
              
                  //Important:
                  geometricBounds:parentTextFrameBounds
              
                  });
              
              myRectangle.place(myFile);
              myRectangle.fit(FitOptions.proportionally);
              myRectangle.fit(FitOptions.frameToContent);
              
              app.pdfPlacePreferences.pdfCrop = myOldPDFCrop;
              
              //COMMENT:
              //The selection is still the same after execution!
              //So we could use this function theoretically as the ingredients for a loop through all pages of the PDF.
              
              };
              
              • 4. Re: Go back to certain insertionpoint
                wimkees Level 1

                Hi Uwe,

                 

                Thx!!! You did solve the problem.

                 

                My script now is:

                 

                // Script to insert PDF-pages (as inline graphics in a text story)
                // Graphic frames in which the PDF-pages are placed are the same size as the text-frame they are in
                // After placing the PDF-page is proportionally scaled and the graphic frame is fit to the PDF-page

                 

                Main();

                function Main()
                {
                  
                // Check if cursor is placed in textframe
                    if (app.selection[0] == null )
                        {
                        alert ("Plaats eerst de invoegcursor in een tekstframe");
                        return;
                        }   

                // Select the PDF-file
                var myFile = File.openDialog("Selecteer het PDF-bestand", "");
                   
                // Use the cropbox of the PDF-file
                    app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
                   
                // Put the current cursorposition in ip
                    var ip = app.selection[0].insertionPoints[0];
                    var ipIndex = ip.index;
                       
                // Insert two framebreaks
                    app.selection[0].contents = SpecialCharacters.frameBreak;
                    app.selection[0].contents = SpecialCharacters.frameBreak;

                // Add a small rectangle to place the PDF-pages in (just for pagedetection)
                    var myDocument = app.activeDocument;
                    var myPage = myDocument.pages.item(0);
                    var myFrame = myPage.rectangles.add({geometricBounds:[0, 0, 50, 50]});  

                // Here the PDF file is placed in myFrame untill it reaches the end
                // The number of pages is put in variable maxP
                // After that the frame is removed
                for (var c = 1; ; c++)
                    {
                        app.pdfPlacePreferences.pageNumber = c;
                        var newitem = myFrame.place(myFile)[0];
                        newpagenumber = newitem.pdfAttributes.pageNumber;
                        if (c>1 && newpagenumber>1)
                        {
                            maxP=newpagenumber;
                        }

                        if (c > 1 && newpagenumber == 1 )
                        {
                            myFrame.remove();
                            break;
                        }
                    }

                // Here is the loop to place the PDF-pages in frames
                for (var c = maxP;c>=1 ; c--)
                    {
                        app.pdfPlacePreferences.pageNumber = c;
                       
                        var ipToAddRectangle = ip.parentStory.insertionPoints[ipIndex+1];
                        var parentTextFrameBounds = ip.parentStory.insertionPoints[ipIndex+1].parentTextFrames[0].geometricBounds;

                        var myRectangle = ip.parentStory.insertionPoints[ipIndex+1].rectangles.add({

                                //Important:
                                strokeColor:"None",
                                strokeWeight:0,
                        
                                //Less important:
                                fillColor:"None",
                        
                                //Important:
                                geometricBounds:parentTextFrameBounds
                        
                            });

                        var newitem = myRectangle.place(myFile)[0];
                        myRectangle.fit(FitOptions.proportionally);
                        myRectangle.fit(FitOptions.frameToContent);

                        newpagenumber = newitem.pdfAttributes.pageNumber;
                    }
                }

                 

                Only thing that's missing now (but not a big issue): there are no framebreaks after each placed PDF-page. Only after the last one.

                 

                Sorry for putting the script in this message. I don't know how to put it in a frame as you did...

                 

                Thx,

                Wim

                • 5. Re: Go back to certain insertionpoint
                  Laubender Adobe Community Professional & MVP

                  I don't know how to put it in a frame as you did...

                   

                  @Wim – There is a "Use advanced editor" button at the top.
                  Select your code, go to the >> controls and chose Insert / Syntax Highlighting / Java.

                   

                  Uwe

                  • 6. Re: Go back to certain insertionpoint
                    wimkees Level 1

                    @Uwe: See it now.

                     

                    Did some little tweaks to the script.

                    I found out that the recompose() command is important when you insert pages at the end of a story.

                    Here's the (almost?) finally script:

                     

                     

                    // Script to insert PDF-pages (as inline graphics in a text story)
                    // Graphic frames in which the PDF-pages are placed are the same size as the text-frame they are in
                    // After placing the PDF-page is proportionally scaled and the graphic frame is fit to the PDF-page
                     
                    Main();
                    
                     function Main()
                    {
                       
                    // Check if cursor is placed in textframe
                        if (app.selection[0] == null ) 
                            {
                            alert ("Plaats eerst de invoegcursor in een tekstframe");
                            return;
                            }    
                    
                        var myDocument = app.activeDocument;
                        myDocument.textPreferences.smartTextReflow = true;
                        myDocument.textPreferences.limitToMasterTextFrames = false;
                        myDocument.textPreferences.deleteEmptyPages = true
                    
                     // Select the PDF-file
                        var myFile = File.openDialog("Selecteer het PDF-bestand", "");
                        
                    // Use the cropbox of the PDF-file
                        app.pdfPlacePreferences.pdfCrop = PDFCrop.CROP_PDF;
                        
                    // Put the current cursorposition in ip
                        var ip = app.selection[0].insertionPoints[0];
                        var ipIndex = ip.index;
                        
                    // Use the geometric bounds of the textframe the cursor is in
                        var currentframe = app.selection[0].parentTextFrames[0];
                        var bounds = currentframe.geometricBounds;
                            
                    // Insert a framebreak and recompose the document (which is important to do)
                        app.selection[0].contents = SpecialCharacters.frameBreak;
                        myDocument.recompose();
                        
                    // Add a small rectangle to place the PDF-pages in (just to detect the number of pages of the PDF)
                    
                        var myPage = myDocument.pages.item(0);
                        var myFrame = myPage.rectangles.add({geometricBounds:[0, 0, 5, 5]});   
                    
                     // Here the PDF file is placed in myFrame untill it reaches the end
                    // The number of pages is put in variable maxP
                    // After that the frame is removed
                    app.scriptPreferences.enableRedraw = false;
                    for (var c = 1; ; c++)
                        {
                            app.pdfPlacePreferences.pageNumber = c;
                            var newitem = myFrame.place(myFile)[0];
                            newpagenumber = newitem.pdfAttributes.pageNumber;
                            if (c>1 && newpagenumber>1)
                            {
                                maxP=newpagenumber;
                            }
                            if (c > 1 && newpagenumber == 1 )
                            {
                                myFrame.remove();
                                break;
                            }
                        }
                    
                     // Here is the loop to place the PDF-pages in frames
                    for (var c = maxP;c>=1 ; c--)
                        {
                            app.pdfPlacePreferences.pageNumber = c;
                            
                            var ipToAddRectangle = ip.parentStory.insertionPoints[ipIndex+1];
                            var myRectangle = ipToAddRectangle.rectangles.add({
                     
                                    //Important:
                                    strokeColor:"None",
                                    strokeWeight:0,
                             
                                    //Less important:
                                    fillColor:"None",
                             
                                    //Important:
                                    geometricBounds:bounds
                             
                                });
                     
                            var newitem = myRectangle.place(myFile)[0];
                            myRectangle.fit(FitOptions.proportionally);
                            myRectangle.fit(FitOptions.frameToContent);
                            newpagenumber = newitem.pdfAttributes.pageNumber;
                        }
                    } 
                    
                    
                    • 7. Re: Go back to certain insertionpoint
                      Jump_Over Level 5

                      Hi,

                       

                      Here's the (almost?) finally script...

                       

                      In case of "almost" I suggest to modify:

                      if (app.selection[0] == null )

                       

                      This one is also true if some pageItem is selected, so the script will fail (i.e. no insertionPoint property for rectangle) and alert omitted.

                      Add a condition like

                      if (app.selection[0]==null || !app.selection[0].hasOwnProperty("alignToBaseline"))

                      to filter selection of texts only.

                       

                       

                      app.selection[0].contents = SpecialCharacters.frameBreak

                       

                       

                      This one could replace some selection (not only insertionPoints are passing alert_statement)

                      Suggest to change it to: ip.contents = SpecialCharacters.frameBreak;

                      By the way: is your intention to start a doc with an empty frame (page)?

                       

                      In case of adding frameBreaks between rectangles insert

                      ipToAddRectangle.contents = SpecialCharacters.frameBreak;

                      in the last line of "for" loop.

                      By the way: current last line seems unnecessary...

                       

                      rgds

                      Jarek

                      • 8. Re: Go back to certain insertionpoint
                        wimkees Level 1

                        @ Jarek

                         

                        Thx for your suggestions. I will implement these (and some lines from Uwe) to my script.

                         

                        @Jarek & Uwe

                        You guys helped me so well!!

                        Maybe you noticed I'm not a programmer but with your help the script works fine now.

                        And I learned a lot more about programming too.

                         

                        Wim