指定日における誕生日を計算するAppleScriptの修正版です。
macOS 27で前バージョンがうまく動いていなかったので、ChatGPTにチェックしてもらったところ、Time Zoneが合っていないという指摘があったので、修正してみました。
割とCocoa Scriptingに手を出した直後あたりで試行錯誤していた時代に、「正しくはないが、API側が気を利かせて動いていた」というものをいくつか見つけたので、アップデートしておく必要性を感じます。
| AppleScript名:指定日における年齢を計算する_v2.scpt |
| — — Created 2015-09-10 by Takaaki Naganoya — Revised 2026-10-19 — Piyomaru Software — use AppleScript version "2.4" use scripting additions use framework "Foundation" — 誕生日(日本時間) set aBirthday to "2000-08-08 12:00:00" set anAge to getAgeFromBirthdayAndSpecifiedDay(aBirthday, 2026, 10, 19, "Asia/Tokyo") of me –> 26 –誕生日を求める(生年月日(ISO) on getAgeFromBirthdayAndSpecifiedDay(aBDStr as string, tYear as integer, tMonth as integer, tDay as integer, tTz as string) — 年齢を求める基準日時(日本時間) set targetDate to getDateInternational(tYear, tMonth, tDay, 23, 59, 59, tTz) of me — 日時文字列をNSDateに変換 set aFormatter to current application’s NSDateFormatter’s alloc()’s init() aFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en_US_POSIX") aFormatter’s setCalendar:(current application’s NSCalendar’s calendarWithIdentifier:(current application’s NSCalendarIdentifierGregorian)) aFormatter’s setDateFormat:"yyyy-MM-dd HH:mm:ss" aFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithName:tTz) set aBirthdayDate to aFormatter’s dateFromString:aBDStr if aBirthdayDate is missing value then error "Birthday format error" end if — 年齢計算用のカレンダー set aCalendar to current application’s NSCalendar’s calendarWithIdentifier:(current application’s NSCalendarIdentifierGregorian) aCalendar’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithName:tTz) — 誕生日から基準日時までの暦上の年数を計算 set ageComponents to aCalendar’s components:(current application’s NSCalendarUnitYear) fromDate:aBirthdayDate toDate:targetDate options:0 set myAge to (ageComponents’s |year|()) as integer return myAge end getAgeFromBirthdayAndSpecifiedDay — 指定したタイムゾーンでNSDateを生成 on getDateInternational(aYear, aMonth, aDay, anHour, aMinute, aSecond, timeZoneName) set aCalendar to current application’s NSCalendar’s calendarWithIdentifier:(current application’s NSCalendarIdentifierGregorian) aCalendar’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithName:timeZoneName) set theDate to aCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:aDay hour:anHour minute:aMinute |second|:aSecond nanosecond:0 return theDate end getDateInternational |



















