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. ]
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
--------------------------------------------------------------------------------
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.
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?
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
--------------------------------------------------------------------------------