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

タグ: Calendar

Calendar.appのイベントにAppletを添付できる?

Posted on 9月 22 by Takaaki Naganoya

Calendar.appのイベント(予定)に、ファイルを添付できるようになっています。ここに、AppleScriptアプレットを添付して実行することで、タイマー実行アプリとしてCalendar.appを利用する、というスタイルが初期のMac OS Xで見られました。Mac OS X 10.6ぐらいまででしょうか。

その後、どうもCalendar.appのイベントにAppletを添付しても実行されなくなった、という話をよく聞くようになりました。

自分も、タイマー実行アプリとしてCalendar.appは「使えないもの」と判断し、自前でタイマー機能などを作り込んで使ってきました。確実さでいえば、これは実に確実ではあるものの、それを外部に提供するのかという話については、いい「落とし所」を見出せない感じではあります。

こちらもタダで出すつもりはないけれど、これどうせ国内外のScripter連中は買わないよね、というところで話が止まっているものでもあります。モノがモノなので、Mac App Storeにも出せないでしょう(あらかじめ、どういうアプリを操作する、という設定を行なって提出する必要があるので。事前にわかるわけがない)。

そんな中、X(旧称Twitter)上で「Calendar.appをAppleScript Appletのタイマー起動用に使っている」という話を聞きつけ、その内容について実際にやりとりをして、自分の手元でもできることを確認しました。

その時点での自分の理解では、「イベントにAppletを添付して、イベントのアクションで『添付ファイルをオープンする』という指定を行えばOK」だという話だと納得しました。なるほど、自分は使い方を誤解していたのか、と。

しかし、その翌日に「別のAppletも添付して試してみよう」ということで、別のイベントを作成して別のAppletを添付して「添付ファイルをオープン」させてみたものの、今度はイベントの指定時刻に通知は来るものの、添付ファイルであるAppletは起動されませんでした。

Calendar.app自体のバグなのか、それともイベントへのApplet添付・実行には何か気づかないパラメータが存在しているのか?(イベント登録時のアカウントであるとか、ローカルの通知だけにしておかないとダメとか) そのあたりがまったく不明です。

昨日、一昨日ぐらいの段階でCalendar.appへのAppletの添付・実行は2・3度成功していたので、なかなか謎なところがあるものです。

Posted in Bug Calendar | Tagged 13.0savvy Calendar | Leave a comment

macOS 10.15以降のカレンダーアプリケーション内に同梱された大量のAppleScriptを発見

Posted on 10月 15, 2020 by Takaaki Naganoya

たまたま、macOS 11.0Betaで特定のアプリケーションのアイコンを取得できない問題に直面し、その調査を行なっていたところ……カレンダーアプリケーションのバンドル内のResourcesフォルダに大量(32本)のAppleScriptが入っているのを見つけました。

見つけたのはmacOS 11.0Beta上ですが、10.15.7環境でもチェックしてみたところ同数のAppleScriptが入っていました。10.14.6ではみつかりません。10.15.x以降でこうなっているようです。

macOS 10.15はBetaの途中で「これは使い物にならない!」と見切りをつけ、テスト環境で様子を見ながらも本気では使い込んでこなかったので、いまごろ細かい変更点に気づきました(全力で10.15はパスして11.0に移行したいです)。


▲macOS 10.15.7のカレンダー内に入っているAppleScriptと11.0betaに入っているもののdiffをとってみた結果。違いはないもよう

拡張子がscptで、実行専用になっていません。つまり、中身が読めるわけです。

サンドボックス環境のアプリケーションが内部からこうしたAppleScriptを呼ぶ際には実行専用で、かつファイルを読み取り専用に設定しておく必要があります。読み取り&編集可能な状態なので、サンドボックス化されている「カレンダー」アプリケーションがこれらを内部で呼び出している可能性は低そうです。部品として入れるならハンドラ宣言を行なって、外部から呼び出しやすいようにするはずです。これらのAppleScriptをテンプレートとして読み込んで、変数内容を実際のデータに置換して実行するという方法もないことはありませんが、そこまでやらないでしょう(必要に迫られて、やったことありますけれども)。そういう使い方をするならテキスト形式で保存しておくとか、もっと「テンプレートらしく」書いておくことでしょう。書き換え部分を「%@」と書いておくとか。

よって、カレンダーアプリケーション内部でこれらのAppleScript群を呼び出している可能性はほとんどないと思います。

実際にScriptの内容を読んでみると、基礎的なカレンダーアプリケーションの操作を行う、地に足のついた実に質実剛健で良質なサンプルScript集という雰囲気でした。AppleのサンプルScriptにありがちな、冒頭に著作権と(訴訟回避の)お断り文言のオンパレードのテンプレートコメントが30行ぐらい書いてありそうなものではありません。

