Pages: Calendar builder script in Apple's Pages.app

I use a bit of applescript to utilize “cal” in a “do shell script”. After a bit of massaging, I put the results in the clipboard and paste them automatically in Pages. Calendar Template included.

Calendar Script and Template here:
http://pagesuser.com/smf/index.php?topic=73.0

GCL
-enjoy!

OR, Here is the script!
Read the instructions below on how to set up a table in Pages for the script to excecute successfully…

(If you haven’t done much with tables in Pages, I suggest using my link above. I have both a basic template and a Nice photo calendar template using tables, ready to use, in the link above).


(*
"CalendarPage(s)"   -- a calendar script for Pages 2.01

PURPOSE:
This script will;
1.  Create a single calendar page for any one month, OR
2.  Create 12 calendars pages for an entire year.

REQUIREMENTS:
This script is somewhate dependent on the templates "Calendar1" and "Calendar12"
These calendar templates are for 1 month and 12 months respectively.
There nothing special about them except there is some minor formatting that the script
looks for in order to execute succussfully.

TABLE SETUP:
Insert a table into pages with a 7X7 grid. Format the entire first row with "Merge fields", and put the text "&C" centered in that same first row. The script will look for this text and paste the Month and year there. Then, the script will continue to insert the numbered days.

INSTALLATION: 
Place the Calendar templates, "Calendar1" , "Calendar12", and "PhotoCalendar" in;
	~/Library/Application Support/iWork/Pages/Templates/My Templates/Calendar1
	~/Library/Application Support/iWork/Pages/Templates/My Templates/Calendar12

Place the script "CalendarPage(s)" in;
	~/Library/Scripts/CalendarPage(s).scpt

DIRECTIONS:
Load the appropriate calendar template:  "Calendar1" or "Calendar12".
These calendar templates are for 1 month and 12 months respectively.

Enter the month and year in the format ("01/2006") and select 1 or 12 month button to execute script.
Script will SLOWLY build your calendar. Then, modify your calendar to your hearts content.

You can start the calendar on any month/year. I used this feature so you can create a
school calendar, for instance, from June to June. 

______________________________________________________________________________
*)

display dialog "Start calendar on (\"01/2006\"): " default answer "01/2006" buttons {"Cancel", "12-Months", "1-Month"} default button "1-Month"
copy the result as list to {text_returned, button_pressed}

if button_pressed is not "Cancel" then
	set _month to (text 1 through 2 of text_returned as number)
	
	set _year to (text 4 through 7 of text_returned as number)
	
	if button_pressed is "12-Months" then
		set _noOfmos to 12
	else
		set _noOfmos to 1
	end if
	
	repeat with j from 1 to _noOfmos
		do shell script "cal " & _month & " " & _year & " | pbcopy"
		
		-- Calc the number of indents(tabs) in the first week by subtracting the number of fields from 7. 
		set sedScript to "echo \"8-`cal " & _month & " " & _year & " | grep -n . | grep \"3:\" | awk '{ printf \"%s\\n\", NF}'`\" |bc"
		set _indents to (do shell script sedScript)
		
		set _indentfirstweek to ""
		repeat with i from 1 to _indents
			set _indentfirstweek to (_indentfirstweek & " " & return)
			set i to i + 1
		end repeat
		
		
		-- CHANGE SPACES TO TABS SO YOU'RE NOT LIMITED TO FIXED WIDTH FONTS
		--"pbpaste | sed -e 's/^space\\([0-9,S]\\)/\\1/' | pbcopy"
		set sedScript to "pbpaste | sed -e 's/^ *\\([0-9,A-z]\\)/\\1/' | pbcopy" -- delete spaces in first column only
		do shell script sedScript
		
		-- globally replace any# spaces with one tab
		--"pbpaste | sed -e 's/spacespace*\\([0-9,A-z]\\)/" & tab & "\\1/g' | pbcopy"
		set sedScript to "pbpaste | sed -e 's/  *\\([0-9,A-z]\\)/" & tab & "\\1/g' | pbcopy"
		do shell script sedScript
		
		-- fix tab in title 
		set sedScript to "pbpaste | sed -e 's/\\(^.*\\)" & tab & "\\([0-9][0-9][0-9][0-9]\\)/\\1 \\2/' | pbcopy"
		do shell script sedScript
		
		-- fix indent in first week 1 day only
		set sedScript to "pbpaste | sed -e 's/^\\([1]\\)$/" & _indentfirstweek & "\\1/' | pbcopy"
		do shell script sedScript
		
		-- fix indent in first week when 1+ days
		set sedScript to "pbpaste | sed -e 's/^\\([1]" & tab & ".*\\)$/" & _indentfirstweek & "\\1/' | pbcopy"
		do shell script sedScript
		
		-- TABLES: replace return char with tab
		set sedScript to "pbpaste | tr '\\t' '\\n'| pbcopy"
		do shell script sedScript
		-- month variable increases when printing consecutive months 
		set _month to _month + 1
		
		
		-- Paste
		tell application "Pages"
			activate
			-- For printing into tables
			--set _myCal to do shell script "pbpaste"
			
			-- Find the table start
			
			tell application "System Events" to (keystroke "f" using {command down})
			tell application "System Events" to (keystroke "&C")
			tell application "System Events" to (keystroke tab)
			delay 1
			tell application "System Events" to (keystroke return)
			delay 1.7
			tell application "System Events" to (keystroke "." using {command down})
			delay 1.7
			tell application "System Events" to (keystroke (ASCII character 30)) --arrow up very important; gets out of cell edit mode
			delay 1.7
			tell application "System Events" to (keystroke "v" using {command down, option down, shift down})
			
		end tell
		set j to j + 1 --  increment j  for number of months user entered
		
		-- increase year if you're printing 24 mos. or a June to June calendar
		if _month = 13 then
			set _month to 1
			set _year to _year + 1
		end if
		
	end repeat
	
end if
beep -- done