-
Notifications
You must be signed in to change notification settings - Fork 13
/
Journler2Blogger.applescript
319 lines (305 loc) · 30.6 KB
/
Journler2Blogger.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
(*
Journler2Blogger 1.5.1
Written by Eric Knibbe, 2008
This script allows posting from Journler directly to Blogger. It relies on the same backend
used by Blogger's BlogThis! bookmarklet. If you have Markdown.pl or SmartyPants.pl installed
in your Scripts folder, your path, or in the support folders for BBEdit, TextMate, or
TextWrangler, they will be applied to your text. Most special characters, if present, will be
converted to named or numeric entity references before posting.
After showing a preview of the processed text, the script copies it to the clipboard and a
page is opened in your default browser, into which you paste your entry for posting.
To install, place this file in ~/Library/Scripts/Journler/ and link it to Journler's "Blog" button
under Preferences > Advanced. In order to use Markdown.pl and SmartyPants.pl, they should
be in one of the folders listed under pathAdditions, preferably in /Library/Scripts. If either is
not found, the script skips them silently.
Notes:
- Posts beyond a certain length will not fit in the preview dialog, although the script will still work.
- Since Journler entries' "is blogged" attribute cannot be set using Applescript, this
script instead tags the entry as "Blogged".
- Blogged entries' Created dates are by default reset to the current date, so Journler lists them in
the order they were posted.
- Your Blogger blog's "Convert line breaks" setting should be off.
Version history: see Description field
*)
-- Preferences
property useMarkdown : true
property useSmartyPants : true
property useNamedEntities : true
property setEntryDate : true
property tagAsBlogged : "Blogged"
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 "Journler.app"
set blogEntries to «class psEN»
if blogEntries = {} then
display alert "There are no entries selected." message "Please select an entry to post." as warning buttons {"Cancel"} default button 1 giving up after 5
else
repeat with theEntry in blogEntries
-- add tags for bold, italic, and/or non-black text
set entryFonts to {}
set entryColors to {}
repeat with textRun in every «class catr» of theEntry
set entryFonts to entryFonts & {(font of textRun) as string}
set entryColors to entryColors & {(color of textRun)}
end repeat
set entryText to every «class catr» of theEntry
repeat with i from 1 to the number of items in entryText
if item i of entryFonts contains "Bold" then
set item i of entryText to "<strong>" & item i of entryText & "</strong>"
end if
if item i of entryFonts contains "Italic" then
set item i of entryText to "<em>" & item i of entryText & "</em>"
end if
if item i of entryFonts contains "Oblique" then
set item i of entryText to "<em>" & item i of entryText & "</em>"
end if
if item i of entryColors as string is not "000" then
set item i of entryText to "<span style=\"color:" & my RGB_to_HEX(item i of entryColors) & ¬
"\">" & item i of entryText & "</span>"
end if
end repeat
set entryText to entryText as string
-- apply Markdown
if useMarkdown then
set entryText to do shell script pathAdditions & ¬
"echo " & quoted form of entryText & " | 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 entryText to my find_replace(item 1 of smartyChar, item 2 of smartyChar, entryText)
end repeat
set entryText to do shell script pathAdditions & ¬
"echo " & quoted form of entryText & " | SmartyPants.pl -2;"
end if
-- convert special characters
repeat with specialChar in specialChars
if useNamedEntities then
set entryText to my find_replace(item 1 of specialChar, item 2 of specialChar, entryText)
else
set entryText to my find_replace(item 1 of specialChar, item 3 of specialChar, entryText)
end if
end repeat
-- display preview dialog before copying to clipboard and opening browser window
if the button returned of (display dialog entryText with title (name of theEntry as string) ¬
buttons {"Cancel", "Copy & Post"} cancel button 1 default button 2) is "Copy & Post" then
set the clipboard to (do shell script "echo " & quoted form of entryText)
if setEntryDate then
set «class pDCD» of theEntry to (current date)
end if
set isTagged to false
set theEntryTags to «class pTAG» of theEntry
repeat with tag in theEntryTags
if tag as string is equal to tagAsBlogged then
set isTagged to true
end if
end repeat
if isTagged is false then
set «class pTAG» of theEntry to («class pTAG» of theEntry) & tagAsBlogged
end if
set entryNameEncoded to my encode_URL_string(name of theEntry)
tell application "System Events"
open location "http://www.blogger.com/blog_this.pyra?&n=" & entryNameEncoded
end tell
end if
end repeat
«event aevtcSVC»
end if
end tell
property hex_list : (characters of "0123456789ABCDEF")
property allowed_URL_chars : (characters of "$-_.+!*(),1234567890abcdefghijklmnopqrstuvwxyz")
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"}}
-- http://www.macosxautomation.com/applescript/sbrt/sbrt-04.html
on RGB_to_HEX(RGBvalues)
set hex_value to ""
repeat with RGBvalue in RGBvalues
set this_value to RGBvalue div 256
if this_value is 256 then set this_value to 255
set x to item ((this_value div 16) + 1) of hex_list
set y to item (((this_value / 16 mod 1) * 16) + 1) of hex_list
set hex_value to (hex_value & x & y) as string
end repeat
return ("#" & hex_value)
end RGB_to_HEX
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
-- http://schinckel.net/2006/03/04/searching-zen/
on encode_URL_string(this_item)
set character_list to (characters of this_item)
repeat with this_char in character_list
if this_char is not in allowed_URL_chars then set this_char to my encode_URL_char(this_char)
end repeat
return character_list as string
end encode_URL_string
on encode_URL_char(this_char)
set ASCII_num to (ASCII number this_char)
set x to item ((ASCII_num div 16) + 1) of hex_list
set y to item ((ASCII_num mod 16) + 1) of hex_list
return ("%" & x & y) as string
end encode_URL_char