AppleScript名:addAttendees.scpt
–> Script creates an event and adds invitees. Please modify the email to test account before running the script
set theStartDate to (current date)
set hours of theStartDate to 15
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (1 * hours)
tell application "Calendar"
  tell calendar "calendar"
    make new event with properties {summary:"Apple Script Invitations", start date:theStartDate, end date:theEndDate, location:"one Infinite Loop"}
    
    
set theEvent to (first event where its summary = "Apple Script Invitations")
    
tell theEvent
      make new attendee at end of attendees with properties {display name:"test", email:"caltest_as1@icloud.com"}
    end tell
  end tell
end tell

★Click Here to Open This Script 

自分もCotEditorのサンプルAppleScriptパックを配布していますが、あれの飛び道具系(PowerPack)ではなく、基礎的なBasicPackの雰囲気です。

この手のサンプルScriptというと、AppleがmacOSに同梱している悪夢のようなApple純正サンプルスクリプト集があるわけですが(日本語環境で構文確認が通らないとか言語道断。しかも書き方が古臭い)、そういうテイストではありません。

「実務でAppleScriptを書いている人間」による簡素なテストコードといった雰囲気を感じます。

まるで、Apple社内で機能テストに使っているAppleScriptがそのままRelease版に残ってしまったような感じです。正確なところはわかりませんけれども。

意図して入れているのか、意図していないのかはまったく不明ですが、OSのアップデートで消去されないうちにバックアップをとっておくことをおすすめします。

このScriptを入れてあることが何かのメッセージなのかどうかは不明です。たいして意味はないでしょう。ただ、Apple社内もしくは外注にちゃんとまともなAppleScriptを書ける人間がいるぞ、という自己紹介的なものだと受け取っておきます。

カレンダーアプリケーションのAppleScript用語辞書内にこれらのAppleScriptサンプルを(本Blog掲載のAppleScriptのようにリンクつきで?)掲載しておけという指定を受けた部下が、その言葉の意味がわからずにそのままバンドル内に渡されたAppleScriptをファイルのまま入れてしまったというあたりが真相ではないかと思われます。

Posted in news | Tagged 10.15savvy 11.0savvy Calendar | Leave a comment

2月1日が日曜日でうるう年ではないかチェック(ASOC)

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:2月1日が日曜日でうるう年ではないかチェック(ASOC)
— Created 2015-02-02 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–use framework "ASObjCExtras"

set resYList to {} –hit year

set theNSCalendar to current application’s NSCalendar’s currentCalendar() — do *not* use initWithCalendarIdentifier:

repeat with y from 2000 to 2100
  set aMlen to getMlenAndVerifyFirstDaysNum(y, 2, theNSCalendar, 28, 1) of me
  
if aMlen = true then
    set the end of resYList to y
  end if
  
–set the end of resYList to aMlen
end repeat

return resYList
–>  {​​​​​2009, ​​​​​2015, ​​​​​2026, ​​​​​2037, ​​​​​2043, ​​​​​2054, ​​​​​2065, ​​​​​2071, ​​​​​2082, ​​​​​2093, ​​​​​2099​​​}

on getMlenAndVerifyFirstDaysNum(aYear, aMonth, theNSCalendar, dMax, fdayNum)
  
  
–現在のLocaleのCalendarで指定年月の日数をかぞえる
  
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}
  
set mLen to |length| of theResult
  
  
–指定年月の1日を取得
  
set theDay to theNSCalendar’s components:((current application’s NSWeekdayCalendarUnit) + (current application’s NSMonthCalendarUnit as integer)) fromDate:theDate
  
  
–指定年月の1日の曜日が指定曜日で、うるう月でなく、指定年月の日数が指定日数(dMax)であるか判定
  
if (theDay’s |weekday|() = fdayNum) and (theDay’s isLeapMonth() is false) and (mLen = dMax) then
    return true
  else
    return false
  end if
  
end getMlenAndVerifyFirstDaysNum

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Calendar | Leave a comment

国民の祝日を求める v4

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:国民の祝日を求める v4
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

(*

本バージョンのテーマ:

日付ベースだけでカレンダー計算を行うのではなく、祝日の名称や振替休日といった属性情報を考慮しつつ計算を行う
元日の振替休日に対応

*)

set aList to retHolidayRec(2018) of me

