はじめに
Markdown でブログ書いていても、読み仮名は HTML で書く必要があります。読み仮名、ときどき入れたくなります。「
CotEditor のスクリプトを書いた
ブログの編集に使っている CotEditor で入力しやすいように、AppleScript でスクリプトを書きました。
せっかくなので共有します。よろしければ CotEditor のスクリプトフォルダに入れてご利用ください。
ルビを振る.scpt
-- 開発環境: -- AppleScript 2.7 -- CotEditor 4.0.2(457) -- macOS Catalina 10.15.7 tell application "CotEditor" tell document 1 -- エディタの選択文字列を取得する set src to contents of selection if src = "" then set existsSelectedWord to false else set existsSelectedWord to true end if -- 最初のダイアログ try set theResponse to display dialog "ルビを振る文字列を指定してください。" default answer src buttons {"キャンセル", "次へ >"} default button "次へ >" if button returned of theResponse = "キャンセル" then return on error return end try set src to text returned of theResponse if src = "" then return -- 最初にエディタで文字列が選択されていなければ選択状態にする if not existsSelectedWord then findNext(src) of me end if -- 2回目のダイアログ try set theResponse to display dialog "ルビを指定してください。" default answer "" buttons {"キャンセル", "変換する"} default button "変換する" if button returned of theResponse = "キャンセル" then return on error return end try set ruby to text returned of theResponse if ruby = "" then return -- 変換する replaceSelection(src, ruby) of me -- 変換できそうなものがあり続けるなら繰り返し変換するか尋ねる repeat findNext(src) of me set theRange to range of selection set theLength to (item 2 of theRange) + 0 if theLength = 0 then exit repeat try set theResponse to display dialog "続けて変換しますか?" buttons {"キャンセル", "変換する"} default button "変換する" if button returned of theResponse = "キャンセル" then exit repeat on error return end try replaceSelection(src, ruby) of me end repeat end tell end tell on replaceSelection(src, ruby) set replacement to "<ruby><rb>" & src & "</rb><rp>(</rp><rt>" & ruby & "</rt><rp>)</rp></ruby>" tell application "CotEditor" tell document 1 set contents of selection to replacement end tell end tell end replaceSelection on findNext(src) tell application "CotEditor" tell front document set selRange to range of selection set startPos to (item 1 of selRange) + (item 2 of selRange) set range of selection to {startPos, 0} end tell find front document for src & "(?!<\\/)" with wrap and RE end tell end findNext
以下の環境で確認。
- macOS Catalina 10.15.7
- AppleScript 2.7
- CotEditor 4.0.2(457)
使い方
スクリプトを起動すると、ルビを振りたい単語の入力を求められます。CotEditor 上で選択している文字列があれば、それが初期入力されています。
「次へ」ボタンをクリックすると、振りたいルビの入力を求められます。
「変換する」ボタンをクリックすると、HTML の ruby タグに変換します。
続けて文章の中に含まれる同じ単語を選択状態にし、同じように変換するかどうか、確認を求められます。「変換する」をクリックすると、クリックするたびに単語に ruby タグが設定されていきます。
たくさん変換したい場合はキャンセルして、最初に変換された文字列で CotEditor の検索・置き換え機能を使った方が早いでしょう。
最後に
振りたいルビは、元文字列から再変換して自動入力したい。