Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19825
    Meir
    Participant

    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?

    #19826
    Meir
    Participant

    Sorry, got it!
    One has to also load the selection into a variable, so I added s = 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 to s.toLowerCase?
    And alternatively, how can document.selection.ChangeCase(eeCaseCapitalize); be used?
    Regards
    Meir

    #19827
    Stefan
    Participant

    if(document.selection.IsEmpty){document.selection.SelectWord()}
    document.selection.ChangeCase(eeCaseCapitalize); //Title case

     

    #19829
    Meir
    Participant

    Thank 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

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.