–> {{date "2018年1月1日月曜日 0:00:00", "元旦"}, {date "2018年1月8日月曜日 0:00:00", "成人の日"}, {date "2018年2月11日日曜日 0:00:00", "建国記念日"}, {date "2018年2月12日月曜日 0:00:00", "振替休日"}, {date "2018年3月21日水曜日 0:00:00", "春分の日"}, {date "2018年4月29日日曜日 0:00:00", "昭和の日"}, {date "2018年4月30日月曜日 0:00:00", "振替休日"}, {date "2018年5月3日木曜日 0:00:00", "憲法記念日"}, {date "2018年5月4日金曜日 0:00:00", "みどりの日"}, {date "2018年5月5日土曜日 0:00:00", "こどもの日"}, {date "2018年7月16日月曜日 0:00:00", "海の日"}, {date "2018年9月17日月曜日 0:00:00", "敬老の日"}, {date "2018年9月23日日曜日 0:00:00", "秋分の日"}, {date "2018年9月24日月曜日 0:00:00", "振替休日"}, {date "2018年10月8日月曜日 0:00:00", "体育の日"}, {date "2018年11月3日土曜日 0:00:00", "文化の日"}, {date "2018年11月23日金曜日 0:00:00", "勤労感謝の日"}, {date "2018年12月23日日曜日 0:00:00", "天皇誕生日"}, {date "2018年12月24日月曜日 0:00:00", "振替休日"}}

–国民の祝日を求める(属性つき)
on retHolidayRec(aYear)
  set holidayList to {{"1/1", "元旦"}, {"2/11", "建国記念日"}, {"4/29", "昭和の日"}, {"5/3", "憲法記念日"}, {"5/4", "みどりの日"}, {"5/5", "こどもの日"}, {"11/3", "文化の日"}, {"11/23", "勤労感謝の日"}, {"12/23", "天皇誕生日"}}
  
  
set the end of holidayList to {get_specifiedDay(aYear, 1, 2, 2), "成人の日"} –成人の日–1月の第2月曜日
  
set the end of holidayList to {get_specifiedDay(aYear, 7, 2, 3), "海の日"} –海の日–7月の第3月曜日
  
set the end of holidayList to {get_specifiedDay(aYear, 9, 2, 3), "敬老の日"} –敬老の日–9月の第3月曜日
  
set the end of holidayList to {get_specifiedDay(aYear, 10, 2, 2), "体育の日"} –体育の日–10月の第2月曜日
  
set the end of holidayList to {"3/" & get_ShunbunNoHi(aYear), "春分の日"} –春分の日
  
set the end of holidayList to {"9/" & get_ShuubunNoHi(aYear), "秋分の日"} –秋分の日
  
  
set holiDate to {}
  
repeat with i in holidayList
    set holiD to date (((aYear as text) & "/" & item 1 of i) as text)
    
set holiNum to weekday of holiD as number
    
    
–元日以外を対象とする(元旦に振替休日なし)–> いや、ある(汗)
    
–if ((item 1 of i) as text) is not "1/1" then
    
–振替休日付加処理
    
if holiNum = 1 then –祝祭日が日曜日だったら
      –日付を動かすのではなく、振替休日を追加する
      
set holiD_furikae to holiD + (1 * days)
      
set the end of holiDate to {holiD_furikae, "振替休日"}
    end if
    
–end if
    
set the end of holiDate to {holiD, item 2 of i}
    
  end repeat
  
  
  
–重複した休日が発生した場合の再振替処理  
  
–基本ルール:  振替休日を後に送る
  
–        「振替休日」が重複リストに入っていないかどうかをチェックし、振替休日の再配置を行う
  
set itemNum to 1
  
set holiDateDup to detectDuplicatesFromNestedList(holiDate, itemNum) of me
  
  
set huriList to {}
  
repeat with i in holiDateDup
    set iCount to length of i
    
repeat with ii in i
      set {aDate, aDateName} to ii
      
if aDateName = "振替休日" then
        set the end of huriList to contents of ii
      end if
    end repeat
  end repeat
  
  
set holiDate to shellSortListAscending(holiDate, 1) of me
  
set holiDateList to spritOrderedItemFromNestedList(holiDate, 1) of me
  
  
repeat with i in huriList
    set {aDate, aName} to i
    
set j to contents of i
    
set offsetDate to 1
    
repeat
      set bDate to aDate + (offsetDate * days)
      
if bDate is not in holiDateList then
        exit repeat
      end if
      
set offsetDate to offsetDate + 1
    end repeat
    
    
set iCount to 1
    
repeat with ii in holiDate
      set jj to contents of ii
      
if jj = j then
        –「複数要素一括削除サブルーチン」などという高機能すぎるサブルーチンを使用。ちょっともったいない
        
