Automating the Keyboard Maestro Forum Editor

Hey Folks,

Some people are trying to automate the Keyboard Maestro forum editor, so let’s make that a little bit easier.

This script will fetch the text currently visible in Preview-Area of the forum editor.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/26 15:00
# dMod: 2016/08/26 15:16
# Appl: Safari
# Task: Get Keyboard Maestro Forum Editor Preview Text from Front Safari Window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Get, @Keyboard_Maestro, @Discourse, @Forum, @Editor,  @Preview, @Text @Front, @Window
--------------------------------------------------------------------------------

# Get the Text Content of the editor block.
set forumEditorText to doJavaScriptInSafari("document.getElementsByClassName('d-editor-preview')[0].textContent")

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
   try
      tell application "Safari" to do JavaScript javascriptStr in front document
   on error e
      error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
   end try
end doJavaScriptInSafari
--------------------------------------------------------------------------------

[ EDIT: 2016/08/27 13:15 – Altered Task Description – See new scripts below for working with the Keyboard Maestro Editor text field. ]

Thanks for this Chris, but it is not exactly what we need.
You are getting what's in the Preview panel, not what's in the Edit panel.

I've been playing with this, and I do not see a way to get, or set, what's in the Edit panel:

The <textarea ...> is the Edit panel.
I can get a reference to it, but it returns its innerText as empty ("").

But I'm a real novice with HTML DOM and JavaScript. Maybe someone more skilled can figure this out.

Hey JM,

Oops – you're right.

Look at this:

document.getElementsByClassName('ember-view ember-text-area d-editor-input')[0].value

And this:

document.getElementsByClassName('ember-view ember-text-area d-editor-input')[0].value = 'TEST TEXT'

-Chris

Chris, that works perfectly! :thumbsup:
Exactly what we need.
Many thanks.

Get the text from the Keyboard Maestro Forum Editor.

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

set javascriptStr to "
document.getElementsByClassName('ember-view ember-text-area d-editor-input')[0].value
"
set textFromForum to doJavaScriptInSafari(javascriptStr)

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
   try
      tell application "Safari" to do JavaScript javascriptStr in front document
   on error e
      error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
   end try
end doJavaScriptInSafari
--------------------------------------------------------------------------------

Set the text in the Keyboard Maestro Forum Editor.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/27 13:22
# dMod: 2016/08/27 13:27
# Appl: Safari
# Task: Set text visible in the Keyboard Maestro Forum Editor.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Set, @Text, @Keyboard_Maestro, @Forum, @Editor
--------------------------------------------------------------------------------

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

set javascriptStr to "
document.getElementsByClassName('ember-view ember-text-area d-editor-input')[0].value = '" & textToPostToForum & "'
"
set textFromForum to doJavaScriptInSafari(javascriptStr)

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
   try
      tell application "Safari" to do JavaScript javascriptStr in front document
   on error e
      error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
   end try
end doJavaScriptInSafari
--------------------------------------------------------------------------------

Thanks, Chris.

We are almost there.

Your script does SET the Editor Input area, but, like my macro, the Preview is NOT updated:

I can't seem to find a method/function that will update the Preview.

So, I had to resort to using KM to type a RETURN after I set the Editor.
This causes the Preview to be updated.

Hey JM,

I noticed that.

I'm guessing there is a keyboard-listener script waiting for keyboard-input to refresh the preview area (but I don't really know what I'm looking for).

It may be possible to run that via JavaScript, but inserting a space or a return should be adequate for now.

My inclination is to let the user do it, but I'll have to test more to make up my mind.

-Chris

IMO, leaving the Preview not refreshed would be very confusing to the user.

So, I'm going to either:

  1. Have KM type a RETURN
    OR
  2. Revert to using Paste.
  • Since I can now set the focus via JavaScript to the Editor Input area, this should be safe.
  • I'm tempted to do this anyway, because the only Browsers that support scripting are Safari and Chrome.
    • KM will upload using other browsers (just tested with FireFox).
    • So with those browsers, I'll have to use UI navigation instead of JavaScript.

Hey JM,

I just noticed that I can’t input mult-line text using .value = text with my AppleScripted JavaScript.

Can you confirm this?

I can do so using the Keyboard Maestro set-field-text action though, so I’m assuming I need to convert the bare string into a JavaScript string somehow.

Do you know how to manage the string, so JavaScript uses it properly?

-Chris

I figured it out.

You have to replace actual linefeeds with \\n in the AppleScript.

Okay, here’s a script you can just feed some text – and it will place it in the editor.

It automatically handles multi-line text.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/27 13:22
# dMod: 2016/08/27 20:29
# Appl: Safari
# Task: Set text visible in the Keyboard Maestro Forum Editor.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Set, @Text, @Keyboard_Maestro, @Forum, @Editor
--------------------------------------------------------------------------------
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 postTextToKeyboardMaestroForumEditor: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 postTextToKeyboardMaestroForumEditor: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 postTextToKeyboardMaestroForumEditor:
--------------------------------------------------------------------------------

A simplified version of the get forum editor text script.

I’ve tested this successfully in another Discourse Forum, so henceforth handlers and such will be named more generically.

-Chris

--------------------------------------------------------------------------------
# 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
--------------------------------------------------------------------------------
1 Like