Drafts Action Directory

shortcuts

Posted by dchar, Last updated: 2018-04-27 03:03:36 UTC

This action scans the current draft for shortcut abbreviations and replaces them with larger phrases of text that you define.

Defining shortcuts

Create a draft with a tag shortcuts and add some abbreviations you want expanded into larger phrases of text. Put each abbreviation and phrase on one line separated by a comma.

.hw,Hello World
.pi,3.14159265358979323846

.ty,Thank you for your assistance in resolving this matter.

Multiline phrases can be enclosed in double quotes.

.multiline,"
Line 1
Line 2
Line 3"

.reg,"
Warmest Regards,
Clark Griswold"

.li,"
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua..."

Using shortcuts

Type out the abbreviations as you write your draft.

Programmers say ".hw"

pi to 20 places is .pi

Insert random text with

.li

Run the shortcut action to expand the abbreviations. You can run the shortcut action once when you’re done or periodically while you’re typing.

Programmers say "Hello World"

pi to 20 places is 3.14159265358979323846

Insert random text with

Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...
Install

Steps

  • script

    //
    // A simple shortcut replacement action
    //
    
    const shortcutTag = 'shortcuts';
    
    //
    // Multiline CSV parser. Copied from StackOverflow:
    // https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data
    //
    function parseCSV(str) {
        var arr = [];
        var quote = false;  // true means we're inside a quoted field
    
        // iterate over each character, keep track of current row and column (of the returned array)
        for (var row = col = c = 0; c < str.length; c++) {
            var cc = str[c], nc = str[c+1];
            arr[row] = arr[row] || [];
            arr[row][col] = arr[row][col] || '';
    
            // If the current character is a quotation mark, and we're inside a
            // quoted field, and the next character is also a quotation mark,
            // add a quotation mark to the current column and skip the next character
            if (cc == '"' && quote && nc == '"') { arr[row][col] += cc; ++c; continue; }  
    
            // If it's just one quotation mark, begin/end quoted field
            if (cc == '"') { quote = !quote; continue; }
    
            // If it's a comma and we're not in a quoted field, move on to the next column
            if (cc == ',' && !quote) { ++col; continue; }
    
            // If it's a newline (CRLF) and we're not in a quoted field, skip the next character
            // and move on to the next row and move to column 0 of that new row
            if (cc == '\r' && nc == '\n' && !quote) { ++row; col = 0; ++c; continue; }
            if (cc == '\n' && !quote) { ++row; col = 0; ++c; continue; }
    
            // If it's a newline (LF or CR) and we're not in a quoted field,
            // move on to the next row and move to column 0 of that new row
            if (cc == '\n' && !quote) { ++row; col = 0; continue; }
            if (cc == '\r' && !quote) { ++row; col = 0; continue; }
    
            // Otherwise, append the current character to the current column
            arr[row][col] += cc;
        }
        return arr;
    }
    
    function* parseShortcutDrafts(draftsArr)
    {
        for (let d of draftsArr) {
            var arr = parseCSV(d.content);
            for (let r of arr) {
                if (r.length < 2) {
                    continue;
                }
                // remove leading newline  
                if (r[1][0] == '\n') {
                    r[1] = r[1].substr(1);
                }
                yield r;
            }
        }
    }
    
    (() => {
        var shortcutDrafts = Draft.query("", "all", [shortcutTag]);
        if (shortcutDrafts.length == 0) {
            var msg = "";
            msg += "No shortcuts defined.\n";
            msg += "\n";
            msg += "Create a draft with a tag 'shortcuts'\n";
            msg += "and add some abbreviations you want\n";
            msg += "expanded into larger phrases of text.\n";
            msg += "\n";
            msg += "Put each shortcut and phrase on one line\n";
            msg += "separated by a comma. Multiline phrases\n";
            msg += "can be enclosed in double quotes";
            alert(msg);
            context.fail();
            return;
        }
    
        if (draft.hasTag(shortcutTag)) {
            var msg = "";
            msg += "You can't run the replacement\n";
            msg += "action on a shortcut file.";
            alert(msg);
            context.fail();
            return;
        }
    
        var content = draft.content;
    
        for (let shortcut of parseShortcutDrafts(shortcutDrafts))
            content = content.replace(shortcut[0], shortcut[1]);
        
        draft.content = content;
        draft.update();
    })()
    
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.