set holiDate to itemsDelete(holiDate, {iCount}) of me
      end if
      
set iCount to iCount + 1
    end repeat
    
    
set the end of holiDate to {bDate, "振替休日"}
    
  end repeat
  
  
  
–秋分の日と敬老の日の「間の日」の休日判定処理
  
–参考文献:
  
–http://ja.wikipedia.org/wiki/秋分の日
  
set septDL to {}
  
set the end of septDL to "9/" & get_ShuubunNoHi(aYear) of me –秋分の日
  
set the end of septDL to get_specifiedDay(aYear, 9, 2, 3) –敬老の日 –9月の第3月曜日
  
set septDL to shellSort(septDL) of me
  
if septDL = {"9/21", "9/23"} then
    set kokuminShukujitu to (aYear as string) & "/9/22"
    
set kokuminShukujitu to date kokuminShukujitu
    
set the end of holiDate to {kokuminShukujitu, "国民の祝日"}
  end if
  
  
–最後に、並べ替えを行って仕上げ
  
set holiDate to shellSortListAscending(holiDate, 1) of me
  
  
return holiDate
  
end retHolidayRec

–春分の日を求める
–2000年から2099年の間まで計算可能
on get_ShunbunNoHi(aYear)
  set a to 20.69115
  
set b to (aYear – 2000) * 0.2421904
  
set c to round ((aYear – 2000) / 4) rounding toward zero
  
set d to round (a + b – c) rounding toward zero
  
return d
end get_ShunbunNoHi

–秋分の日を求める
–2000年から2099年の間まで計算可能
on get_ShuubunNoHi(aYear)
  set a to 23.09
  
set b to (aYear – 2000) * 0.2421904
  
set c to round ((aYear – 2000) / 4) rounding toward zero
  
set d to round (a + b – c) rounding toward zero
  
return d
end get_ShuubunNoHi

–指定月の第x指定曜日に該当する日付を求める(mm/dd形式)
– 曜日の指定を数値(weekday of (current date) as number)で行えるようにした。
– 曜日を「日曜日」などの日本語ローカライズド文字列で指定するのをやめた
–パラメータ: 年, 月, 曜日番号, 順番
on get_specifiedDay(aYear as integer, aMonth as integer, Youbi as integer, orderNum as integer)
  set sDat to date ((aYear & "/" & aMonth & "/1") as text)
  
set eDat to getMlenInternational(aYear, aMonth) of me
  
  
set countNum to 0
  
  
repeat with i from 1 to eDat
    set aCal to date ((aYear & "/" & aMonth & "/" & (i as text)) as text)
    
set aWeekDayNum to weekday of aCal as number
    
if Youbi = aWeekDayNum then
      set countNum to countNum + 1
      
if countNum is orderNum then
        set aCalText to (aMonth & "/" & i as text)
        
return aCalText
      end if
    end if
  end repeat
end get_specifiedDay

–指定日の月のみ返す
on getMonth(aDat as date)
  set bDate to month of aDat
  
return bDate as number
end getMonth

–指定日の日付のみ返す
on getDate(aDat as date)
  set bDate to day of aDat
  
return bDate as number
end getDate

–指定日の年のみ返す
on getYear(aDat as date)
  set bDate to year of aDat
  
return bDate as number
end getYear

–現在のカレンダーで指定年月の日数を返す(getMlenから置き換えた)
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
  
return |length| of theResult
end getMlenInternational

–リスト中から重複項目をリストアップする
on detectDuplicates(aList)
  set aCount to length of aList
  
  
set duplicationList to {}
  
repeat aCount times
    set anItem to contents of (first item of aList)
    
set aList to rest of aList
    
if anItem is in aList then
      set the end of duplicationList to anItem
    end if
  end repeat
  
  
return duplicationList
end detectDuplicates

–リストから重複部分を除外
on removeDuplicates(aList)
  set newList to {}
  
repeat with i from 1 to (length of aList)
    set anItem to item 1 of aList
    
set aList to rest of aList
    
if {anItem} is not in aList then set end of newList to anItem
  end repeat
  
return newList
end removeDuplicates

–シェルソート
on shellSort(aSortList)
  script oBj
    property list : aSortList
  end script
  
set len to count oBj’s list’s items
  
set gap to 1
  
repeat while (gap ≤ len)
    set gap to ((gap * 3) + 1)
  end repeat
  
repeat while (gap > 0)
    set gap to (gap div 3)
    
if (gap < len) then
      repeat with i from gap to (len – 1)
        set temp to oBj’s list’s item (i + 1)
        
set j to i
        
