-
Notifications
You must be signed in to change notification settings - Fork 13
/
HTMLize Selected Text.applescript
257 lines (246 loc) · 24.8 KB
/
HTMLize Selected Text.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
(*
HTMLize Selected Text 1.3.2
Written by Eric Knibbe, 2008
This script will grab selected text from the frontmost window and convert non-standard characters to
named or numeric HTML entities, and optionally apply the Markdown and SmartyPants filters before
replacing the originally selected text with the processed version. It works best when associating it with
a hotkey using a utility like Butler, as it requires the user to manually select their frontmost application
when invoked from the Scripts menu under OS X 10.4 Tiger.
Markdown and SmartyPants are available at http://daringfireball.net/projects/, and should be present
in your Scripts folder, your path, or in one of the support folders for BBEdit, TextMate, or TextWrangler.
Also, ensure that GUI Scripting is enabled (use AppleScript Utility, located in /Applications/AppleScript).
Version history: see Description field
*)
-- Preferences
property useMarkdown : true
property useSmartyPants : true
property useNamedEntities : true
tell application "Finder" to set homepath to POSIX path of (home as alias)
set pathAdditions to "export PATH=$PATH:" & ¬
"/Applications/BBEdit.app/Contents/PlugIns/\"Language Modules\"/Markdown.bblm/Contents/Resources:" & ¬
"/Applications/TextWrangler.app/Contents/PlugIns/\"Language Modules\"/Markdown.bblm/Contents/Resources:" & ¬
"/Library/Scripts:" & ¬
"/Library/Scripts/bin:" & ¬
homepath & "Library/Scripts:" & ¬
homepath & "Library/Scripts/bin:" & ¬
homepath & "Library/\"Application Support\"/BBEdit/\"Unix Support\"/\"Unix Filters\":" & ¬
homepath & "Library/\"Application Support\"/TextMate/Scripts:" & ¬
homepath & "Library/\"Application Support\"/TextWrangler/\"Unix Support\"/\"Unix Filters\";"
set markdownCheck to do shell script pathAdditions & "Markdown.pl -shortversion; exit 0"
if markdownCheck = "" then
set useMarkdown to false
end if
set smartypantsCheck to do shell script pathAdditions & "SmartyPants.pl -shortversion; exit 0"
if smartypantsCheck = "" then
set useSmartyPants to false
end if
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
-- if the script was invoked from the Script menu under OS X 10.4 or earlier
if frontApp = "System Events" then
set appList to (name of every application process whose visible is true)
choose from list appList with prompt "Select your current application:"
if result is not false then
set frontApp to result as string
else
return
end if
end if
end tell
tell application frontApp to activate
tell application "System Events"
tell application process frontApp
tell (menu "Edit" of menu bar item "Edit" of menu bar 1)
click menu item "Copy"
end tell
end tell
-- pause to allow copy action to take effect
delay 0.2
-- remove formatting from the clipboard text
set theText to «class ktxt» of ((the clipboard as string) as record)
-- apply Markdown
if useMarkdown then
set theText to do shell script pathAdditions & ¬
"echo " & quoted form of theText & " | Markdown.pl;"
end if
-- apply SmartyPants
if useSmartyPants then
-- straighten any existing smart quotes so SmartyPants will recognize them
repeat with smartyChar in smartyChars
set theText to my find_replace(item 1 of smartyChar, item 2 of smartyChar, theText)
end repeat
set theText to do shell script pathAdditions & ¬
"echo " & quoted form of theText & " | SmartyPants.pl -2;"
end if
-- convert special characters
repeat with specialChar in specialChars
if useNamedEntities then
set theText to my find_replace(item 1 of specialChar, item 2 of specialChar, theText)
else
set theText to my find_replace(item 1 of specialChar, item 3 of specialChar, theText)
end if
end repeat
-- remove any styles applied by AppleScript from the text
set the clipboard to (do shell script "echo " & quoted form of theText)
-- replace the originally selected text
tell application process frontApp
tell (menu "Edit" of menu bar item "Edit" of menu bar 1)
if (menu item "Paste and Match Style" exists) then
click menu item "Paste and Match Style"
else
click menu item "Paste"
end if
end tell
end tell
end tell
property smartyChars : {¬
{"“", "\""}, ¬
{"”", "\""}, ¬
{"‘", "'"}, ¬
{"’", "'"}, ¬
{"–", "--"}, ¬
{"—", "---"}, ¬
{"…", "..."}}
-- since AppleScript supports only the MacRoman character set, only HTML entities within that set are listed
-- entity list from https://evolt.org/entities
property specialChars : {¬
{" ", " ", " ", "non-breaking space"}, ¬
{"¡", "¡", "¡", "inverted exclamation mark"}, ¬
{"¢", "¢", "¢", "cent sign"}, ¬
{"£", "£", "£", "pound sign"}, ¬
{"¥", "¥", "¥", "yen sign"}, ¬
{"§", "§", "§", "section sign"}, ¬
{"¨", "¨", "¨", "diaeresis"}, ¬
{"©", "©", "©", "copyright sign"}, ¬
{"ª", "ª", "ª", "feminine ordinal indicator"}, ¬
{"«", "«", "«", "left-pointing double angle quotation mark"}, ¬
{"¬", "¬", "¬", "not sign"}, ¬
{"®", "®", "®", "registered sign"}, ¬
{"¯", "¯", "¯", "macron"}, ¬
{"°", "°", "°", "degree sign"}, ¬
{"±", "±", "±", "plus-minus sign"}, ¬
{"´", "´", "´", "acute accent"}, ¬
{"µ", "µ", "µ", "micro sign"}, ¬
{"¶", "¶", "¶", "pilcrow sign"}, ¬
{"·", "·", "·", "middle dot"}, ¬
{"¸", "¸", "¸", "cedilla"}, ¬
{"º", "º", "º", "masculine ordinal indicator"}, ¬
{"»", "»", "»", "right-pointing double angle quotation mark"}, ¬
{"1⁄4", "¼", "¼", "vulgar fraction one quarter"}, ¬
{"1⁄2", "½", "½", "vulgar fraction one half"}, ¬
{"3⁄4", "¾", "¾", "vulgar fraction three quarters"}, ¬
{"¿", "¿", "¿", "inverted question mark"}, ¬
{"À", "À", "À", "latin capital letter A with grave"}, ¬
{"Á", "Á", "Á", "latin capital letter A with acute"}, ¬
{"Â", "Â", "Â", "latin capital letter A with circumflex"}, ¬
{"Ã", "Ã", "Ã", "latin capital letter A with tilde"}, ¬
{"Ä", "Ä", "Ä", "latin capital letter A with diaeresis"}, ¬
{"Å", "Å", "Å", "latin capital letter A with ring above"}, ¬
{"Æ", "Æ", "Æ", "latin capital letter AE"}, ¬
{"Ç", "Ç", "Ç", "latin capital letter C with cedilla"}, ¬
{"È", "È", "È", "latin capital letter E with grave"}, ¬
{"É", "É", "É", "latin capital letter E with acute"}, ¬
{"Ê", "Ê", "Ê", "latin capital letter E with circumflex"}, ¬
{"Ë", "Ë", "Ë", "latin capital letter E with diaeresis"}, ¬
{"Ì", "Ì", "Ì", "latin capital letter I with grave"}, ¬
{"Í", "Í", "Í", "latin capital letter I with acute"}, ¬
{"Î", "Î", "Î", "latin capital letter I with circumflex"}, ¬
{"Ï", "Ï", "Ï", "latin capital letter I with diaeresis"}, ¬
{"Ñ", "Ñ", "Ñ", "latin capital letter N with tilde"}, ¬
{"Ò", "Ò", "Ò", "latin capital letter O with grave"}, ¬
{"Ó", "Ó", "Ó", "latin capital letter O with acute"}, ¬
{"Ô", "Ô", "Ô", "latin capital letter O with circumflex"}, ¬
{"Õ", "Õ", "Õ", "latin capital letter O with tilde"}, ¬
{"Ö", "Ö", "Ö", "latin capital letter O with diaeresis"}, ¬
{"Ø", "Ø", "Ø", "latin capital letter O with stroke"}, ¬
{"Ù", "Ù", "Ù", "latin capital letter U with grave"}, ¬
{"Ú", "Ú", "Ú", "latin capital letter U with acute"}, ¬
{"Û", "Û", "Û", "latin capital letter U with circumflex"}, ¬
{"Ü", "Ü", "Ü", "latin capital letter U with diaeresis"}, ¬
{"ß", "ß", "ß", "latin small letter sharp s"}, ¬
{"à", "à", "à", "latin small letter a with grave"}, ¬
{"á", "á", "á", "latin small letter a with acute"}, ¬
{"â", "â", "â", "latin small letter a with circumflex"}, ¬
{"ã", "ã", "ã", "latin small letter a with tilde"}, ¬
{"ä", "ä", "ä", "latin small letter a with diaeresis"}, ¬
{"å", "å", "å", "latin small letter a with ring above"}, ¬
{"æ", "æ", "æ", "latin small letter ae"}, ¬
{"ç", "ç", "ç", "latin small letter c with cedilla"}, ¬
{"è", "è", "è", "latin small letter e with grave"}, ¬
{"é", "é", "é", "latin small letter e with acute"}, ¬
{"ê", "ê", "ê", "latin small letter e with circumflex"}, ¬
{"ë", "ë", "ë", "latin small letter e with diaeresis"}, ¬
{"ì", "ì", "ì", "latin small letter i with grave"}, ¬
{"í", "í", "í", "latin small letter i with acute"}, ¬
{"î", "î", "î", "latin small letter i with circumflex"}, ¬
{"ï", "ï", "ï", "latin small letter i with diaeresis"}, ¬
{"ñ", "ñ", "ñ", "latin small letter n with tilde"}, ¬
{"ò", "ò", "ò", "latin small letter o with grave"}, ¬
{"ó", "ó", "ó", "latin small letter o with acute"}, ¬
{"ô", "ô", "ô", "latin small letter o with circumflex"}, ¬
{"õ", "õ", "õ", "latin small letter o with tilde"}, ¬
{"ö", "ö", "ö", "latin small letter o with diaeresis"}, ¬
{"÷", "÷", "÷", "division sign"}, ¬
{"ø", "ø", "ø", "latin small letter o with stroke"}, ¬
{"ù", "ù", "ù", "latin small letter u with grave"}, ¬
{"ú", "ú", "ú", "latin small letter u with acute"}, ¬
{"û", "û", "û", "latin small letter u with circumflex"}, ¬
{"ü", "ü", "ü", "latin small letter u with diaeresis"}, ¬
{"ÿ", "ÿ", "ÿ", "latin small letter y with diaeresis"}, ¬
{"ƒ", "ƒ", "ƒ", "latin small f with hook"}, ¬
{"Ω", "Ω", "Ω", "greek capital letter omega"}, ¬
{"π", "π", "π", "greek small letter pi"}, ¬
{"•", "•", "•", "bullet"}, ¬
{"…", "…", "…", "horizontal ellipsis"}, ¬
{"⁄", "⁄", "⁄", "fraction slash"}, ¬
{"™", "™", "™", "trade mark sign"}, ¬
{"∂", "∂", "∂", "partial differential"}, ¬
{"∏", "∏", "∏", "n-ary product"}, ¬
{"∑", "∑", "∑", "n-ary sumation"}, ¬
{"√", "√", "√", "square root"}, ¬
{"∞", "∞", "∞", "infinity"}, ¬
{"∫", "∫", "∫", "integral"}, ¬
{"≈", "≈", "≈", "almost equal to"}, ¬
{"≠", "≠", "≠", "not equal to"}, ¬
{"≤", "≤", "≤", "less-than or equal to"}, ¬
{"≥", "≥", "≥", "greater-than or equal to"}, ¬
{"◊", "◊", "◊", "lozenge"}, ¬
{"Œ", "Œ", "Œ", "latin capital ligature OE"}, ¬
{"œ", "œ", "œ", "latin small ligature oe"}, ¬
{"Ÿ", "Ÿ", "Ÿ", "latin capital letter Y with diaeresis"}, ¬
{"ˆ", "ˆ", "ˆ", "modifier letter circumflex accent"}, ¬
{"˜", "˜", "˜", "small tilde"}, ¬
{"–", "–", "–", "en dash"}, ¬
{"—", "—", "—", "em dash"}, ¬
{"‘", "‘", "‘", "left single quotation mark"}, ¬
{"’", "’", "’", "right single quotation mark"}, ¬
{"‚", "‚", "‚", "single low-9 quotation mark"}, ¬
{"“", "“", "“", "left double quotation mark"}, ¬
{"”", "”", "”", "right double quotation mark"}, ¬
{"„", "„", "„", "double low-9 quotation mark"}, ¬
{"†", "†", "†", "dagger"}, ¬
{"‡", "‡", "‡", "double dagger"}, ¬
{"‰", "‰", "‰", "per mille sign"}, ¬
{"‹", "‹", "‹", "single left-pointing angle quotation mark"}, ¬
{"›", "›", "›", "single right-pointing angle quotation mark"}, ¬
{"€", "€", "€", "euro sign"}, ¬
{"∆", "∆", "∆", "increment"}, ¬
{"fi", "fi", "fi", "latin small ligature fi"}, ¬
{"fl", "fl", "fl", "latin small ligature fl"}, ¬
{"", "", "", "Apple logo"}, ¬
{"ı", "ı", "ı", "latin small letter dotless i"}, ¬
{"˘", "˘", "˘", "breve"}, ¬
{"˙", "˙", "˙", "dot above"}, ¬
{"˚", "˚", "˚", "ring above"}, ¬
{"˝", "˝", "˝", "double acute accent"}, ¬
{"˛", "˛", "˛", "ogonek"}, ¬
{"ˇ", "ˇ", "ˇ", "caron"}}
on find_replace(findText, replaceText, sourceText)
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to findText
set sourceText to text items of sourceText
set AppleScript's text item delimiters to replaceText
set sourceText to "" & sourceText
set AppleScript's text item delimiters to ASTID
return sourceText
end find_replace