1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

日本語のイベント名検索がダメな macOS のカレンダーアプリからちゃんと検索できるアプリを作った

Last updated at Posted at 2024-07-13

イベント名検索がダメな macOS のカレンダーアプリ

 macOS と iOS, iPadOS でメインで使用しているカレンダーアプリですが、あれ?あれはいつだったかな?と検索したら iOS版、iPadOS版のカレンダーでは見つかるのに、macOS版だと「結果なし」と表示され、一つも見つけてこない。
(私の環境だけ?)
英語表記のイベントは問題なくヒットして表示してくれる。

自分で機能強化だ!

 AppleScript で検索するものを作ろうと仕様を考えてみたが、AppleScript の文字列検索はかなり面倒くさいので grep コマンドの助けを借りようと思った。
また、せっかくイベント名と日時を取り出すので、それをファイルとして保存できるようにすれば、一石二鳥だと思った。

完成した AppleScript

 ということで、作成したアプリのソースはこちら

ExportCalenderEvent.scpt
-- カレンダー抽出・検索アプリ
--
-- カレンダーアプリから指定された文字列を含むイベント名、日時を表示するアプリ
-- カレンダーアプリはイベントの検索がうまく動作しないので、イベントを取り出し、
-- デスクトップ上に .csv ファイルとして出力した後、そのファイルから指定された
-- 文字列にヒットしたイベントを表示するアプリ
-- by YNOMURA.COM

-- 検索文字列を入力(文字列を入力しないと、ファイル出力のみ)
display dialog "検索イベント名?" default answer ""
set ans to item 2 of text of result

tell application "Calendar"
   set CR to ASCII character 13
   set LF to ASCII character 10
   -- 複数のカレンダーから対象のカレンダーを選ぶ
   name of calendars whose writable is true
   set selectedCalendar to choose from list result
   if selectedCalendar is false then return
   set selectedCalendar to selectedCalendar as Unicode text
   get calendar selectedCalendar
   set sums to ""
   set allEvents to events of (calendar selectedCalendar)
   repeat with i from 1 to the number of allEvents
   	set sum to summary of item i of allEvents
   	set evDate to my DateTime(start date of item i of allEvents)
   	set sums to sums & evDate & "," & sum & LF
   end repeat
   set cmd to ("cat - << EOS > ~/Desktop/" & selectedCalendar as text) & ".csv" & LF
   set cmd to cmd & sums & "EOS"
   do shell script cmd
   display dialog (i as text) & " 件を抽出しました"
   
   -- 出力された予定のリストから指定された文字列を検索・表示する
   if ans = "" then return
   set cmd to ("grep " & ans & " " & "~/Desktop/" & selectedCalendar as text) & ".csv"
   set rslt to do shell script cmd
   display dialog rslt
   
   -- set the clipboard to sums --(クリップボードにコピーしたいときコメントアウトする)
end tell

-- ここからは関数
------------------#日付を数字テキストに
to DateTime(theDate)
   set y to (year of theDate)
   set m to Month2Num(month of theDate)
   set d to day of theDate
   set hms to time of theDate
   set hh to h of Sec2HMS(hms)
   set mm to m of Sec2HMS(hms)
   set ss to s of Sec2HMS(hms)
   return (y as text) & "/" & Fzero(m) & "/" & Fzero(d) & "," & Fzero(hh) & ":" & Fzero(mm)
   
end DateTime
------------------#月の表示を数字に
to Month2Num(theMonth)
   set monList to {January, February, March, April, May, June, July, August, September, October, November, December}
   repeat with i from 1 to 12
   	if item i of monList is theMonth then exit repeat
   end repeat
   return i
end Month2Num
------------------#2桁の数字テキストに
to Fzero(n)
   if n < 10 then
   	return "0" & n
   else
   	return n as text
   end if
end Fzero
------------------#時間表示を:で分割
to Sec2HMS(sec)
   set ret to {h:0, m:0, s:0}
   set h of ret to sec div hours
   set m of ret to (sec - (h of ret) * hours) div minutes
   set s of ret to sec mod minutes
   return ret
end Sec2HMS

使用方法

 このスクリプトを実行すると、最初に検索する文字列を入力するダイアログが出力されます。
ここに検索文字列を入れないと、単純にカレンダーからイベント名と日時が抽出された CSV
ファイルを出力します。
次に、複数のカレンダーのリストが表示されますので、対象にしたいカレンダーを一つ選択します。
抽出した件数が表示され、CSV ファイルが出力されます。Numbersアプリなどで開けますね。
次に指定された文字列にヒットしたイベントが表示されます。

おまけ

 ソース中にクリップボードへのコピー機能がコメントアウトされていますので、必要ならご使用ください。

ツッコミや改善案は大歓迎

 ソースのバグなどの指摘、改善案など大歓迎です。コメントがあると励みになります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?