repeat while ((j ≥ gap) and (oBj’s list’s item (j – gap + 1) > temp))
          set oBj’s list’s item (j + 1) to oBj’s list’s item (j – gap + 1)
          
set j to j – gap
        end repeat
        
set oBj’s list’s item (j + 1) to temp
      end repeat
    end if
  end repeat
  
return oBj’s list
end shellSort

–シェルソートで入れ子のリストを昇順ソート
on shellSortListAscending(a, keyItem)
  set n to length of a
  
set cols to {1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1}
  
repeat with h in cols
    if (h ≤ (n – 1)) then
      repeat with i from h to (n – 1)
        set v to item (i + 1) of a
        
set j to i
        
repeat while (j ≥ h) and ((contents of item keyItem of item (j – h + 1) of a) > (item keyItem of v))
          set (item (j + 1) of a) to (item (j – h + 1) of a)
          
set j to j – h
        end repeat
        
set item (j + 1) of a to v
      end repeat
    end if
  end repeat
  
return a
end shellSortListAscending

–シェルソートで入れ子のリストを降順ソート
on shellSortListDecending(a, keyItem)
  set n to length of a
  
set cols to {1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1}
  
repeat with h in cols
    if (h ≤ (n – 1)) then
      repeat with i from h to (n – 1)
        set v to item (i + 1) of a
        
set j to i
        
repeat while (j ≥ h) and ((contents of item keyItem of item (j – h + 1) of a) < (item keyItem of v))
          set (item (j + 1) of a) to (item (j – h + 1) of a)
          
set j to j – h
        end repeat
        
set item (j + 1) of a to v
      end repeat
    end if
  end repeat
  
return a
end shellSortListDecending

–入れ子のリスト中から重複項目をアイテム番号つきでリストアップする
on detectDuplicatesFromNestedList(aList, itemNum)
  set aCount to length of aList
  
copy aList to orig_aList
  
  
set duplicationList to {}
  
repeat aCount times
    set anItem to contents of (first item of aList)
    
set aList to rest of aList
    
    
–指定アイテムだけのリストを毎回再生成して存在確認を行う
    
set aaList to spritOrderedItemFromNestedList(aList, itemNum) of me
    
if (contents of (item itemNum of anItem)) is in aaList then
      set the end of duplicationList to anItem
    end if
    
  end repeat
  
  
–検出した重複データを元に、該当するデータをリストアップ
  
set detectList to {}
  
repeat with i in duplicationList
    set j to contents of (item itemNum of i)
    
set detectItem to {}
    
repeat with ii in orig_aList
      set jj to contents of (item itemNum of ii)
      
      
if jj = j then
        set the end of detectItem to (contents of ii)
      end if
    end repeat
    
set the end of detectList to detectItem
  end repeat
  
  
return detectList
end detectDuplicatesFromNestedList

–入れ子のリストの全要素から指定アイテム目の要素だけを取り出してリストで返す
on spritOrderedItemFromNestedList(aList, itemNum)
  set aaList to {}
  
repeat with i in aList
    set the end of aaList to contents of (item itemNum of i)
  end repeat
  
return aaList
end spritOrderedItemFromNestedList

–リスト中の指定要素を削除して返す
on itemsDelete(aList, delNumList)
  set delLen to length of delNumList
  
  
repeat with i from 1 to delLen
    
    
set newList to {}
    
set aLen to length of aList
    
    
set ii to item i of delNumList
    
    
if ii = 1 then
      set maeList to items 2 thru aLen of aList
      
set newList to maeList
      
    else if ii = aLen then
      set maeList to items 1 thru (aLen – 1) of aList
      
set newList to maeList
      
    else
      set maeList to items 1 thru (ii – 1) of aList
      
set atoList to items (ii + 1) thru -1 of aList
      
set newList to maeList & atoList
    end if
    
    
–アイテム指定の補正
    
set delNumList to adjustItemNo(ii, delNumList) of me
    
log delNumList
    
    
set aList to newList
  end repeat
  
  
return newList
end itemsDelete

–itemsDeleteのサブルーチン
–リストに対して複数アイテムの削除を行う場合に、1つ削除した後にはアイテム指定が
–狂ってしまうため、毎回削除するたびにアイテム指定の補正を行う
–paramNumとelemListの間でのパラメータの衝突は関知しない

–項目要素補正をリストに対して行う
on adjustItemNo(paramNum, elemList)
  –項目ゼロを指定してきた場合には、そのままelemListを戻す
  
if paramNum = 0 then return elemList
  
–プラス方向のレンジ外判定は行っていない。elemListのlengthよりも大きな値は関知しない
  
