Automating the Discourse Forum Editor

Hey Folks,

Working in the Discourse Forum Editor can be tedious at times.

Here’s a couple of scripts to help with that.

###Get text from the open Discourse Editor.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/27 13:22
# dMod: 2016/08/27 20:51
# Appl: Safari
# Task: Get text visible in the Discourse Forum Editor.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Get, @Text, @Discourse, @Forum, @Editor
# Vers: 1.00
--------------------------------------------------------------------------------

set discourseForumEditorText to getDiscourseEditorText()

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on getDiscourseEditorText()
   tell application "Safari"
      do JavaScript "document.getElementsByClassName('ember-view ember-text-area d-editor-input')[0].value" in front document
   end tell
end getDiscourseEditorText
--------------------------------------------------------------------------------

###Set the text of the open Discourse editor.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/27 13:22
# dMod: 2016/08/27 20:29
# Appl: Safari
# Task: Set text visible in a Discourse Forum Editor.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Set, @Text, @Discourse, @Forum, @Editor
# Vers: 1.00
--------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
--------------------------------------------------------------------------------

set postText to "Now is the time
for all good men
to come to the aid
of their country.
"

my postTextToDiscourseForumEditor:postText

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
   set anNSString to current application's NSString's stringWithString:dataString
   set dataString to (anNSString's ¬
      stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
         options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
--------------------------------------------------------------------------------
on postTextToDiscourseForumEditor:postText
   set postText to my cngStr:"[\\n\\r]" intoString:"\\\\n" inString:postText
   tell application "Safari"
      do JavaScript ¬
         "document.getElementsByClassName('ember-view ember-text-area d-editor-input')[0].value = '" & postText & "'" in ¬
         front document
   end tell
end postTextToDiscourseForumEditor:
--------------------------------------------------------------------------------

-Chris