Page 1 of 1

[script] Switch case (like in MS Word)

Posted: 03 Jun 2014 09:36
by pjj
This is not my aforementioned backup script, but a little one that may come in handy, though. It switches case from lower case to UPPER CASE to Name Case, like in MS Word, so you need only one keyboard shortcut. If nothing is selected, word under cursor is selected automatically (again like in MS Word.) Known limitations:
* Namecase() fails if a word starts with non-letter, e.g. ( or " or when selection starts with a space -- interestingly, Format (in the Manin Menu) > Change Case > Capitalize works fine in this regard (it would be beneficial to fix Namecase(), I think)
* if column mode is on, selection isn't preserved

Enjoy!

Code: Select all

/*
  switches circularly between lower case--UPPER CASE--Name Case (like in MS Word)
*/

var selection = Document.SelText;
var selectionStart = Document.SelStart;
var selectionLength = Document.SelLength;

if (selection == "") {
    Document.CursorWordLeft(false);
    Document.CursorWordRight(true);
    selection = Document.SelText;
}

if (selection == Lowercase(selection)) {
    Document.InsertText(Uppercase(selection));
} else if (selection == Uppercase(selection)) {
    Document.InsertText(Namecase(selection));
} else {
    Document.InsertText(Lowercase(selection));
}

// bring back original selection
if (Document.IsColumnModeActive() != true) {
    Document.SelStart = selectionStart;
    Document.SelLength = selectionLength;
}