[Numbers]ナンバーズで祭日入りカレンダー
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # |
006 | # |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | ##自分環境がos12なので2.8にしているだけです |
010 | use AppleScript version "2.8" |
011 | use framework "Foundation" |
012 | use scripting additions |
013 | |
014 | property refMe : a reference to current application |
015 | |
016 | ####設定項目 |
017 | ##文字サイズ |
018 | set numFontSize to 14 as number |
019 | ##フォント名はPS名? |
020 | set strFontName to "Osaka-Mono" as text |
021 | |
022 | |
023 | ######################## |
024 | #####ここから処理 |
025 | ####日付情報の取得--> 今の『年』の数値を求める |
026 | set ocidDate to refMe's NSDate's |date|() |
027 | set ocidCalendar to refMe's NSCalendar's autoupdatingCurrentCalendar() |
028 | set ocidCalendarUnitYear to refMe's NSCalendarUnitYear |
029 | set ocidCalendarUnitMonth to refMe's NSCalendarUnitMonth |
030 | set ocidDateComponents to ocidCalendar's components:((ocidCalendarUnitYear) + (ocidCalendarUnitMonth)) fromDate:ocidDate |
031 | set numSetYear to (ocidDateComponents's |year|) as integer |
032 | set numSetMonth to (ocidDateComponents's |month|) as integer |
033 | #####各種リスト |
034 | set listWeekDay to {"日", "月", "火", "水", "木", "金", "土"} as list |
035 | set listYear to {(numSetYear - 1), (numSetYear), (numSetYear + 1)} as list |
036 | set listMonth to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} as list |
037 | ######################## |
038 | #####ダイアログを前面に |
039 | tell current application |
040 | set strName to name as text |
041 | end tell |
042 | ####スクリプトメニューから実行したら |
043 | if strName is "osascript" then |
044 | tell application "Finder" to activate |
045 | else |
046 | tell current application to activate |
047 | end if |
048 | #####年ダイアログを出す |
049 | try |
050 | set objResponseYear to (choose from list listYear with title "年選択" with prompt "年を選択してください" default items (item 2 of listYear) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) |
051 | on error |
052 | log "エラーしました" |
053 | return |
054 | end try |
055 | if objResponseYear is false then |
056 | return |
057 | end if |
058 | set numSetYear to (objResponseYear) as integer |
059 | ######################## |
060 | #####月ダイアログを出す |
061 | if numSetMonth = 12 then |
062 | set numSetMonth to 1 as integer |
063 | else |
064 | set numSetMonth to numSetMonth + 1 as integer |
065 | end if |
066 | try |
067 | set objResponseMonth to (choose from list listMonth with title "月選択" with prompt "月を選択してください" default items (item (numSetMonth) of listMonth) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) |
068 | on error |
069 | log "エラーしました" |
070 | return |
071 | end try |
072 | if objResponseMonth is false then |
073 | return |
074 | end if |
075 | set numSetMonth to (objResponseMonth) as integer |
076 | ######################## |
077 | tell application "Numbers" to launch |
078 | ##ナンバーズ 表題部のみ処理 |
079 | tell application "Numbers" |
080 | make new document |
081 | tell front document |
082 | activate |
083 | set strDociID to id as text |
084 | end tell |
085 | tell document id strDociID |
086 | tell front sheet |
087 | set name to "カレンダー" |
088 | tell front table |
089 | set name to "" & numSetYear & "年" & numSetMonth & "月のカレンダー" |
090 | set rangeTable to cell range |
091 | tell rangeTable |
092 | set background color to {65535, 65535, 65535} |
093 | set text color to {0, 0, 0} |
094 | set vertical alignment to center |
095 | set format to text |
096 | set font size to numFontSize |
097 | set font name to strFontName |
098 | set alignment to center |
099 | end tell |
100 | tell cell "A1" |
101 | set value to "" & numSetYear & "/" & numSetMonth & "" as text |
102 | end tell |
103 | tell cell "B1" |
104 | set value to "曜日" as text |
105 | end tell |
106 | set row count to 35 |
107 | end tell |
108 | end tell |
109 | end tell |
110 | end tell |
111 | |
112 | ######################## |
113 | #####カレンダー初期化 |
114 | set ocidCalendar to refMe's NSCalendar's calendarWithIdentifier:(refMe's NSCalendarIdentifierGregorian) |
115 | set ocidLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP") |
116 | ocidCalendar's setLocale:(ocidLocale) |
117 | #####日付コンポーネント初期化 |
118 | set ocidDateComponents to refMe's NSDateComponents's alloc()'s init() |
119 | ocidDateComponents's setYear:(numSetYear) |
120 | ocidDateComponents's setMonth:(numSetMonth) |
121 | set ocidDate to ocidCalendar's dateFromComponents:(ocidDateComponents) |
122 | ###指定月のカレンダーの最大日数-->繰り返し回数になる |
123 | set ocidMonthDateRange to ocidCalendar's rangeOfUnit:(refMe's NSCalendarUnitDay) inUnit:(refMe's NSCalendarUnitMonth) forDate:(ocidDate) |
124 | set numDateLength to ocidMonthDateRange's |length| |
125 | |
126 | |
127 | ######################## |
128 | ###祭日データJSONで取得 |
129 | set strURL to "https://holidays-jp.github.io/api/v1/date.json" as text |
130 | set ocidJsonURL to refMe's NSURL's URLWithString:(strURL) |
131 | set ocidOption to refMe's NSDataReadingMappedIfSafe |
132 | set listDownLoadData to refMe's NSData's dataWithContentsOfURL:(ocidJsonURL) options:(ocidOption) |error| :(reference) |
133 | set ocidJsonData to (item 1 of listDownLoadData) |
134 | ##################### |
135 | ###JSON初期化 してレコードに格納 |
136 | set listJSONSerialization to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidJsonData) options:0 |error| :(reference)) |
137 | set ocidJsonData to item 1 of listJSONSerialization |
138 | set ocidHolidayDict to refMe's NSDictionary's alloc()'s initWithDictionary:(ocidJsonData) |
139 | |
140 | ##################### |
141 | ###取得するカレンダーの月(処理としては不要なんだけど、今後の展開のために処理) |
142 | set numYear to ocidCalendar's component:(refMe's NSCalendarUnitYear) fromDate:(ocidDate) |
143 | set numMonth to ocidCalendar's component:(refMe's NSCalendarUnitMonth) fromDate:(ocidDate) |
144 | ###年はそのまま |
145 | set strYear to numYear as text |
146 | ###月はゼロ月 |
147 | set strMonth to (text -2 through -1 of ("00" & (numMonth as text))) as text |
148 | ###日付毎処理 |
149 | set numCntRows to 2 |
150 | |
151 | repeat with numDayNo from (1) to (numDateLength) |
152 | -->ここまでは年と月でのカレンダーなので |
153 | ###日付を入れて |
154 | (ocidDateComponents's setDay:(numDayNo)) |
155 | set ocidDate to (ocidCalendar's dateFromComponents:(ocidDateComponents)) |
156 | set ocidWeekDayClender to (ocidCalendar's components:(refMe's NSWeekdayCalendarUnit) fromDate:(ocidDate)) |
157 | ###日付 |
158 | log ocidDate as date |
159 | ###日付番号 |
160 | log numDayNo |
161 | set strDayNoZeroSupp to (text -2 through -1 of ("00" & numDayNo)) as text |
162 | ##################### |
163 | ###曜日処理 |
164 | set numWeekDayNo to (ocidWeekDayClender's |weekday|)'s intValue() |
165 | ###曜日番号 |
166 | log numWeekDayNo |
167 | ###曜日テキスト |
168 | set strWeekDayJp to (item numWeekDayNo of listWeekDay) as text |
169 | ###祭日取得用 |
170 | set strDateText to ("" & strYear & "-" & strMonth & "-" & strDayNoZeroSupp & "") as text |
171 | ###祭日取得 |
172 | set ocidHolidayValue to (ocidHolidayDict's valueForKey:(strDateText)) |
173 | ###祭日処理 |
174 | if ocidHolidayValue is (missing value) then |
175 | set numChkHoliday to 0 as integer |
176 | else |
177 | set numChkHoliday to 1 as integer |
178 | set strHolidayValue to ocidHolidayValue as text |
179 | log strHolidayValue |
180 | ####Cの列 |
181 | tell application "Numbers" |
182 | tell document id strDociID |
183 | tell front sheet |
184 | tell front table |
185 | tell cell ("C" & numCntRows & "") |
186 | ###Cの列に祭日名を入力 |
187 | set value to (strHolidayValue) as text |
188 | end tell |
189 | end tell |
190 | end tell |
191 | end tell |
192 | end tell |
193 | end if |
194 | |
195 | tell application "Numbers" |
196 | tell document id strDociID |
197 | tell front sheet |
198 | set name to "カレンダー" |
199 | tell front table |
200 | tell cell ("A" & numCntRows & "") |
201 | ###日付を入れて |
202 | set value to "" & strDayNoZeroSupp & "" as text |
203 | end tell |
204 | tell cell ("B" & numCntRows & "") |
205 | ###曜日を入れる |
206 | set value to strWeekDayJp as text |
207 | end tell |
208 | if strWeekDayJp is "日" then |
209 | tell row numCntRows |
210 | ###日曜日の色 |
211 | set background color to {64945, 37783, 61881} |
212 | set text color to {38078, 202, 22456} |
213 | end tell |
214 | else if strWeekDayJp is "土" then |
215 | ###土曜日の色 |
216 | tell row numCntRows |
217 | set background color to {41120, 61174, 59832} |
218 | set text color to {5140, 16191, 37008} |
219 | end tell |
220 | else |
221 | tell row numCntRows |
222 | ###平日の色 |
223 | set background color to {65535, 65535, 65535} |
224 | set text color to {0, 0, 0} |
225 | end tell |
226 | end if |
227 | if numChkHoliday = 1 then |
228 | tell row numCntRows |
229 | ###祭日の色 |
230 | set background color to {64945, 37783, 61881} |
231 | set text color to {38078, 202, 22456} |
232 | end tell |
233 | end if |
234 | end tell |
235 | end tell |
236 | end tell |
237 | end tell |
238 | |
239 | |
240 | set numCntRows to numCntRows + 1 |
241 | end repeat |
AppleScriptで生成しました |
| 固定リンク