I have written a little JavaScript that converts a selected text, into a "Book Title"-like capitalized text. E.g. "The end of the game" is replaced with "The End of the Game". A list of articles ("the", "a", etc.) and prepositions ("at", "on", etc.) are *not* capped.
The code should be a nice addition to the already existing Format menu, "To Upper Case", "To Lower Case", "Capitalize" and "Invert Case" functions already in UE. I'll be posting the code later in the thread.
Since this is my first JavaScript vs. UE code I have come across a few things I don't understand, because these things would work in ANSI C, or D or PHP, but do not in JS.
Issues:
The code should be a nice addition to the already existing Format menu, "To Upper Case", "To Lower Case", "Capitalize" and "Invert Case" functions already in UE. I'll be posting the code later in the thread.
Since this is my first JavaScript vs. UE code I have come across a few things I don't understand, because these things would work in ANSI C, or D or PHP, but do not in JS.
Issues:
- Selected text is lower cased, but the text variable ignores the *current* state of the selected text:
The manual states that .selection: "** This is a READ ONLY property of the active/specified document". As I understand it this is actually not "read only", but in fact .selection is a state remembered at launch time of the script, and this state is *not* updated.Code: Select all
UltraEdit.activeDocument.toLower(); // Lower case selected text var text = UltraEdit.activeDocument.selection; // Remember selected text UltraEdit.outputWindow.write( text ); // Output "text" to console
If I understand this correctly... then what use is something like toLower() if you have no access to the changed selection? Would you have to manually "re-select" the changed text? - Overwriting a single char in a string array?
But for some reason JS does not let you overwrite the single char in words[0][0]. What is up with that?Code: Select all
var text = UltraEdit.activeDocument.selection; var words = text.split(" "); // Trying to change the capitalization of the 1st word's 1st letter // E.g. words[0] = "end" -> words[0][0].toLocaleUpperCase() yields "E" words[0][0] = words[0][0].toLocaleUpperCase();
I did something like this to fix the problem:
Code: Select all
words[0] = words[0][0].toLocaleUpperCase() + words[0].slice(1);