Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Mar 17, 2023 Mar 17, 2023

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?

 

TOPICS
Scripting
569
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 17, 2023 Mar 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;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 17, 2023 Mar 17, 2023

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

I agree with @Robert at ID-Tasker, that's what I think you have to do. Your code seems fine and is quite simple. Go for it!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

@Mr Mossa here is a modification just for fun and learning—there's nothing wrong with what you've done already.

myProps = [
    { key: 'underline', display: 'Underline', value: true },
    { key: 'strikeThru', display: 'StrikeThru', value: true },
    { key: 'position', display: 'SuperScript', value: Position.SUPERSCRIPT },
    { key: 'position', display: 'SubScript', value: Position.SUBSCRIPT },
    { key: 'capitalization', display: 'AllCaps', value: Capitalization.ALL_CAPS },
    { key: 'capitalization', display: 'SmallCaps', value: Capitalization.SMALL_CAPS },
];

var text = app.selection[0].texts[0];
for (var i = 0; i < text.textStyleRanges.length; i++) {
    var x = txtProp(text.textStyleRanges[i], myProps);
    if (x.length > 0) {
        alert(x);
    }
}

function txtProp(txt, props) {
    var result = [];
    for (var i = 0; i < props.length; i++) {
        if (txt[props[i].key] == props[i].value) {
            result.push(props[i].display)
        }
    }
    return result;
};

Only things I did differently are: separated the data from the logic, returns an array of results and displays the results in order.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 22, 2023 Mar 22, 2023
LATEST

Hi @Mr Mossa 
May be it’s too late for my suggestions but let’s go.
First of all, I think you have to discard all the text that is “normal”, text without any property. Unless that all your text have applied any of your properties. You can do this with the method "textHasOverrides()". I think you will see the difference if the text is very long.


After that you can check the properties.


No problem in your script with underline and strikeThru properties.


In my opinion you can improve the script with the capitalization and the position properties. In your script if you have a normal text underlined (like this) you check all the possibilities of capitalization and position and it is useful.


My suggestion is to check if the text is not normal (text.capitalization != Capitalization.Normal) and after that check the options, but not one after another with if statements. Use switch statements, so if it finds that your text it's all caps it will not check if it is small caps. It can't be both things at the same time.


For position you have to use the same solution, even in this case it is better because you have 6 options (OT_Denominator, OT_Numerator, OT_Subscript, OT_Superscript, subscript and superscript).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines