Skip to main content
Participant
August 10, 2013
Answered

How to get a date on different pages?

  • August 10, 2013
  • 1 reply
  • 1041 views

Hey everyone, I am working on a daily planner for the office and have a script that automatically generates the dates but I don´t know how to get each of the dates on a different page. I'd really appreciate if anyone could help me get each date generated by the script on teh following page of my 365 page document. The scrip is:

var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

var length = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

var days = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];

count = 0;

day = 0;

month = 0;

weekday = 5;

str = "";

while (count < 365)

{

count++;

str += days[weekday]+", "+(day+1)+" "+months[month]+"\r";

day++;

if (day >= length[month])

{

  day = 0;

  month++;

  if (month > 11)

   month = 0;

}

weekday = (weekday+1) % 7;

}

app.selection[0].contents = str;

Tanks!

This topic has been closed for replies.
Correct answer Sajeev Sridharan

Try this,

app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

app.activeDocument.zeroPoint = [0,0];

with(app.activeDocument.viewPreferences){

    horizontalMeasurementUnits=MeasurementUnits.points;

    verticalMeasurementUnits=MeasurementUnits.points;

    rulerOrigin=RulerOrigin.pageOrigin;

    }

var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

var length = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

var days = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];

count = 0;

day = 0;

month = 0;

weekday = 5;

str = [];

while (count < 365)

{

count++;

str[count]= days[weekday]+", "+(day+1)+" "+months[month]+"\r";

str1 =str.toString();

day++;

if (day >= length[month])

{

  day = 0;

  month++;

  if (month > 11)

   month = 0;

}

weekday = (weekday+1) % 7;

}

for(i=0;i<document.pages.length;i++)

{

var myTextframe = document.pages.textFrames.add({geometricBounds:[0,0,50,150]});

myTextframe.insertionPoints[0].contents=str[i+1];

}

Vandy

1 reply

Sajeev SridharanCorrect answer
Legend
August 10, 2013

Try this,

app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

app.activeDocument.zeroPoint = [0,0];

with(app.activeDocument.viewPreferences){

    horizontalMeasurementUnits=MeasurementUnits.points;

    verticalMeasurementUnits=MeasurementUnits.points;

    rulerOrigin=RulerOrigin.pageOrigin;

    }

var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

var length = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

var days = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];

count = 0;

day = 0;

month = 0;

weekday = 5;

str = [];

while (count < 365)

{

count++;

str[count]= days[weekday]+", "+(day+1)+" "+months[month]+"\r";

str1 =str.toString();

day++;

if (day >= length[month])

{

  day = 0;

  month++;

  if (month > 11)

   month = 0;

}

weekday = (weekday+1) % 7;

}

for(i=0;i<document.pages.length;i++)

{

var myTextframe = document.pages.textFrames.add({geometricBounds:[0,0,50,150]});

myTextframe.insertionPoints[0].contents=str[i+1];

}

Vandy

Participant
August 10, 2013

Thanks! Didnt think would have the answer this quickly!

Jump_Over
Legend
August 10, 2013

Hi,

Notice that there is a calendar ready to use, so your string builder could be simpler and more flexible:

var mDoc = app.activeDocument, mGBounds = [0,0,50,150],

          mYear=2013, mMonth = 0, mDay,

          currDate, currString;

mDoc.zeroPoint = [0,0];

with(mDoc.viewPreferences){

    horizontalMeasurementUnits=MeasurementUnits.points;

    verticalMeasurementUnits=MeasurementUnits.points;

    rulerOrigin=RulerOrigin.pageOrigin;

    }

// to prepare date string and place it on page

for (mDay = 1; mDay < 366; mDay ++)

          {

          currDate = new Date(mYear,mMonth,mDay).toDateString();

          currString = currDate.replace(/(\b\w+\b)\s(\b\w+\b)\s(0?)(\d+)\s(\b\w+\b)/, "$1, $4 $2");

          mDoc.pages[mDay -1].textFrames.add({geometricBounds: mGBounds, contents: currString})

          }

\s(0?) - is to eliminate leading zeros

"$1, $4 $2" - is to recompose a date string

Jarek