Macのメモ帳アプリの中身をローカルに保存する
参考情報
@hyuki: おひるねするときに目を閉じるとアイディアが浮かぶのでSiriにお願いして口頭でアイディアメモとして書き留めます。 結城「Hey Siri, メモを書いて」
Siri「内容はどうしますか?」
という対話。iCloudのメモ(notes)に保存される。cronで自動的にmd.privateに移動する。すばらしい😊
https://pbs.twimg.com/media/G1Q2rStbQAAWi-C.jpg
@hyuki: ◆notes-to-md-private: 「メモ」アプリからメモを抽出するツール by Claude Code 考えてみると、私はSNSに書くんじゃなくて、私はクロコさん(Claude Code)に直接伝えるべきでした。
https://pbs.twimg.com/media/GwwG4zvaoAAxd3S.jpg
code:move_notes.scpt
-- Notes.app から直近1週間のノートを取得し、Markdownファイルに保存後、archive フォルダへ移動する
set oneWeekAgo to (current date) - (7 * days)
-- 保存先フォルダを指定(例:デスクトップの NotesExports フォルダ)
set targetFolder to POSIX path of ("/Users/Tadanori/Library/CloudStorage/Dropbox/ideaMCP/notes/")
tell application "Notes"
set sourceFolder to folder "inbox" of account "iCloud"
set archiveFolder to folder "archive" of account "iCloud"
set theNotes to notes of sourceFolder
repeat with n in theNotes
set cDate to creation date of n
if cDate ≥ oneWeekAgo then
set noteTitle to name of n
set noteBody to body of n
-- ファイル名を安全にする
set safeTitle to my sanitizeFileName(noteTitle, cDate)
-- 保存パス
set filePath to targetFolder & safeTitle & ".md"
-- Markdown用テキスト
set plainBody to my htmlToPlainText(noteBody)
-- メタ情報付きで書き出し
set mdText to "---" & return
set mdText to mdText & "title: " & noteTitle & return
set mdText to mdText & "created: " & (cDate as string) & return
set mdText to mdText & "modified: " & (modification date of n as string) & return
set mdText to mdText & "---" & return & return
set mdText to mdText & "# " & noteTitle & return & return & plainBody
my writeToFile(mdText, filePath)
-- 書き出しが終わったら archive フォルダに移動
move n to archiveFolder
end if
end repeat
end tell
-- ▼ タイトルをファイル名として安全化し、日付プレフィックスを付ける
on sanitizeFileName(theText, cDate)
-- 日付を YYYY-MM-DD 形式に整形
set {year:y, month:m, day:d} to cDate
set mNum to m as integer
if mNum < 10 then set mNum to "0" & mNum
if d < 10 then set d to "0" & d
set datePrefix to (y as text) & "-" & (mNum as text) & "-" & (d as text)
-- タイトル中の禁止文字を置換
set invalidChars to {":", "/", "*", "?", "<", ">", "|", "\"", return, linefeed}
set safeText to theText
repeat with c in invalidChars
set AppleScript's text item delimiters to c
set safeText to text items of safeText
set AppleScript's text item delimiters to "_"
set safeText to safeText as text
end repeat
set AppleScript's text item delimiters to ""
-- 仕上げ:notes_プレフィックス付きファイル名
return datePrefix & "-notes_" & safeText
end sanitizeFileName
-- ▼ HTML → プレーンテキスト(超簡易版)
on htmlToPlainText(theHTML)
set theText to theHTML
set theText to my replaceText(theText, "<br>", return)
set theText to my replaceText(theText, "<^>+>", "") return theText
end htmlToPlainText
-- ▼ 正規表現置換
on replaceText(theText, pattern, replacement)
set shCmd to "echo " & quoted form of theText & " | perl -pe 's/" & pattern & "/" & replacement & "/g'"
return do shell script shCmd
end replaceText
-- ▼ 書き込み
on writeToFile(theText, filePath)
do shell script "mkdir -p " & quoted form of (POSIX path of (do shell script "dirname " & quoted form of filePath))
set fRef to open for access POSIX file filePath with write permission
set eof fRef to 0
write theText to fRef as «class utf8»
close access fRef
end writeToFile
これを定期的に実行すればいい。