set retList to {}
  
repeat with i in elemList
    set j to contents of i
    
if j > paramNum then
      set ansNum to j – 1
    else
      set ansNum to j
    end if
    
set the end of retList to ansNum
  end repeat
  
  
return retList
end adjustItemNo

–現在のカレンダーで指定年月のdate objectを返す(年、月、日、時、分、秒)
on getDateInternationalYMDhms(aYear, aMonth, aDay, anHour, aMinute, aSecond)
  set theNSCalendar to current application’s NSCalendar’s currentCalendar()
  
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 getDateInternationalYMDhms

–現在のカレンダーで指定年月のdate objectを返す(年、月、日)
on getDateInternational(aYear, aMonth, aDay)
  set theNSCalendar to current application’s NSCalendar’s currentCalendar()
  
set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:aDay hour:0 minute:0 |second|:0 nanosecond:0
  
return theDate as date
end getDateInternational

★Click Here to Open This Script 

Posted in Calendar | Tagged 10.11savvy 10.12savvy 10.13savvy Calendar | Leave a comment

自然言語による相対日付指定v15

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:自然言語による相対日付指定v15
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–v15 ASOCの部品に入れ替え、macOS 10.11以降用にハンドラの型強制オプションをつけた
–v14 10.8の上で動作するように書き換えた

–The Calendar Scripting Module

global d3List –3か月カレンダーの格納変数
global d4List –3か月カレンダーBの格納変数(当月の情報しか日付は入っていないカレンダー)
global weekCounter — 今日が3か月カレンダーの何週目にあたるか

set aDate to calcDate("今日", "") of me
log {"今日:" & aDate as text}

set aDate to calcDate("先々週", "水曜日") of me
log {"先々週の水曜日:" & aDate as text}

set aDate to calcDate("来週", "金曜") of me
log {"来週の金曜日:" & aDate as text}

on calcDate(param1 as string, param2 as string)
  
  
–相対日付指定パラメータテーブル
  
–data sample: {{"今日", "きょう"}, 0, "date"}
  
–data format:{{"keyword_1", "keyword_2"…."keyword_n"}, vector, "class_of_this_keyword"}
  
  
–Vector量をその場で(ユーザー環境に応じて)算出もしくは取得する必要がある場合には「999」を設定しておく
  
  
set dDat to {{{"来年"}, 1, "year"}, {{"今年"}, 0, "year"}, {{"去年"}, -1, "year"}, {{"再来年"}, 2, "year"}, {{"今週"}, 0, "week"}, {{"先週"}, -1, "week"}, {{"先々週"}, -2, "week"}, {{"来週"}, 1, "week"}, {{"再来週"}, 2, "week"}, {{"今日", "きょう"}, 0, "date"}, {{"明日", "あした"}, 1, "date"}, {{"明後日", "あさって"}, 2, "date"}, {{"明々後日", "しあさって"}, 3, "date"}, {{"昨日", "きのう"}, -1, "date"}, {{"一昨日", "おととい"}, -2, "date"}, {{"一昨々日", "さきおととい"}, -3, "date"}, {{"次", "今度", "こんど", "つぎ"}, 1, "variable"}, {{"前"}, -1, "variable"}}
  
  
–曜日指定パラメータ
  
set wDat to {"日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日", "日曜", "月曜", "火曜", "水曜", "木曜", "金曜", "土曜", "にちようび", "げつようび", "かようび", "すいようび", "もくようび", "きんようび", "どようび", "にちよう", "げつよう", "かよう", "すいよう", "もくよう", "きんよう", "どよう"}
  
  
–3か月カレンダーを作成
  
makeCalendar() of me
  
  
–今日の日付を保持
  
set todayDate to current date
  
  
–指定週の計算用ワーク変数
  
set targetWeek to 0
  
  
–指定月の計算用ワーク変数
  
set targetMonth to (month of todayDate) as number
  
set targetYear to year of todayDate
  
set targetDate to day of todayDate
  
  
set aItem to ""
  
repeat with i in dDat
    if param1 is in item 1 of i then
      copy i to aItem
      
exit repeat
    end if
  end repeat
  
  
if (item 3 of aItem) is "date" then
    log "Hit date class parameter"
    
–dateレベルのパラメータの場合
    
set retDate to todayDate + (item 2 of aItem) * days
    
set time of retDate to 0
    
return retDate
    
–「時刻を問わない」ことを示す表現として、00時を指定してdate型を返す
    
  else if item 3 of aItem is "week" then
    log "hit week class parameter"
    
    
–weekレベルのパラメータの場合
    
