iWork '06の Pages 2 が面白い。
Pages 2 は、日本語環境に対応していない...という批判(?)はよく聞くけど、なんの。横書きしかできなくてもルビが使えなくても、数式を埋め込むのが面倒でも、それでも個人の環境で便利に使える場面というのは多々ある。
ちょっと Web 見ただけでもなんとたくさんのテンプレートが無料で配布されていることか。Canon や Epson のサイトにいってみると横書きでもいいんだ、ということがよく分かる。なにかを紙に印刷するのが目的であれば、工夫次第で様々なことができる。Pages 2 があれば、もう無料のテンプレートいらないや。はがきだって(横書きだけど)できるんだぞぅ。
と、少々ワケの分からないことを宣いましたが。Pages 2 から AppleScript に対応しているんですね。まだまだできないことも多いけど。
ということで Pages を AppleScript で操ってカレンダーなんかを作ってみました。いろんな部分で手を抜いていますが、Pages を持っているならぜひ、お試しください。面白いですから(なんか変なことが起きても責任は持てませんが)。
Script Editor で開く
using terms from application "Pages"
-- for A4 size
property paperWidth : 21.0
property paperHeight : 29.7
property cellWidth : 2.8
property cellHeight : 2.4
property topMargin : 1.0
property bottomMargin : 1.5
property leftMargin : 0.5
property rightMargin : 0.5
-- calendar cells props
property strokeWidth : 0.15
property strokeColor : {26214, 26214, 26214}
property textFont : "Georgia"
property textSize : 14.0
property textAlignment : right
-- weekday cells props
property wCellWidth : cellWidth
property wCellHeight : 1.5
property wTextFont : "Optima-Bold"
property wTextSize : 18.0
property wTextAlignment : center
-- month cells props
property mCellWidth : cellWidth * 7
property mCellHeight : 2.0
property mTextFont : "Optima-ExtraBlack"
property mTextSize : 40.0
property mTextAlignment : center
property mTextColor : {46003, 46003, 46003}
property mTextTracking : 3.0
-- other props
property weekdayList : {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
end using terms from
set theDate to "2006 4 1" -- for debug
set theDate to date theDate -- for debug
set theDate to current date
tell application "Pages"
set ruler units to centimeters
set theDocument to make new document
tell theDocument
set top margin to topMargin
set bottom margin to bottomMargin
set left margin to leftMargin
set right margin to rightMargin
-- 印刷可能領域の算出
set contentsWidth to paperWidth - (right margin + left margin)
set contentsHeight to paperHeight - (top margin + bottom margin)
set tmp to my getCalendar(theDate)
set dateList to {}
repeat with thisItem in tmp
set thisItem to contents of thisItem
if thisItem is not {} then set end of dateList to thisItem
end repeat
-- カレンダーの出来上がりサイズ
set calendarWidth to cellWidth * 7
set calendarHeight to cellHeight * (count dateList)
--印刷可能領域よりカレンダーサイズが大きい場合は処理しない
if calendarWidth is greater than contentsWidth then return
-- 余白の算出
set blank to contentsWidth - calendarWidth
-- 最初のセルの開始位置
set x to left margin + (blank / 2)
set y to (contentsHeight - calendarHeight) + top margin
-- カレンダーの作成
set num to 0
repeat with i from 1 to (count dateList)
set currentWeek to item i of dateList
set dayCount to count currentWeek
repeat with j from 1 to 7
set num to num + 1
if j is greater than dayCount then
set thisItem to ""
else
set thisItem to (item j of currentWeek) as Unicode text
end if
set theProp to {width:cellWidth, height:cellHeight, horizontal position:x + (cellWidth * (j - 1)), vertical position:y, name:(num as Unicode text), object text:thisItem}
set newShape to make new shape with properties theProp
if (num mod 7) is 0 then
set thisColor to {0, 0, 65535}
else if (num mod 7) is 1 then
set thisColor to {65535, 0, 0}
else
set thisColor to {0, 0, 0}
end if
tell object text of newShape
set color of it to thisColor
end tell
end repeat
set y to y + cellHeight
end repeat
-- カレンダーセルの見た目の変更
tell shapes
set stroke width to strokeWidth
set stroke color to strokeColor
set wrap to none
set fill type to none
tell object text
set alignment to textAlignment
set font name to textFont
set font size to textSize
end tell
end tell
-- 曜日セルの作成
set weekdayPosition to (vertical position of first shape) - wCellHeight
repeat with i from 1 to 7
set newShape to make new shape
tell newShape
set horizontal position to x + (wCellWidth * (i - 1))
set vertical position to weekdayPosition
set width to wCellWidth
set height to wCellHeight
set wrap to none
set stroke type to none
set fill type to none
set object text to item i of weekdayList
set alignment of object text to wTextAlignment
set font name of object text to wTextFont
set font size of object text to wTextSize
end tell
end repeat
-- 月のセルを作成
set monthPosition to (vertical position of last shape) - mCellHeight
make new text box with properties {vertical position:monthPosition, height:mCellHeight, width:mCellWidth, horizontal position:x, name:"month"}
tell text box "month"
set thisYear to year of (theDate) as Unicode text
set thisMonth to month of (theDate) as Unicode text
set object text to thisMonth & space & thisYear
set alignment of object text to mTextAlignment
set font name of object text to mTextFont
set font size of object text to mTextSize
set tracking of object text to mTextTracking
set color of object text to mTextColor
end tell
say "Complete."
end tell
end tell
on getCalendar(selectedDate)
set theList to {{}, {}, {}, {}, {}, {}}
set numDaysInMonth to {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
set currentYear to year of selectedDate
set currentMonth to month of selectedDate as number
set day of selectedDate to 1
set time of selectedDate to 0
copy selectedDate to firstOfMonth
set startOffset to weekday of firstOfMonth as number
set daysInMonth to item currentMonth of numDaysInMonth
if ((currentMonth is 2) and (isLeap(currentYear))) then set daysInMonth to daysInMonth + 1
set dayLabel to 1
set num to 0
repeat with i from 1 to 6
set currentWeek to item i of theList
repeat with j from 1 to 7
set num to num + 1
if (num is less than startOffset) then
set end of currentWeek to ""
else if (num is greater than or equal to (daysInMonth + startOffset)) then
exit repeat
else
set end of currentWeek to dayLabel
set dayLabel to dayLabel + 1
end if
end repeat
end repeat
return theList
end getCalendar
on isLeap(theYear)
return (((theYear mod 4) is 0 and ((theYear mod 100) is not 0)) or (theYear mod 400) is 0)
end isLeap