12 Replies Latest reply on Jun 7, 2016 2:13 AM by Johnwhite

    How to define stories

    Johnwhite Level 1

      Hi experts,

       

      I have a this kind of script for add a start at beginning of each of paragraph but if the document just has one single story.

       

      var myDoc = app.activeDocument;
              myStory = myDoc.stories.everyItem();
      for (var i = myStory.paragraphs.length-1; i >= 0; i--)
              myStory.paragraphs[i].insertionPoints[0].contents = "*"
      

       

      my goal is if that is a document which has many of text frame and many stories, and I need to add a start at beginning of each para, how can I define it?

       

      thanks

       

      Regard

      John

        • 1. Re: How to define stories
          Jump_Over Level 5

          Hi John,

           

          Try:

          app.activeDocument.stories.everyItem().paragraphs.everyItem().insertionPoints[0].contents = "*";
          

           

          Jarek

          • 2. Re: How to define stories
            Laubender Adobe Community Professional & MVP

            You already were there. At least half the way.

             

            // EDIT:

            Puts a "*" on every start of a story:

             

            app.documents[0].stories.everyItem().insertionPoints[0].contents = "*";
            

             

            Recommended, Marc Autret's take on everyItem():

            Indiscripts :: On ‘everyItem()’ – Part 1

            Indiscripts :: On ‘everyItem()’ – Part 2

             

            // EDIT:

            I misread what the OP really wanted. On every item of every paragraph.
            So Jarek is right.

             

             

            Regards,
            Uwe

            • 3. Re: How to define stories
              Laubender Adobe Community Professional & MVP

              But the problem could be, that there is an empty text frame where no paragraph object is present.

              Then you'll get an error message.

               

              So it would be better to:

               

              1. Put a "*" to the start of every story to add a paragraph object also to empty text frames or text paths.

              2. Put a "*" to the start of every paragraph.
              Now you did double the "*" of the start of every story.

              3. Remove the double entry of the start of every story.

               

              More generally:

               

              var newContents = "*";
              var lengthOfNewContents = newContents.length;
              
              app.documents[0].stories.everyItem().insertionPoints[0].contents = "*";
              app.documents[0].stories.everyItem().paragraphs.everyItem().insertionPoints[0].contents = "*";
              
              for(var n=0;n<lengthOfNewContents.length;n++)
              {
                  app.documents[0].stories.everyItem().characters[0].remove();
              };
              

               

              You could try itemByRange() to remove the double entries, but this would fail.

               

              Regrads,
              Uwe

               

              // EDIT: I still need my first morning coffee. Edited the code where I forgot .length in the for-loop.

              // Plus: I have to test this a bit more. There might be issues.

              • 4. Re: How to define stories
                Johnwhite Level 1

                Thank you all guys

                regard

                John

                • 5. Re: How to define stories
                  Johnwhite Level 1

                  Hi Uwe,

                   

                  As you says, if there is an empty frames, will course script error,

                  how can I define like Paragraph.length >= 0 continue;

                   

                  how can I make this script working?

                   

                  var myDoc = app.activeDocument;
                          myStory = myDoc.stories.everyItem();
                          myParagraph = myStory.paragraphs.everyItem();
                      if (myParagraph.length <= 0) continue;
                          myParagraph.insertionPoints[0].contents = "*"
                  

                   

                   

                  thanks

                  regard

                  John

                  • 6. Re: How to define stories
                    Laubender Adobe Community Professional & MVP

                    After my first cup of coffee and some tests:

                     

                    var newContents = "MyText";
                    var lengthOfNewContents = newContents.length;
                    
                    // Add the new contents to every beginning of every story:
                    app.documents[0].stories.everyItem().insertionPoints[0].contents = newContents;
                    
                    // Add the new contents to every paragraph:
                    app.documents[0].stories.everyItem().paragraphs.everyItem().insertionPoints[0].contents = newContents;
                    
                    // Remove the double entries:
                    app.documents[0].stories.everyItem().texts[0].characters.itemByRange(0,lengthOfNewContents-1).remove();
                    
                    // Remove the entries on formerly empty text frames or text paths:
                    var myStories = app.documents[0].stories.everyItem().getElements();
                    
                    for(var n=0;n<myStories.length;n++)
                    {
                        if(myStories[n].texts[0].contents == newContents)
                        {
                              myStories[n].texts[0].contents = "";
                        };
                    };
                    

                     

                    The alternative would be to loop all stories and check, if paragraphs are there.
                    If yes, do a loop on the paragraphs.

                     

                    Uwe

                    1 person found this helpful
                    • 7. Re: How to define stories
                      Laubender Adobe Community Professional & MVP

                      I think, the following method will be even better, if you want to ommit empty text frames and text on paths:

                       

                      var newContents = "My text I want to add";
                      
                      addTextToEveryParagraphOfStory(newContents);
                      
                      function addTextToEveryParagraphOfStory(/*String*/newContents)
                      {
                          if(app.documents.length == 0){return};
                          if(app.documents[0].stories.length == 0){return};
                      
                          var doc = app.documents[0];
                          var storiesArray = doc.stories.everyItem().getElements();
                      
                          for(var n=0;n<storiesArray.length;n++)
                          {
                              // Check:
                              if(storiesArray[n].paragraphs.length == 0){continue};
                              storiesArray[n].paragraphs.everyItem().insertionPoints[0].contents = newContents;
                          };
                      };
                      

                       

                      Regards,
                      Uwe

                      1 person found this helpful
                      • 8. Re: How to define stories
                        Jump_Over Level 5

                        Hi Uwe,

                         

                        From my point of view to say that above "everyItem()_1_liner" returns error is to quick.

                        mDoc.stories.everyItem().paragraphs[0] --> could return error, since some stories have no paragraphs

                        but

                        mDoc.stories.everyItem().paragraphs.everyItem().insertionPoints[0] should not cause insertionPoints[0] is called only for existing paras here...

                         

                        Am I right?

                         

                        Jarek

                        • 9. Re: How to define stories
                          Laubender Adobe Community Professional & MVP

                          Hm. Yes, you are right.


                          I did my first test this morning with a special case:

                          You'll get an error, if all stories in a document are empty.

                           

                          Uwe

                          • 10. Re: How to define stories
                            Laubender Adobe Community Professional & MVP

                            So we could do the following to make sure it would work in every case without using a try/catch:

                             

                            var tempFrame = app.documents[0].textFrames.add({contents : "tempContents"});
                            app.documents[0].stories.everyItem().paragraphs.everyItem().insertionPoints[0].contents = "MyAddedText";
                            tempFrame.remove();
                            

                             

                            Uwe

                            • 11. Re: How to define stories
                              Laubender Adobe Community Professional & MVP

                              I always have to remember, that at least one instance of an object should be there, if we call everyItem() on it.
                              That's the real insight I should have remembered and was brought back by this discussion. Yes, I am enjoing that.

                               

                              Thank you for your contribution, Jarek!

                               

                              Uwe

                              • 12. Re: How to define stories
                                Johnwhite Level 1

                                thank you Uwe, thank you Jarek

                                thank you all guys

                                 

                                thank so much

                                John