set targetWeek to weekCounter + (item 2 of aItem)
    
if param2 is in wDat then
      
      
–パラメータが「何曜日か?」を得る
      
set tmpD to 1
      
repeat with j in wDat
        if param2 is contents of j then exit repeat
        
set tmpD to tmpD + 1
      end repeat
      
      
if tmpD > 7 then
        set tmpD to tmpD mod 7
      end if
      
      
–今月か?
      
if item tmpD of item targetWeek of d4List is 0 then
        –今月ではない場合
        
set tmp2 to item tmpD of item targetWeek of d3List
        
if ((item 2 of aItem) < 0) then
          –今月ではなく、パラメータがマイナスの週計算を行う場合
          
–先月に入ったので月を減らす
          
set targetMonth to targetMonth – 1
          
set tmp4 to (targetYear & "/" & targetMonth & "/" & tmp2) as string
          
set ret2Date to date (tmp4 & " 0:00:00")
          
return ret2Date
        else
          –来月として処理
          
set targetMonth to targetMonth + 1
          
set tmp4 to (targetYear & "/" & targetMonth & "/" & tmp2) as string
          
set ret2Date to date (tmp4 & " 0:00:00")
          
return ret2Date
        end if
      else
        –今月の場合の処理
        
set tmp2 to item tmpD of item targetWeek of d3List
        
set tmp3 to day of todayDate
        
        
set tmp4 to (targetYear & "/" & targetMonth & "/" & tmp2) as string
        
set ret2Date to date (tmp4 & " 0:00:00")
        
return ret2Date
        
      end if
      
    else
      –ここはエラーを返すべし("来週","")といったパターンになるので
      
—-あるいは、「○○日」といったダイレクトパラメータの処理を行う
      
      
–日付ダイレクトパラメータの処理
      
      
error "日付を特定するためのパラメータがありません"
      
(*
      ただし、このように決め打ちでエラーメッセージを返すような実装は暫定仕様。
      何が欠けているかをGUIフィードバックするのが最終的な実装
      *)

    end if
  else if item 3 of aItem is "month" then
    –monthレベルのパラメータの場合
    
log "Hit month class parameter"
    
  else if item 3 of aItem is "year" then
    –yearレベルのパラメータの場合
    
log "Hit year class parameter"
    
  end if
  
  
return "no hit"
end calcDate

–相対日付計算用の3か月カレンダーを作成する
on makeCalendar()
  
  
set aToday to current date
  
  
–とりあえず、当月の情報を収集する
  
set y1 to getYear(aToday) of me
  
set m1 to getMonth(aToday) of me
  
set d1 to getDate(aToday) of me
  
set dy1 to getDay(aToday) of me
  
set mlen1 to getMlen(y1, m1) of me
  
  
set mXDay to getDay(aToday) of me –今日の曜日
  
  
–前月の情報を組み立てる
  
if m1 is not equal to 1 then
    –2〜12月の場合
    
set y0 to y1
    
set m0 to m1 – 1
  else
    –1月の場合
    
set y0 to y1 – 1
    
set m0 to 12
  end if
  
  
–前月の月の長さ
  
set mLen0 to getMlen(y0, m0) of me
  
  
–前月のみ開始日の曜日を求めておく必要がある(3か月カレンダーを作るため)
  
set m0Date to (y0 & "/" & m0 & "/1 0:00:00") as text
  
set m0Date to date m0Date
  
set m0Day to getDay(m0Date) of me –1日の曜日
  
  
–翌月の情報を組み立てる
  
if m1 is not equal to 12 then
    –1〜11月の場合
    
set y2 to y1
    
set m2 to m1 + 1
  else
    –12月の場合
    
set y2 to y1 + 1
    
set m2 to 1
  end if
  
–翌月の月の長さ
  
set mLen2 to getMlen(y2, m2) of me
  
  
–3か月カレンダーの格納変数
  
set d3List to {}
  
–3か月カレンダーBの格納変数(当月の情報しか日付は入っていないカレンダー)
  
set d4List to {}
  
  
set gapList to {0, 0, 0, 0, 0, 0, 0}
  
set w1temp to items 1 thru (m0Day) of gapList
  
set w2temp to items 1 thru (m0Day) of gapList
  
  
set dayCounter to 1
  
  
–最初の1週目の要素を組み立てる
  
repeat with i from m0Day to 7
    set the end of w1temp to dayCounter
    
set the end of w2temp to 0
    
set dayCounter to dayCounter + 1
  end repeat
  
set the end of d3List to w1temp
  
set the end of d4List to w2temp
  
  
set w1temp to {}
  
set w2temp to {}
  
  
  
