#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application
####設定項目
set numFontSize to 14 as number
##set strFontName to "Osaka−等幅" as text
##set strFontName to "Arial Unicode MS" as text
set strFontName to "メイリオ" as text
########################
#####ここから処理
####日付情報の取得--> 今の『年』の数値を求める
set ocidDate to refMe's NSDate's |date|()
set ocidCalendar to refMe's NSCalendar's autoupdatingCurrentCalendar()
set ocidCalendarUnitYear to refMe's NSCalendarUnitYear
set ocidCalendarUnitMonth to refMe's NSCalendarUnitMonth
set ocidDateComponents to ocidCalendar's components:((ocidCalendarUnitYear) + (ocidCalendarUnitMonth)) fromDate:ocidDate
set numSetYear to (ocidDateComponents's |year|) as integer
set numSetMonth to (ocidDateComponents's |month|) as integer
#####各種リスト
set listWeekDay to {"日", "月", "火", "水", "木", "金", "土"} as list
set listYear to {(numSetYear - 1), (numSetYear), (numSetYear + 1)} as list
set listMonth to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} as list
########################
#####ダイアログを前面に
tell current application
set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
#####年ダイアログを出す
try
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)
on error
log "エラーしました"
return
end try
if objResponseYear is false then
return
end if
set numSetYear to (objResponseYear) as integer
########################
#####月ダイアログを出す
if numSetMonth = 12 then
set numSetMonth to 1 as integer
else
set numSetMonth to numSetMonth + 1 as integer
end if
try
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)
on error
log "エラーしました"
return
end try
if objResponseMonth is false then
return
end if
set numSetMonth to (objResponseMonth) as integer
########################
tell application "Microsoft Excel" to launch
###エクセルの基本処理
set strMonthNoZeroSupp to (text -2 through -1 of ("00" & numSetMonth)) as text
set strSheetName to ("" & numSetYear & "-" & strMonthNoZeroSupp & "") as text
set strFileName to ("" & strSheetName & "カレンダーxlsx") as text
set strDesktopPath to POSIX path of (path to desktop folder from user domain) as text
set strSaveFilePath to (strDesktopPath & strFileName) as text
####新規ワークブック
tell application "Microsoft Excel"
activate
set objWorkBook to make new workbook with properties {excel 8 compatibility mode:false}
tell objWorkBook
set strWorkBookName to name as text
end tell
end tell
####シートの名前の変更
tell application "Microsoft Excel"
tell objWorkBook
tell active sheet
set name to strSheetName
end tell
end tell
end tell
###A1 左上のセルの処理
tell application "Microsoft Excel"
tell objWorkBook
tell sheet strSheetName
tell cell "A1"
set number format to "@"
###Aに年月を入力
set value to ("" & (numSetYear as text) & "/" & strMonthNoZeroSupp) as text
####タイトル行の処理
tell cell "$1:$1"
tell font object
set font size to numFontSize
set name to strFontName
set color to {0, 0, 0}
end tell
tell interior object
set color to {255, 255, 255}
end tell
tell border id border top
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
tell border id border bottom
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
tell border id border left
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
tell border id border right
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
end tell
end tell
end tell
end tell
end tell
########################
#####カレンダー初期化
set ocidCalendar to refMe's NSCalendar's calendarWithIdentifier:(refMe's NSCalendarIdentifierGregorian)
set ocidLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP")
ocidCalendar's setLocale:(ocidLocale)
#####日付コンポーネント初期化
set ocidDateComponents to refMe's NSDateComponents's alloc()'s init()
ocidDateComponents's setYear:(numSetYear)
ocidDateComponents's setMonth:(numSetMonth)
set ocidDate to ocidCalendar's dateFromComponents:(ocidDateComponents)
###指定月のカレンダーの最大日数-->繰り返し回数になる
set ocidMonthDateRange to ocidCalendar's rangeOfUnit:(refMe's NSCalendarUnitDay) inUnit:(refMe's NSCalendarUnitMonth) forDate:(ocidDate)
set numDateLength to ocidMonthDateRange's |length|
########################
###祭日データJSONで取得
set strURL to "https://holidays-jp.github.io/api/v1/date.json" as text
set ocidJsonURL to refMe's NSURL's URLWithString:(strURL)
set ocidOption to refMe's NSDataReadingMappedIfSafe
set listDownLoadData to refMe's NSData's dataWithContentsOfURL:(ocidJsonURL) options:(ocidOption) |error|:(reference)
set ocidJsonData to (item 1 of listDownLoadData)
#####################
###JSON初期化 してレコードに格納
set listJSONSerialization to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidJsonData) options:0 |error|:(reference))
set ocidJsonData to item 1 of listJSONSerialization
set ocidHolidayDict to refMe's NSDictionary's alloc()'s initWithDictionary:(ocidJsonData)
#####################
###取得するカレンダーの月(処理としては不要なんだけど、今後の展開のために処理)
set numYear to ocidCalendar's component:(refMe's NSCalendarUnitYear) fromDate:(ocidDate)
set numMonth to ocidCalendar's component:(refMe's NSCalendarUnitMonth) fromDate:(ocidDate)
###年はそのまま
set strYear to numYear as text
###月はゼロパディング
set strMonth to (text -2 through -1 of ("00" & (numMonth as text))) as text
###日付毎処理
set numCntRows to 2
repeat with numDayNo from (1) to (numDateLength)
-->ここまでは年と月でのカレンダーなので
###日付を入れて
(ocidDateComponents's setDay:(numDayNo))
set ocidDate to (ocidCalendar's dateFromComponents:(ocidDateComponents))
set ocidWeekDayClender to (ocidCalendar's components:(refMe's NSWeekdayCalendarUnit) fromDate:(ocidDate))
###日付
log ocidDate as date
###日付番号
log numDayNo
set strDayNoZeroSupp to (text -2 through -1 of ("00" & numDayNo)) as text
#####################
###曜日処理
set numWeekDayNo to (ocidWeekDayClender's |weekday|)'s intValue()
###曜日番号
log numWeekDayNo
###曜日テキスト
set strWeekDayJp to (item numWeekDayNo of listWeekDay) as text
###祭日取得用
set strDateText to ("" & strYear & "-" & strMonth & "-" & strDayNoZeroSupp & "") as text
###祭日取得
set ocidHolidayValue to (ocidHolidayDict's valueForKey:(strDateText))
###祭日処理
if ocidHolidayValue is (missing value) then
set numChkHoliday to 0 as integer
else
set numChkHoliday to 1 as integer
set strHolidayValue to ocidHolidayValue as text
log strHolidayValue
####Cの列
set strCellNoHoliday to ("C" & numCntRows & "") as text
tell application "Microsoft Excel"
tell objWorkBook
tell sheet strSheetName
tell cell strCellNoHoliday
###Cの列に祭日名を入力
set value to (strHolidayValue)
end tell
end tell
end tell
end tell
end if
###セル番号
set strCellNoDay to ("A" & numCntRows & "") as text
set strCellNoWeek to ("B" & numCntRows & "") as text
set strCellRowsRange to "$" & numCntRows & ":$" & numCntRows & ""
########################
###エクセル処理
tell application "Microsoft Excel"
tell objWorkBook
tell sheet strSheetName
tell cell strCellNoWeek
set value to strWeekDayJp
end tell
tell cell strCellNoDay
set value to strDayNoZeroSupp
end tell
end tell
end tell
#######行処理
tell cell strCellRowsRange
set number format to "@"
#####
tell interior object
if numWeekDayNo = 1 then
set color to {254, 188, 190}
else if numWeekDayNo = 7 then
set color to {212, 243, 255}
else if numChkHoliday = 1 then
set color to {254, 188, 190}
else
set color to {255, 255, 255}
end if
end tell
#####
tell font object
set font size to numFontSize
set name to strFontName
if numWeekDayNo = 1 then
set color to {170, 0, 0}
else if numWeekDayNo = 7 then
set color to {0, 88, 176}
else if numChkHoliday = 1 then
set color to {170, 0, 0}
else
set color to {0, 0, 0}
end if
end tell
tell border id border top
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
tell border id border bottom
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
tell border id border left
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
tell border id border right
set color to {0, 0, 0}
#set color index to color index automatic
set line style to continuous
set weight to border weight thin
#set tint and shade to 0.0
end tell
end tell
end tell
set numCntRows to numCntRows + 1
end repeat