Markdownの見出しレベルを変更するスクリプト(CotEditor用)
1月 9th, 2018CotEditorでMarkdownのテキストを書いている時、見出しレベル(#で始まる行)をショートカットキーで変更できると便利そうだと思ったので、スクリプトを書いてみました。
下記のスクリプトをmacOS標準のAppleScriptエディタで入力し、適当なファイル名を付けて、CotEditorのスクリプトフォルダに保存します。
ファイル名例に付いている「^@k」、「^@j」というのはショートカットキーの指定(CotEditorの機能)。ここでは、control+command+kで見出しレベルを下げる、control+command+jで上げるように設定しています。
見出しレベルを下げる(md_heading_down.^@k.applescript)
tell application "CotEditor"
if exists front document then
tell front document
if coloring style is not in {"Markdown"} then
quit
end if
-- get selection's positional data
set {locLength, selectionLength} to range of selection
set {locLines, selectionLines} to line range of selection
set looseSelect to 0
-- whole lines select
if (selectionLines is greater than or equal to looseSelect as number) then
set line range of selection to {locLines, selectionLines}
set {locLength, selectionLength} to range of selection
end if
-- ignore last line break
if contents of selection ends with "
" then set range of selection to {locLength, selectionLength - 1}
-- get contents
set theSelection to contents of selection
if rich text 1 of theSelection is equal to "#" then
set contents of selection to "#" & theSelection
end if
end tell
end if
end tell
見出しレベルを上げる(md_heading_up.^@j.applescript)
tell application "CotEditor"
if exists front document then
tell front document
if coloring style is not in {"Markdown"} then
quit
end if
-- get selection's positional data
set {locLength, selectionLength} to range of selection
set {locLines, selectionLines} to line range of selection
set looseSelect to 0
-- whole lines select
if (selectionLines is greater than or equal to looseSelect as number) then
set line range of selection to {locLines, selectionLines}
set {locLength, selectionLength} to range of selection
end if
-- ignore last line break
if contents of selection ends with "
" then set range of selection to {locLength, selectionLength - 1}
-- get contents
set theSelection to contents of selection
if (selectionLength ≥ 2) and (rich text 1 thru 2 of theSelection is equal to "##") then
set contents of selection to rich text 2 thru -1 of theSelection
end if
end tell
end if
end tell