Tagged: macro case
- AuthorPosts
- February 6, 2015 at 5:05 am #19825MeirParticipant
Hey,
Apologies, JavaScript is new to me…
I am trying to write a macro to toggle a selection or the word where the cursor is between lowercase and uppercase.
the following code works fine if a text is already selected:s = document.selection.Text; if( s.length == 0 ){s = document.selection.SelectWord();} n = s.charCodeAt(0); if( n >= 0x41 && n <= 0x5a ){ s = s.toLowerCase(); } else { s = s.toUpperCase(); } document.selection.Text = s;
But if nothing is selected (and the
document.selection.SelectWord()
is supposed to kick in) I get the error message:‘s’ is null or not an object at line 3
Where am I wrong?
February 6, 2015 at 5:19 am #19826MeirParticipantSorry, got it!
One has to also load the selection into a variable, so I addeds = document.selection.Text;
after extending the selection range.
(Now I have to figure out how to extend the toggle to also capitalize…)
So, what is the ‘Capitalize’ equivalent tos.toLowerCase
?
And alternatively, how candocument.selection.ChangeCase(eeCaseCapitalize);
be used?
Regards
MeirFebruary 6, 2015 at 11:47 am #19827StefanParticipantif(document.selection.IsEmpty){document.selection.SelectWord()}
document.selection.ChangeCase(eeCaseCapitalize); //Title caseFebruary 8, 2015 at 2:27 am #19829MeirParticipantThank you Stefan!
OK, now my first ever JavaScript macro is complete (bug reports are welcomed) :
/* This eeJavaScript macro will toggle a selected string through the sequence of lower case -> capitalize -> uppercase -> lower case again. If no string is selected, the word where the cursor is will. The starting state is a function of the "first" and "next" characters. * If both characters are upper, the string will be converted be lower. * If only the first character is upper, the string will be converted to upper. * If neither the first character nor the next are upper case, the string will be capitalyzed. A single character will toggle just between lower and upper case. */ s = document.selection.Text; if( document.selection.IsEmpty ){ document.selection.SelectWord(); s = document.selection.Text; } f = s.charCodeAt(0); n = s.charCodeAt(1); if( (f >= 0x41 && f <= 0x5a && s.length == 1) || (f >= 0x41 && f <= 0x5a && n >= 0x41 && n <= 0x5a)){ document.selection.ChangeCase(eeCaseLowerCase); } else if( f >= 0x41 && f <= 0x5a ){ document.selection.ChangeCase(eeCaseUpperCase); } else { document.selection.ChangeCase(eeCaseCapitalize); }
Regards,
Meir - AuthorPosts
- You must be logged in to reply to this topic.