指定日を基準として「今週末」までの日付を求めるAppleScriptです。
日付の範囲指定で「今週」という指定を行った場合、+7日を足して相対的に1週間先の日付を返すという処理方法もありますが、(日本国内の)常識的には週末までの日付を返すことでしょう。
そして、今週末といった場合に、日曜日はじまりのカレンダーであれば土曜日が該当するわけですが、全世界的に考えると「日曜日はじまりではない」カレンダーを採用しているエリア(国)も存在しています。
月曜日はじまりのカレンダーを採用している国には、ヨーロッパ全域(EU諸国?)などがあるようです。
「今週末」というキーワードをmacOSの自然言語処理系の機能を用いて具体的な日付に変換することは可能ですが、OSアップデートのたびにAppleがバグを作りやすい機能なので(こんなところにまでバグを作るAppleのエンジニアが信じられない)、もうちょっと安心して計算できるプログラムがあったほうがいいと考えました。
そこで、すでに作ってあるさまざまな日付計算系のルーチンを組み合わせて計算してみました。
AppleScript名:指定日が所属する週のうち、最終日の日付を求める v2.scpt |
— Created 2015-02-02 by Shane Stanley — Modified 2015-02-02 by Takaaki Naganoya — Modified 2025-10-01 by Takaaki Naganoya — v2 最終週で、カレンダーが途中で途切れているケースに対応 use AppleScript use scripting additions use framework "Foundation" set targY to 2025 set targM to 9 set targD to 21 set theLastDay to calcLastDayFromTheTargetDay(targY, targM, targD) of me –> date "2025年9月27日 土曜日 0:00:00" –指定日が所属する週のうち、最終日の日付を求める on calcLastDayFromTheTargetDay(targY, targM, targD) –指定日が月内で何週目に該当するかを算出 set targWN to getDatesWeekNumberWithinAMonth(targY, targM, targD) of me –> 4 –指定日の当該週の日付を1D Listで返す set wRes to getWeekDatesWithinAWNinAMonth(targY, targM, targWN) of me –> {21, 22, 23, 24, 25, 26, 27} –1D List(当該週の日付)内における指定日の登場インデックスを算出 set cRes to offsetOf(wRes, targD) of me –1D List(当該週の日付)内の末尾までの日付を返す set restDays to items cRes thru -1 of wRes –> {28, 29, 30, missing value, missing value, missing value, missing value} –指定日が所属する週のうち、最終日の日付を求める set revList to reverse of restDays –逆順で末尾から数字が入っている場所をシーケンシャルサーチ repeat with i in revList set j to contents of i if j is not equal to missing value then exit repeat end if end repeat set targDate to getDateInternational(targY, targM, j, 23, 59, 59, "JST") of me return targDate end calcLastDayFromTheTargetDay –日曜日はじまりのカレンダーにおいて、指定年月内の指定週目の日付を配列で返す on getWeekDatesWithinAWNinAMonth(targY, targM, targWN) set mRen to getMlenInternational(targY, targM) of me set aList to make2DBlankArray(7, 5) of me –1か月分の2次元配列を求める set aCount to 1 repeat with i from 1 to mRen set tmpDO to getDateInternational(targY, targM, i, 23, 59, 59, "JST") of me –当初、00:00:00に指定していたが、週末(土曜日)を指定したときに、範囲が00:00;00〜00:00:00になると具合がよくないので00:00:00〜23:59:59になるようこの時間に変更し set aWD to weekday of tmpDO as number set aList to setItemByXY(aWD, aCount, aList, i) of me if aWD = 7 then set aCount to aCount + 1 end if end repeat return item targWN of aList end getWeekDatesWithinAWNinAMonth –日曜日はじまりのカレンダーにおいて、指定年月日の日付が月内の何週目にあたるかを算出 on getDatesWeekNumberWithinAMonth(targY, targM, targD) set mRen to getMlenInternational(targY, targM) of me set aList to make2DBlankArray(7, 5) of me set aCount to 1 repeat with i from 1 to mRen set tmpDO to getDateInternational(targY, targM, i, 23, 59, 59, "JST") of me –当初、00:00:00に指定していたが、週末(土曜日)を指定したときに、範囲が00:00;00〜00:00:00になると具合がよくないので00:00:00〜23:59:59になるようこの時間に変更した set aWD to weekday of tmpDO as number set aList to setItemByXY(aWD, aCount, aList, i) of me if i = targD then return aCount if aWD = 7 then set aCount to aCount + 1 end if end repeat return false end getDatesWeekNumberWithinAMonth –現在のカレンダーで指定年月の日数を返す on getMlenInternational(aYear, aMonth) set theNSCalendar to current application’s NSCalendar’s currentCalendar() — do *not* use initWithCalendarIdentifier: set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:1 hour:0 minute:0 |second|:0 nanosecond:0 set theResult to theNSCalendar’s rangeOfUnit:(current application’s NSDayCalendarUnit) inUnit:(current application’s NSMonthCalendarUnit) forDate:theDate –> {location:1, length:31} return |length| of theResult end getMlenInternational –Make a GMT Date Object with parameters from a given time zone. on getDateInternational(aYear, aMonth, aDay, anHour, aMinute, aSecond, timeZoneAbbreviation) set theNSCalendar to current application’s NSCalendar’s currentCalendar() theNSCalendar’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:(timeZoneAbbreviation)) set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:aDay hour:anHour minute:aMinute |second|:aSecond nanosecond:0 return theDate as date end getDateInternational –Listに配列的な添字を使ってアクセスするルーチン群 on xFill(aX, aY, aList, aVal, aRepNum) repeat with x from aX to (aX + aRepNum – 1) set aList to setItemByXY(x, aY, aList, aVal) of me end repeat return aList end xFill on yFill(aX, aY, aList, aVal, aRepNum) repeat with y from aY to (aY + aRepNum – 1) set aList to setItemByXY(aX, y, aList, aVal) of me end repeat return aList end yFill on getItemByXY(aX, aY, aList, aBlankItem) try set aContents to contents of (item aX of item aY of aList) on error set aContents to aBlankItem end try return aContents end getItemByXY on setItemByXY(aX, aY, aList, aContents) try set (item aX of item aY of aList) to aContents end try return aList end setItemByXY –空白の2D Array を出力する on make2DBlankArray(curLen, curMax) set outArray to {} repeat curMax times set aList to {} repeat curLen times set the end of aList to missing value –オリジナルの""(ヌル文字)からmissing valueに変更 end repeat set the end of outArray to aList end repeat return outArray end make2DBlankArray –1D Listのoffsetを求める on offsetOf(aList as list, aTarg) set aArray to current application’s NSArray’s arrayWithArray:aList set aIndex to aArray’s indexOfObjectIdenticalTo:aTarg return (aIndex + 1) end offsetOf |
(Visited 1 times, 1 visits today)