XML Escape


#1

A custom action that “escapes” a text string to make it usable in XML.

XML Escape.v1.0.zip (7.2 KB)

Results:

Here’s the JXA I used for the encoding:

function escapeXml(unsafe) {
    return unsafe.replace(/[<>&'"]/g, function (c) {
        switch (c) {
            case '<': return '&lt;';
            case '>': return '&gt;';
            case '&': return '&amp;';
            case '\'': return '&apos;';
            case '"': return '&quot;';
        }
    });
}

I got this from StackOverflow, and it’s quite possible it doesn’t cover everything. If that’s the case, please let me know so I can update it. Thanks!

REQUIREMENTS

  • Yosemite
    • The underlying scripts are written in Javascript for Applications

INSTALLATION

  • Drag the .zip file you require onto the Keyboard Maestro icon in the OS X Dock.
  • (if updating a previous version of the action, first manually remove the previous copy from the custom actions folder)
    • ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions

MACRO: Dynamic Prompt for User Input
#2

I keep writing that too …

–> 'JavaScript for Automation’
https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html


#3

Yeah, I know - I stole that whole section from you! :sweat_smile:


#4

Good to see more JavaScript resources building up …