–前月の通常処理
  
repeat
    repeat with i from 1 to 7
      set the end of w1temp to dayCounter
      
set the end of w2temp to 0
      
      
set dayCounter to dayCounter + 1
      
if dayCounter > mLen0 then exit repeat
    end repeat
    
if dayCounter > mLen0 then exit repeat
    
set the end of d3List to w1temp
    
set the end of d4List to w2temp
    
    
set w1temp to {}
    
set w2temp to {}
    
  end repeat
  
  
–ここから当月処理
  
  
–1週目
  
if i = 7 then
    set ii to 1
    
set the end of d3List to w1temp
    
set the end of d4List to w2temp
    
set w1temp to {}
    
set w2temp to {}
    
  else
    set ii to i + 1
  end if
  
  
set dayCounter to 1 –日付カウンタの初期化
  
repeat with j from ii to 7
    set the end of w1temp to dayCounter
    
set the end of w2temp to dayCounter
    
    
set dayCounter to dayCounter + 1
  end repeat
  
  
set the end of d3List to w1temp
  
set the end of d4List to w2temp
  
  
set w1temp to {}
  
set w2temp to {}
  
  
–当月の2週目以降
  
repeat
    repeat with j from 1 to 7
      set the end of w1temp to dayCounter
      
set the end of w2temp to dayCounter
      
      
set dayCounter to dayCounter + 1
      
if dayCounter > mlen1 then exit repeat
    end repeat
    
if dayCounter > mlen1 then exit repeat
    
set the end of d3List to w1temp
    
set the end of d4List to w2temp
    
    
set w1temp to {}
    
set w2temp to {}
    
  end repeat
  
  
–ここから翌月処理
  
  
–1週目
  
if j = 7 then
    set iii to 1
    
set the end of d3List to w1temp
    
set the end of d4List to w2temp
    
    
set w1temp to {}
    
set w2temp to {}
    
  else
    set iii to j + 1
  end if
  
  
set dayCounter to 1 –日付カウンタの初期化
  
repeat with j from iii to 7
    set the end of w1temp to dayCounter
    
set the end of w2temp to 0
    
    
set dayCounter to dayCounter + 1
  end repeat
  
  
set the end of d3List to w1temp
  
set the end of d4List to w2temp
  
  
set w1temp to {}
  
set w2temp to {}
  
  
–翌月の2週目以降
  
repeat
    repeat with k from 1 to 7
      set the end of w1temp to dayCounter
      
set the end of w2temp to 0
      
      
set dayCounter to dayCounter + 1
      
if dayCounter > mlen1 then exit repeat
    end repeat
    
if dayCounter > mlen1 then exit repeat
    
set the end of d3List to w1temp
    
set the end of d4List to w2temp
    
    
set w1temp to {}
    
set w2temp to {}
    
  end repeat
  
  
if length of w1temp is not equal to 0 then
    set the end of d3List to w1temp
    
set the end of d4List to w2temp
    
  end if
  
  
  
–ここまで3か月カレンダーの生成
  
  
  
–3か月カレンダーにおいて今日が何週目かを算出
  
set weekCounter to 1
  
  
repeat with aWeek in d4List –裏リストで操作を行う
    –ただし、該当曜日しかチェックしないので全チェックに比べて速度7倍
    
set aDay to (item mXDay of aWeek) as number
    
if aDay = (d1 as number) then
      exit repeat
    end if
    
set weekCounter to weekCounter + 1
  end repeat
  
end makeCalendar

–現在日時から曜日を取得を返す
on getDay(aDate as date)
  return (weekday of aDate) as number
end getDay

–指定日の月のみ返す
on getMonth(aDat as date)
  return (month of aDat) as number
end getMonth

–指定日の日付のみ返す
on getDate(aDat as date)
  return (day of aDat) as number
end getDate

–指定日の年のみ返す
on getYear(aDat as date)
  return (year of aDat) as number
end getYear

–現在のカレンダーで指定年月の日数を返す(国際化対応版)
on getMlen(aYear as integer, aMonth as integer)
  set theNSCalendar to current application’s NSCalendar’s currentCalendar()
  
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
  
return |length| of theResult
end getMlen

★Click Here to Open This Script 

Posted in Calendar Text | Tagged 10.11savvy 10.12savvy 10.13savvy Calendar | Leave a comment

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

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • 新発売:iWork Scripting Book with AppleScript
  • ChatGPTでchatに対する応答文を取得
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (186) 13.0savvy (59) CotEditor (60) Finder (47) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • 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
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • 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
  • 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)
  • 未分類

アーカイブ

  • 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