Skip to main content
Fightergator
Inspiring
June 26, 2023
Answered

Check if TextRange is Start of Paragraph (<$chapnum> variable)

  • June 26, 2023
  • 1 reply
  • 265 views

I'm working on a function to clone the case of replacement text (change).  I can make many of the decisions on how to clone the replacement text (e.g., all caps, initial caps each word, or initial caps first word, etc.) based on the pgf[style].name.  However, the body text in our documents (chpara0, chsubpara1, etc.) start with a title followed by a period, then more paragraph text.  The first sentence (pgf title) is identified by a variable that includes <$chapnum> and the Paragraph Designer identifies the Position as "Start of Paragraph."  If my found text occurs in this section of the paragraph, I need to clone the change text so each word is an initial capital letter (i.e., title caps).   

 

I've looked over a number of posts to this forum, but haven't run across anything that indicates how to identify this text from within a script. 

    This topic has been closed for replies.
    Correct answer frameexpert

    Here are a couple of namespaced functions, where "string" is the change text. Hopefully, you can figure out what is going on. If not, I can try to answer specific questions about the code.

     

    CP_FCB.getCloneCaseString = function (textRange, string, doc) {
        
        // textRange is the text range of the found text.
        // string is the change text.
    
        var firstLetter;
        
        // Get the first letter of the found text.
        firstLetter = CP_FCB.getText (textRange, doc)[0];
        // If the first letter is upper case, change the first
        // character of the change text to upper case.
        if (firstLetter === firstLetter.toUpperCase ()) {
            return string.charAt (0).toUpperCase () + string.slice (1);
        }
        else { // Otherwise, change it to lower case.
            return string.charAt (0).toLowerCase () + string.slice (1);
        }
    };
    
    CP_FCB.getText = function  (textObj, doc) {
        // Gets the text from the text object. or text range.
    
        var text = "", textItems, i;
        
        // Get a list of the strings in the text object or text range.
        if (textObj.constructor.name !== "TextRange") {
            textItems = textObj.GetText (Constants.FTI_String);
        } 
        else {
             textItems = doc.GetTextForRange (textObj, Constants.FTI_String);
        }
        // Concatenate the strings.
        for (i = 0; i < textItems.len; i += 1) {
            text += (textItems[i].sdata);
        }
    
        // Strip any leading or trailing spaces and return the text.
        return text.replace (/(^\s+|\s+$)/g, "");
    };
    

    1 reply

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    June 27, 2023

    Here are a couple of namespaced functions, where "string" is the change text. Hopefully, you can figure out what is going on. If not, I can try to answer specific questions about the code.

     

    CP_FCB.getCloneCaseString = function (textRange, string, doc) {
        
        // textRange is the text range of the found text.
        // string is the change text.
    
        var firstLetter;
        
        // Get the first letter of the found text.
        firstLetter = CP_FCB.getText (textRange, doc)[0];
        // If the first letter is upper case, change the first
        // character of the change text to upper case.
        if (firstLetter === firstLetter.toUpperCase ()) {
            return string.charAt (0).toUpperCase () + string.slice (1);
        }
        else { // Otherwise, change it to lower case.
            return string.charAt (0).toLowerCase () + string.slice (1);
        }
    };
    
    CP_FCB.getText = function  (textObj, doc) {
        // Gets the text from the text object. or text range.
    
        var text = "", textItems, i;
        
        // Get a list of the strings in the text object or text range.
        if (textObj.constructor.name !== "TextRange") {
            textItems = textObj.GetText (Constants.FTI_String);
        } 
        else {
             textItems = doc.GetTextForRange (textObj, Constants.FTI_String);
        }
        // Concatenate the strings.
        for (i = 0; i < textItems.len; i += 1) {
            text += (textItems[i].sdata);
        }
    
        // Strip any leading or trailing spaces and return the text.
        return text.replace (/(^\s+|\s+$)/g, "");
    };
    
    Fightergator
    Inspiring
    June 27, 2023

    Thanks Rick...I have both functions thanks to previous postings of yours, less the regex to strip the leading and trailing spaces, which I can use.  I've made one small change to your CloneCaseString function (see below), that tests whether the entire "find" is all uppercase and returns an all uppercase "change" word.  If not all uppercase, then changes the case to match the first letter of the "find" word. 

     

    function getCloneCaseString (find, change) {
        var firstLetter;
        firstLetter = find[0];
     
         if(find === find.toUpperCase()) { // If "find" all uppercase
            return change.toUpperCase(); // Convert "change" to all uppercase
            } else {
                if (firstLetter === firstLetter.toUpperCase ()) {
                    return change.charAt (0).toUpperCase () + change.slice (1);
                  } else {
                    return change.charAt (0).toLowerCase () + change.slice (1);
                  }
           }
    }

     

    However, in some cases I'm searching for a single compound word; e.g., datalink, that should actually be two words, i.e., data link, according to our technical writing standards.   If found within a normal sentence or as the first word in a sentence, this script works fine.  If found in a title, only the first word will be capitalized, when both change words need to be capitalized.  By using the pgf.Name I can sort out most of the instances where the term is found in a title, with the exception of our primary chpara0 and chsubpara1.  In digging into this deeper, I'm of the conclusion there is no programmatic way to do this...and that's why we have editors accepting or rejecting the changes.  Thanks much for your time and suggestions.