Peter, as I mentioned in my related topic:
- Allows us to read, decode, change, and update the rich text in a KM Action
- Convert the rich text to plain text.
So, both issues have now been solved!
Many, many thanks. First, for exposing the macro and action objects to scripting. Then for your outstanding help and patience in answering all my questions, and providing the key code we needed for a solution.
I have to say, to everyone that might be reading this, Peter, as a developer, provides the most outstanding support for his product of any developer I've known in 30 years! No one else even comes close!
My TEST Script to Demo Getting Plain Text (NOT suitable for Production Use)
This was enabled by your above ObjC code, and is 99% Shane's code.
I made a few changes to refactor into a handler and add some error handling.
All errors are mine.
(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How to Get Plain Text from Rich Text in KM Action
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DATE: 2017-10-03
AUTHOR: ShaneStanley
REF:
• How Do I base64 Decode and Encode Multiple Lines?
• Late Night Software Ltd.,
• http://forum.latenightsw.com/t/how-do-i-base64-decode-and-encode-multiple-lines/759/11?u=jmichaeltx
The part this doesn’t really cover is how to edit the styled text (attributed string), which can be complicated depending on what you want to do. Assuming you don’t want to change the attributes themselves, the methods you’d use are replaceCharactersInRange:withString: and deleteCharactersInRange:. You get the ranges you use based on the unstyled text, as above.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
tell application "Keyboard Maestro"
set oMacro to item 1 of (get selected macros)
set actionList to actions in oMacro
set oAction to item 1 in actionList
tell oAction
set actXML to xml
set actionName to name
end tell -- oAction
set actPlainText to my kmActionRichTextToPlainText(actXML, actionName)
end tell
return actPlainText
--~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~
on kmActionRichTextToPlainText(pXMLStr, pActionName)
local theString, stringData, mutableDict, theError, theData, mutableAttString, plainString, richTextKeyStr
set LF to linefeed
--- Dictionary Key for Rich Text Block ---
set richTextKeyStr to "StyledText"
set theString to pXMLStr
set theString to current application's NSString's stringWithString:theString
-- convert string to data
set stringData to theString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
-- convert plist to mutable dictionary
set {mutableDict, theError} to current application's NSPropertyListSerialization's propertyListWithData:stringData options:(current application's NSPropertyListMutableContainersAndLeaves) |format|:(missing value) |error|:(reference)
if mutableDict is missing value then error (theError's localizedDescription() as text)
-- extract RTFD data and convert to a mutable atributed string
set theData to mutableDict's objectForKey:richTextKeyStr
if (theData is not missing value) then
--- Decode Rich Text ---
set mutableAttString to current application's NSMutableAttributedString's alloc()'s initWithRTFD:theData documentAttributes:(missing value)
--- GET PLAIN TEXT from Rich Text ---
set plainString to (mutableAttString's |string|()) as text ## as text needed to run in KM
else -- ERROR: "StyledText" key was NOT found
set plainString to ""
set msgStr to "❯❯❯ ERROR ❮❮❮" & LF & ¬
"'StyledText' key was NOT found in XML for Action: " & pActionName
set titleStr to "Handler: kmActionRichTextToPlainText"
beep
display dialog msgStr ¬
with title titleStr ¬
buttons {"Cancel", "OK"} ¬
default button ¬
"OK" cancel button ¬
"Cancel" with icon stop
end if
return plainString
end kmActionRichTextToPlainText