Skip to main content
Mr Mossa
Participating Frequently
March 17, 2023
Question

Check if text is underline and/or strikethru and/or superscript.... and so on

  • March 17, 2023
  • 1 reply
  • 604 views

Hi,

I'm trying to simplify a function that checks what of the below properties a string has:

Allcaps, Smallcaps, Superscrtipt, Subscript, Underline, Strikethrough

 

Some of the above properties could be in different combinations. For example, a text could be underline, strikethrough and allcaps at the same time.

Some can not be in combinations, like allcaps and smallcaps OR superscript and subscript.

 

What is the best approch to getting all properties that a text has?

I tried with:

1. searching for all text that is allcaps, and checking if the findings has any else of the above properties.

then

2. searching for all text that is smallcaps, and checking if the findings has any else of the above properties.

then

3. searching for all text that is superscript, and checking if the findings has any else of the above properties....

...and so on.

It kind of works, but seems like a bad solution.

It easily becomes complex. If I search for allcaps and then check for superscript and have a result, I will find the same result when starting to search for superscript (like in example 3.) 

 

I also tried looping over textstyleranges and checking what properties the text has. It might work, but also seems too complicated...

 

My question is. Could anybody think of a more clever solution?

 

This topic has been closed for replies.

1 reply

Mr Mossa
Mr MossaAuthor
Participating Frequently
March 17, 2023

Maybe this is the easiest solution?

 

for (var i = app.selection[0].texts[0].textStyleRanges.length - 1; i >= 0; i--) {
    var x = txtProp(app.selection[0].texts[0].textStyleRanges[i]);
    if (x.length > 0){
        alert(x);
    }
}

function txtProp(txt){
    var props = "";
    if (txt.underline == true){ props += "Underline"; }
    if (txt.strikeThru == true){ props += "strikeThru"; }
    if (txt.position == Position.SUPERSCRIPT){ props += "SuperScript"; }
    if (txt.position == Position.SUBSCRIPT){ props += "SubScript"; }
    if (txt.capitalization == Capitalization.ALL_CAPS){ props += "AllCaps"; }
    if (txt.capitalization == Capitalization.SMALL_CAPS){ props += "SmallCaps"; }
    return props;
}

 

Robert at ID-Tasker
Legend
March 17, 2023

There is no other way than iterating through all the properties.