Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

指定日が所属する週のうち、最終日の日付を求める v2

Posted on 10月 7 by Takaaki Naganoya

指定日を基準として「今週末」までの日付を求める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

★Click Here to Open This Script 

(Visited 1 times, 1 visits today)
Posted in Calendar | Tagged 15.0savvy 26.0savvy | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • Numbersで選択範囲のセルの前後の空白を削除
  • macOS 26, Tahoe
  • macOS 15でも変化したText to Speech環境
  • KagiのWebブラウザ、Orion
  • Script Debuggerの開発と販売が2025年に終了
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • macOS 15 リモートApple Eventsにバグ?
  • NSObjectのクラス名を取得 v2.1
  • 2024年に書いた価値あるAppleScript
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • (確認中)AppleScript Dropletのバグっぽい動作が解消?
  • AVSpeechSynthesizerで読み上げテスト
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • Numbersで選択中の2列のセルを比較して並べ直して書き戻す v2
  • Apple、macOS標準搭載アプリ「写真」のバージョン表記を間違える
  • Script DebuggerがmacOS 15.x上で起動せず→起動

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (152) CotEditor (66) Finder (52) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (56) Pixelmator Pro (20) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Newt On Project
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • Scripting Additions
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC