[script] Switch case (like in MS Word)

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

[script] Switch case (like in MS Word)

Post 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;
}
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Post Reply