11 Replies Latest reply on Nov 16, 2011 1:29 AM by sabrinagelbart

    How to add character styles to 2012 day by day calendar script

    rwl_1976

      I need help with this script please;

       

      var days = [ "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ];
      var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      var length = [ 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 ];
      count = 0;
      day = 0;
      month = 0;
      weekday = 3;
      str = "";
      while (count < 365)
      {
        count++;
        str += days[weekday]+", "+months[month]+", "+(day+1)+"\r";
        day++;
        if (day >= length[month])
        {
         day = 0;
         month++;
         if (month > 11)
          month = 0;
        }
        weekday = (weekday+1) % 7;
      }
      app.selection[0].contents = str;0000000000000000000000000000

       

      And I need to edit this so I  can add character styles as per the image attached. The fonts i want to use is Din regular 10pt for the day and month and Din Bold 26pt for the date. The day is grey, month is black and the date is red.

       

      Please see the image below, the top graphic is how the script displays the script and the bottom one is how i want it to display.

       

      script_visual.jpg

       

      I'm new to adding scripts to InDesign so any help is welcome. I have a deadline for this Friday to have my first draft completed.

       

      Thanks

       

      Richard

        • 1. Re: How to add character styles to 2012 day by day calendar script
          John Hawkinson Level 5

          Javascript will do date calculations for you.

          See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

          Rolling your own code to do this is inadvisable [and mostly unreadable].

           

          I suggest you divorce the date calculations from the formatting. You'll need to format each string seperately, of course. So concatenating them into a single variable (str) is not going to work well.

          • 2. Re: How to add character styles to 2012 day by day calendar script
            rwl_1976 Level 1

            I also need to combine Saturday and Sunday into one page so it reads as Sat/Sun.

            • 3. Re: How to add character styles to 2012 day by day calendar script
              [Jongware] Most Valuable Participant

              John Hawkinson wrote:

               

              Rolling your own code to do this is inadvisable [and mostly unreadable].

               

              Well I was pretty proud of it at the time as it was just a quick-and-dirty.

               

              Jayhawk, how would you create a calender then?

              • 4. Re: How to add character styles to 2012 day by day calendar script
                [Jongware] Most Valuable Participant

                rwl_1976 wrote:

                 

                I also need to combine Saturday and Sunday into one --

                 

                That'd be a "Satansday"

                • 5. Re: How to add character styles to 2012 day by day calendar script
                  rwl_1976 Level 1

                  Mmmm, thanks but doesn't help!

                  • 6. Re: How to add character styles to 2012 day by day calendar script
                    [Jongware] Most Valuable Participant

                    How would you get your required result using character styles?

                    • 7. Re: How to add character styles to 2012 day by day calendar script
                      [Jongware] Most Valuable Participant

                      Oh okay, had some fun with it. Do check your dates, though, as I spotted some numerical errors in the script you quoted. And 2012 is a leap year to boot.

                       

                      This script assumes the text cursor is standing inside an otherwise empty text frame, and you have three character styles "Day", "Month", and "Date" predefined. See the image for what it's supposed to look like after running.

                       

                      var days = [ "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ];
                      var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
                      var length = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
                      count = 0;
                      day = 0;
                      month = 0;
                      weekday = 1;
                      
                      
                      try {
                                dayStyle = app.activeDocument.characterStyles.item("Day");
                      } catch (e)
                      {
                                alert ("Wot no Day style");
                      }
                      try {
                                monthStyle = app.activeDocument.characterStyles.item("Month");
                      } catch (e)
                      {
                                alert ("Wot no Month style");
                      }
                      try {
                                dateStyle = app.activeDocument.characterStyles.item("Date");
                      } catch (e)
                      {
                                alert ("Wot no Date style");
                      }
                      
                      
                      while (count < 366)
                      {
                                count++;
                                if (weekday < 2)
                                {
                                          if (weekday == 0)
                                          {
                                                    app.selection[0].parentStory.insertionPoints[-1].appliedCharacterStyle = dayStyle;
                                                    app.selection[0].parentStory.insertionPoints[-1].contents = "Sat/Sun\n";
                                                    app.selection[0].parentStory.insertionPoints[-1].appliedCharacterStyle = monthStyle;
                                                    app.selection[0].parentStory.insertionPoints[-1].contents = months[month]+"\t";
                                                    app.selection[0].parentStory.insertionPoints[-1].appliedCharacterStyle = dateStyle;
                                                    app.selection[0].parentStory.insertionPoints[-1].contents = String(day+1)+"\r"
                                          }
                                } else
                                {
                                                    app.selection[0].parentStory.insertionPoints[-1].appliedCharacterStyle = dayStyle;
                                                    app.selection[0].parentStory.insertionPoints[-1].contents = days[weekday]+"\n";
                                                    app.selection[0].parentStory.insertionPoints[-1].appliedCharacterStyle = monthStyle;
                                                    app.selection[0].parentStory.insertionPoints[-1].contents = months[month]+"\t";
                                                    app.selection[0].parentStory.insertionPoints[-1].appliedCharacterStyle = dateStyle;
                                                    app.selection[0].parentStory.insertionPoints[-1].contents = String(day+1)+"\r"
                                }
                                day++;
                                if (day >= length[month])
                                {
                                          day = 0;
                                          month++;
                                          if (month > 11)
                                                    month = 0;
                                }
                                weekday = (weekday+1) % 7;
                      }
                      

                       

                      Sample:

                      Screen Shot 2011-11-15 at 10.15.51 PM.png

                      • 8. Re: How to add character styles to 2012 day by day calendar script
                        John Hawkinson Level 5

                        Jayhawk, how would you create a calender then?

                        No 'Jay' please, just 'jhawk'! Sorry for the delay.

                         

                        Like this. No knowledge of leap years required, the only arithmetic is adding the milliseconds in one day each time:

                         

                        var
                          year = (new Date).getFullYear(), // the current year
                          day = new Date(year, 0, 1), // first day of year
                          msPerDay = 3600*24*1000, // milliseconds in one day
                          parts; // split up locale date string.
                        while (day.getFullYear() === year) {
                            // Result: Tuesday, November 15 2011
                            parts = day.toLocaleDateString().split(" ");
                            $.writeln("Day: "+parts[0]+ " Month: "+parts[1]+" Day: "+parts[2]);
                            day = new Date(day.valueOf()+msPerDay);
                        }
                        

                        Note that this unfortunately splits the result of day.toLocaleDateString() to get the full names of the days-of-week. You could instead use day.getDay() and get a numeric day-of-week and look it up in an array, but you would have locale problems. My way has problems if you have locale that gives the date components in a different order or not split with spaces. I guess you could try to generate a list of days-of-week in a locale-independent fashion somehow. But I'm going to ignore those kinds of complexity.

                        • 9. Re: How to add character styles to 2012 day by day calendar script
                          rwl_1976 Level 1

                          Thanks for the script, i get this error message though??? I have set up character styles for "day", "month" and "date" as you mentioned.

                           

                          Screen shot 2011-11-16 at 09.14.56.png

                          • 10. Re: How to add character styles to 2012 day by day calendar script
                            [Jongware] Most Valuable Participant

                            You typed in the script instead of copying? The correct line is

                             

                            dayStyle = app.activeDocument.characterStyles.item("Day");
                            
                            • 11. Re: How to add character styles to 2012 day by day calendar script
                              sabrinagelbart

                              Hey it's probably too late now but if you want an alternative to your script you can look at our automation plugins, which are great for calendars and diaries http://fuga-tech.com. You can create templates that build your whole document and can include text manipulation, advanced positioning, styles, all that stuff, and we already have a bunch of premade ones available that you might be able to adapt for your purposes. They're paid but we tried to make them pretty affordable. If you have any questions or want some help making your template you can e-mail me at sabrina.gelbart@fuga-tech.com.