Skip to content

Instantly share code, notes, and snippets.

@Antisunny
Last active January 1, 2020 08:45
Show Gist options
  • Save Antisunny/2e5fee812937f606391002f5012c9be9 to your computer and use it in GitHub Desktop.
Save Antisunny/2e5fee812937f606391002f5012c9be9 to your computer and use it in GitHub Desktop.
cotEditor script/ wrap with ...
--
-- Created by: mitoxys
-- Created on: 20/1/1
--
-- Copyright ©2019 mitoxys.beer, All Rights Reserved
--
use AppleScript version "2.7"
use scripting additions
use framework "Foundation"
to template_content to pool_name
set TemplatePool to {underlined_red:"<span class=\"highlight_underlined_red\">䷖</span>", bg_blue:"<span class=\"highlight_bg_blue\">䷖</span>", bg_orange:"<span class=\"highlight_bg_orange\">䷖</span>", bg_purple:"<span class=\"highlight_bg_purple\">䷖</span>"}
set TemplatePoolData to my (NSDictionary's dictionaryWithDictionary:TemplatePool)
set TemplatePoolDataKeys to TemplatePoolData's allKeys()
if (TemplatePoolDataKeys as list) contains pool_name then
set item_value to TemplatePoolData's valueForKey:pool_name
return item_value as text
else
return false
end if
end template_content
on template_fill into template under content
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {""}
set resultText to ""
set templateParts to (text items of template as list)
set maxLength to length of templateParts
repeat with idx from 1 to maxLength
if idx comes before maxLength then
set resultText to resultText & item idx of templateParts & content
else
set resultText to resultText & item idx of templateParts
end if
end repeat
set AppleScript's text item delimiters to saveTID
return resultText
end template_fill
on selection_text into thisDocument under range
set documentText to thisDocument
set selectionRange to range
set startPos to item 1 of selectionRange
set selectionLength to item 2 of selectionRange
set endPos to startPos + selectionLength
set selectionText to characters startPos thru endPos of documentText
return selectionText as text
end selection_text
tell application "CotEditor"
set styleList to
"underlined_red", ¬
"bg_blue", ¬
"bg_orange", ¬
"bg_purple"}
set thisDocumentText to contents of front document
set selectionRange to range of selection of front document
set selectionText to selection_text of me into thisDocumentText under selectionRange
choose from list styleList with title "选择所选文字外套样式" with prompt "选择一项" default items {"bg_blue"} OK button name "确定" cancel button name "取消"
if the result is not false then
set styleName to item 1 of the result
set templateText to template_content of me to styleName
if templateText is not false then
set resultText to template_fill of me into templateText under selectionText
set contents of selection of front document to resultText
end if
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment