13 Replies Latest reply on Dec 17, 2010 2:37 PM by IT.Coordinator

    Need some help with this code.

    IT.Coordinator

      var myDoc = app.documents[0]
      var mySel = app.selection[0]
      var myStory = mySel.parentStory; // Now we are pointing to the entire story
      var myHolidayStyle1 = "Holiday-Header" // Header 'day of the week'
      var myHolidayStyle2 = "Holiday-Sub-Heading-Date" // Header 'month and day'
      var myHolidayStyle3 = "Holiday-Header-Body" // Lead Paragraph 'default paragraph style'

       

      if (mystory = "Monday" ) {
          paragraphs(0).appliedparagraphstyles = myHolidayStyle1;
          }
      else if (mystory = "Tuesday") {
          mystory.paragraphs(0). appliedparagraphstyles = myHoldiayStyle1;
          }

      else if (mystory = "Wednesday") {
          mystory.papragraph(0).appliedparagraphstyles = myHolidayStyle1;
          }
      else if (mystory = "Thursday") {
          mystory.paragraphs(0).appliedparagraphstyles = myHolidayStyle1;
          }
      else if (mystory = "Friday")  {
          mystory.paragraphs(0).appliedparagraphstyle = myHolidayStyle1;
          }
      else if (mystory = "Saturday") {
          mystory.paragraphs(0).appliedparagrahstyles = myHolidayStyle1;
          }
      else if (mystory = "Sunday") {
          mystory.paragraphs(0).appliedparagraphstyles = myHolidayStyle1;
          }

       


      // it finds if in the selection of the month and day if they equal for example Decmeber 15 then applies the HolidaySub-Heading-Date
      if   (mystory = "December 15") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 15") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 16") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 17") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 18") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 19") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 20") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 21") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 22") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 23") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 24") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      else if (mystory = "December 25") {
          mystory. paragraphs(1).appliedparagraphstyles = myHolidayStyle2;
          }
      // If text doesn't  equal Day of the week like Monday and doesn't equal month and date like Decmber 14
      // then the document loops into doing the the rest of the document in Holiday-Header-Body
      // not sure if this loop will stop once it reaches another day of the week and then repeat the above tasks again.
      if (mystory =!  [myHolidayStyle0], [myHolidayStyle1]) { // not sure if I did this IF selection not equal Holidaystyle 0 and style 1 then perform loop, correctly???
          for (loop=0; loop<myStory.paragraphs.length; loop++)
        myStory.paragraphs[loop].appliedParagraphStyle = myHolidayStyle3;
        }

       

       

       

      =============================

      ==========================

      I'm getting a error saying Paragraph is not a function, but nor is mystory.paragraph(0) , so i'm just trying to figure out what synax goes before that.

      =============================

       

       

      The text that is bold is the prolbem i'm having. I just got  a Javascript bible on how to program in javascript. How would i make that function work. I'm sure its simple. I'm just hoping I have If some Then ({) command follow by what I want it to do is correct. And I'm understanding this. there is so many different syntax's to choose from, any help would be appreciated. I work for a Newspaper company, and Indesign CS3 Javascript coding is a bit diffrent from normal javascript.

        • 1. Re: Need some help with this code.
          herr mueller Level 1

          Should read "mystory.paragraphs [ 0 ]"

           

          You are using Array access notation to access an item in the paragraphs Array collection.

          • 2. Re: Need some help with this code.
            IT.Coordinator Level 1

            hmmm.... I'm setting a syntex error ..... "Error String: undefined is not an object"

             

            Line: 9

            Source : mystory.paragraph[0].appliedParagraphStyle = myHolidayStyle1;

             

            any idea's?

             

            I'm in CS3 Indesign.

            • 3. Re: Need some help with this code.
              Marijan Tompa [tomaxxi] Level 4

              JavaScript is Case sensitive language.

               

              You declared myStory at beginning, and after that in code you are using mystory.

               

              Hope that hepls.

               

              --

              tomaxxi

              http://indisnip.wordpress.com/

              • 4. Re: Need some help with this code.
                IT.Coordinator Level 1

                hmmm..... I tried change it to both being myStory and then the Var myStory     

                 

                but its still giving me a syntex error.

                 

                I thank you for your advice, but still getting a error ,

                • 5. Re: Need some help with this code.
                  herr mueller Level 1

                  /*     IN CS4 YOU CAN USE     */

                  //Here is how to correctly reference a pragraph style;

                  var myStyle = app.activeDocument.paragraphStyles.item ( "Holiday-Header" ) ;

                   

                   

                  /*     IN CS5 YOU CAN USE    */

                  //Here is how to correctly reference a pragraph style;

                  var myStyle = app.activeDocument.paragraphStyles.itemByName ( "Holiday-Header" ) ;

                  • 6. Re: Need some help with this code.
                    IT.Coordinator Level 1

                    the seemed to help resolve that problem, But I'm curious would this work.

                     

                    if   (mystory = "December 15") {
                        mystory. paragraphs[1].appliedparagraphstyle = myStyle1;
                        }

                     

                     

                     

                    I rememeber from VB days if I designated a Word and then a Numeric number as a varible it gave me a syntex error. is it possible to make the script define if the month of December has a number after it ot apply the paragraph style to the month and day like this

                     

                    December 15

                     

                    just making sure the coding is right and it wont return with a problem.

                     

                     

                    Also I noticed that I cant have a nested

                     

                    If

                     

                    else

                     

                    else

                     

                    else

                     

                    else

                     

                    else

                     

                     

                    ===========

                    it has to be

                    ===========

                    if

                     

                    else

                     

                    if

                     

                    else

                     

                    if

                     

                    .... etc.....

                     

                    is that correct? or could I use if,else,then statements or is that only limited to 3 statements? I coded VB like 4 years ago, so i'm trying to get back in the swing of programming. I bought a 900 page book called the 'Javascript Bible' from Barnes and Noble last night, cause its the Hundreds of Commands that going to drive me crazy..... knowing when to use them all.

                     

                     

                    also I noticed something in this book, so I'm correct in my use of the syntex commands, this loop below would apply the Last paragraph style to the rest of the text after, the day of the week and month and day lines, it would apply this to the paragraphs below that.

                     

                    the && are used to include more than one variable to a function?

                    the != is a not equal to?

                     

                     

                    // not sure if I did this IF selection not equal Holidaystyle 0 and style 1 then perform loop, correctly???


                    if (mystory !=  (myStyle1 && myStyle2))

                    {
                        for (loop=0; loop<myStory.paragraphs.length; loop++)
                      myStory.paragraphs[loop].appliedParagraphStyle = myStyle3;
                      }

                     

                     

                    My question with this loop is would it work, I just slap it together from this book which had a loop, I'm just curious if there is a way after the loops with (I know there is, but to know which one of these bazzlion javascript command to use, that the true question LOL), i'm guessing a then statement to make once it finishes up with the paragraphs to be formated that if it comes to a point were the day of the week comes back, it would start another array? IS THAT POSSIBLE?

                     

                    if you can help in pieces thats fine, I know its allot to ask.

                    • 7. Re: Need some help with this code.
                      Harbs. Level 6

                      To chain if/else, you need if(x) else if(y) else if(z) else

                       

                      Harbs

                      1 person found this helpful
                      • 8. Re: Need some help with this code.
                        IT.Coordinator Level 1

                        sweet, I'm learning javascript now, its making sense.

                         

                        But this bloody same syntex error is plagueing me again.

                         

                        Error number: 21

                        Error String: undefined is not an object

                         

                        Engine: main

                        File : C:\bla blablablabl\blablablabl\

                        Line:12

                        Source : myStory.paragraphs.appliedParagraphStyle = myStyle;

                         

                        ==========================

                        ---------------------------------------

                        ========================

                         

                         

                        //var myDoc = app.documents[0]
                        var mySel = app.selection[0];
                        var myStory = mySel.parentStory; // Now we are pointing to the entire story
                        var myStyle = app.activeDocument.paragraphStyles.item ( "Holiday-Header" ) ;
                        var myStyle1 = app.activeDocument.paragraphStyles.item ( "Holiday-Sub-Heading-Date" ) ;
                        var myStyle3 = app.activeDocument.paragraphStyles.item ( "Holiday-Header-Body" ) ;
                        //var myHolidayStyle1 = "Holiday-Header"; // Header 'day of the week'
                        //var myHolidayStyle2 = "Holiday-Sub-Heading-Date";// Header 'month and day'
                        //var myHolidayStyle3 = "Holiday-Header-Body";// Lead Paragraph 'default paragraph style'

                         

                        if (myStory = "Monday") {
                            myStory.paragraphs[0].appliedParagraphStyle = myStyle; <---This is Line 12
                            }
                        else if  (mystory = "Tuesday") {
                            mystory.paragraphs[0]. appliedParagraphStyle = myStyle;
                            }
                        if  (mystory = "Wednesday") {
                            mystory.papragraph[0].appliedparagraphstyle = myStyle;
                            }
                        else if (mystory = "Thursday") {
                            mystory.paragraphs[0].appliedparagraphstyle = myStyle;
                            }
                        if  (mystory = "Friday")  {
                            mystory.paragraphs[0].appliedparagraphstyle = myStyle;
                            }

                        • 9. Re: Need some help with this code.
                          Marc Autret Level 4

                          First, equality tests need == (double equal signs):

                           

                          if (myStory == "Monday")

                           

                          Anyway, I don't think this will solve all problems in your code...


                          Indeed:

                           

                          if (myStory == "any text")

                           

                          doesn't make sense (assumed that myStory actually is a Story object).

                           

                          You should probably try sth like:

                           

                          if (myStory.contents == "any text")

                           

                           

                          @+

                          Marc

                          1 person found this helpful
                          • 10. Re: Need some help with this code.
                            IT.Coordinator Level 1

                            Okie, I'm making progress now. Because well it didnt crash, but then again, LOL the script didnt apply the paragraph styles when I selected the text!

                             

                            any suggestions?

                             

                             

                            I'm also getting a weird error now with the loop, but I also dont think its working because maybe the myStyle and myStle1 aren't applying themselves correctly, in the first part of the script.

                             

                            this is the Error Message:

                             

                            Error Number : 30477

                            Error String: Invalid value of set propert 'appliedParagraphStyle'. Expected ParagraphStyle or String, but Recieved nothing.

                             

                            Line: 110

                            Source: myStory.paragraphs[loop].appliedParagraphStyle = myStyle3;

                             

                             

                             

                             

                             

                             

                            //var myDoc = app.documents[0]
                            var mySel = app.selection[0];
                            var myStory = mySel.parentStory; // Now we are pointing to the entire story
                            var myStyle = app.activeDocument.paragraphStyles.item ( "Holiday-Header" ) ;
                            var myStyle1 = app.activeDocument.paragraphStyles.item ( "Holiday-Sub-Heading-Date" ) ;
                            var myStyle3 = app.activeDocument.paragraphStyles.item ( "Holiday-Header-Body" ) ;

                             


                            if (myStory.contents == "Monday" ) {
                                myStory.paragraphs.appliedParagraphStyle = myStyle;
                                }
                            else if  (myStory.contents == "Tuesday") {
                                myStory.paragraphs[0]. appliedParagraphStyle = myStyle;
                                }
                            if  (myStory.contents == "Wednesday") {
                                myStory.papragraph[0].appliedparagraphstyle = myStyle;
                                }
                            else if (myStory.contents == "Thursday") {
                                myStory.paragraphs[0].appliedparagraphstyle = myStyle;
                                }
                            if  (myStory.contents == "Friday")  {
                                myStory.paragraphs[0].appliedparagraphstyle = myStyle;
                                }
                            else if (myStory.contents == "Saturday") {
                                myStory.paragraphs[0].appliedparagrahstyle = myStyle;
                                }
                            if  (myStory.contents == "Sunday") {
                                myStory.paragraphs[0].appliedparagraphstyle = myStyle;
                                }

                             

                            // it finds if in the selection of the month and day if they equal for example Decmeber 15 then applies the HolidaySub-Heading-Date
                            if   (myStory.contents == "December 2") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 3") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if   (myStory.contents == "December 4") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 5") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if   (myStory.contents == "December 6") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 7") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if   (myStory.contents == "December 8") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 9") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if   (myStory.contents == "December 10") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 11") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if   (myStory.contents == "December 12") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 13") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if   (myStory.contents == "December 14") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 15") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if  (myStory.contents == "December 16") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 17") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if  (myStory.contents == "December 18") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else  if (myStory.contents == "December 19") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if  (myStory.contents == "December 20") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 21") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if  (myStory.contents == "December 22") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                 }
                            else if (myStory.contents == "December 23") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            if  (myStory.contents == "December 24") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                }
                            else if (myStory.contents == "December 25") {
                                myStory. paragraphs[1].appliedparagraphstyle = myStyle1;
                                } 

                             

                            // If text doesn't  equal Day of the week like Monday and doesn't equal month and date like Decmber 14
                            // then the document loops into doing the the rest of the document in Holiday-Header-Body
                            // not sure if this loop will stop once it reaches another day of the week and then repeat the above tasks again.
                            // not sure if I did this IF selection not equal mystyle and mystyle1 then perform loop, is done correctly???

                             


                            if (myStory.contents !=  (myStyle && myStyle1)) {
                               for (loop=0; loop<myStory.paragraphs.length; loop++)
                              myStory.paragraphs[loop].appliedParagraphStyle = myStyle3;
                              }

                             

                             
                                I'm truly grateful for the Support and Assistance everyone has been providing me, I'm learning, and I thank you all for your help.

                             

                             

                             

                            • 11. Re: Need some help with this code.
                              IT.Coordinator Level 1

                              is there a debug tool somewere that can tell me what these error message are saying?

                              • 12. Re: Need some help with this code.
                                Marc Autret Level 4

                                Dunno exactly what your script is supposed to do, but I suggest you simplify the code. E.g:

                                 

                                 

                                function main()
                                {
                                     var doc = app.activeDocument,
                                          pStyles = doc.paragraphStyles,
                                          myStyles = {
                                               weekDay: pStyles.itemByName("Holiday-Header"),
                                               december: pStyles.itemByName("Holiday-Sub-Heading-Date"),
                                               other: pStyles.itemByName("Holiday-Header-Body")
                                               };
                                
                                     // Assuming that a text frame is selected:
                                     var story = app.selection[0].parentStory,
                                          txt = story.contents;
                                     
                                     var     regWeekDays = /^Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday$/,
                                          regDecember = /^December ([2-9]$)|(1[0-9]$)|(2[0-5]$)/;
                                     
                                     if( regWeekDays.test(txt) )
                                          {
                                          story.appliedParagraphStyle = myStyles.weekDay;
                                          return;
                                          }
                                     if( regDecember.test(txt) )
                                          {
                                          story.appliedParagraphStyle = myStyles.december;
                                          return;
                                          }
                                     story.appliedParagraphStyle = myStyles.other;
                                }
                                
                                try{ main(); }
                                catch(_){ alert(_); }
                                
                                

                                 

                                 

                                @+

                                Marc

                                • 13. Re: Need some help with this code.
                                  IT.Coordinator Level 1

                                  Yes I know my coding was a bit extreme.

                                   

                                  But basically the script is suppose to apply 3 paragrah styles to a document for use in a Newspaper artice (lol Irony from my account name O.o)

                                   

                                  Like

                                   

                                  Thursday (this is 'Holiday-Header')

                                   

                                  December 15 (this is 'Holiday-Sub-Heading-Date')

                                   

                                  Gracie’s Christmas Tree: Books and Prints in Milford, PA, is hosting this fund raiser throughout the holidays to benefit the Pike County Humane Society. Donors are asked to bring a gift for the shelter to place under Gracie’s tree canned and dry dog and cat food, old blankets and towels, collars, leashes, kitty litter, large disposable aluminun lasagna pans, paper towels, laundry detergent, bleach and - always appreciated - cash. The tree is named after Gracie, a year-old standard poodle adopted from the shelter. Books and Prints. Pear Alley, Milford, PA. 570-296-4777

                                   

                                  ^-----(this is 'Holiday-Header-Body')    ^^^^^------ this one will appear often

                                   

                                  basiucally its like a newpaper article, Day of the week, and the date of December, then all the articles(paragraphs) that are below that Decmber 15th are made into the Paragraph Style 'Holiday-Header-Body'

                                   

                                  But What I was trying to do is once it applies this for example:

                                   

                                  Tuesday

                                   

                                  December 14

                                   

                                  Paragraph.........

                                   

                                  (once it gets to another like day of the week like this below, I wanted it repeat all over again. until it came to the end of the selection)

                                   

                                   

                                  Wednesday

                                   

                                  December 15

                                   

                                  paragraph....

                                   

                                   

                                  Does that make sense?

                                   

                                   

                                   

                                  But I follow your coding.