[JS] appliedLanguage list Story

Enthusiast ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

Hello everyone,

Is it possible to make a list of languages applied on paragraphs and locally in an article without having to test all the paragraphs one after the other?

thank you very much

Philip

 

Bonjour à tous,

Est-il possible de faire un liste des langues appliqué sur des paragraphe et localment dans un article sans devoir teste tous les paragraphes l'un après l’autre?

Merci beaucoup

Philippe

 
 
 
 
 
TOPICS
Scripting

Views

210

Likes

Translate

Translate

Report

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 ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

Hi Philippe,

language is a property of characters. So you have to test characters or character ranges.

 

The "tool" for this are textStyleRanges. See for:

story.textStyleRanges

 

The definition of TextStyleRange is:
"A continuous range of identical text formatting attributes. Base Class: Text"

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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
Enthusiast ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

Hello Laubender and thank you for your contribution.

here is global information.

I found info on the forum, which I modified to get the info:

 

Bonjour Laubender et merci pour ta contribution.

ici, c'est une information global.

j'ai trouvé des infos sur le forum, que j'ai modifié pour avoir l'info:

 

 

myDoc=app.documents[0];
var langs=myDoc.languages.everyItem().getElements();
s.writeln(langs.length);

for (var x = 0; x < langs.length; x++) {
    //var p =  langs[x].properties; for (var i in p) $.writeln(i +  "\t"+p[i]); 
    $.writeln(langs[x].name);
 }

 

 

Likes

Translate

Translate

Report

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
Enthusiast ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

j'aurai bien voulus sur l'article mais il fait déjà ça.

 

 

Likes

Translate

Translate

Report

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 ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

Ah. Thanks for that. I misread your initial post.

Thought you'd like to know which text in a document is using what language.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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 ,
Feb 04, 2022 Feb 04, 2022

Copy link to clipboard

Copied

Hi Philippe,

I have to come back to your code:

var myDoc=app.documents[0];
var langs=myDoc.languages.everyItem().getElements();
$.writeln(langs.length);

When I run this code on any document opened with InDesign 2022 I get 60 languages stored in that array. Even if the document contains zero text at all.

 

So what's the version of InDesign you are running this code at?

What operating system?

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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
Enthusiast ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Hi Laubender,

apologies for the delay with the response. I am with a 17.1 vesrion on Mac. Indeed if the document has no text, it returns 60 languages, with text it behaves differently (???)

 

milles excuses pour le retard avec la réponse. Je suis avec un vesrion 17.1 sur Mac. Effectivement si le document n'a pas de texte, il restourne 60 langues, avec du textes il se comporte différament (???)

 

 

 

info_lang();
// .everyItem().getElements()


//
function info_lang(){
    var info_lang_Doc = app.documents[0];// voir ici à mettre une variable
    var info_txt = 'Il y a ';
    var langs = info_lang_Doc.languages.everyItem().getElements();
    // pour info 
    for (var x = 0; x < langs.length; x++) {
        //var p =  langs[x].properties; for (var i in p) $.writeln(i +  "\t"+p[i]); 
        $.writeln(langs[x].name);
    }
    
    if ( langs.length > 1) {
        info_txt +=  langs.length + " langues dans le doc. (";
        for (var x = 0; x < langs.length && x  < 4; x++) {
            //var p =  langs[x].properties; for (var i in p) $.writeln(i +  "\t"+p[i]); 
            //$.writeln(langs[x].name);
            info_txt += (x < 3 ? langs[x].name + ", " : langs[x].name +"…)."); 
        }
    } else {
        info_txt += langs.length + " langue dans le doc. (" + langs[0].name + ")";
    }
    alert(info_txt);
}

 

 

Likes

Translate

Translate

Report

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
Enthusiast ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Effectivement, le comportement n'est pas juste  ...Capture d’écran 2022-03-01 à 09.38.52.png

 

Likes

Translate

Translate

Report

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 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

LATEST

Hi @Liphou , If you look at the InDesign API, a document has a .languages property—a collection of languages—but doesn’t have the .appliedLanguage properties that a text object has—e.g. character, word, line, etc. The .appliedLanguage is a character level property, so to get the language of text you have to use the .appliedLanguage property.

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html

 

Character has .appliedLanguage, but not .languages:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html

 

It’s similar to .paragraphStyles—a document has a collection of .paragraphStyles, but doesn’t have an .appliedParagraphStyle. A text object has an .appliedParagraphStyle property, but no .paragraphStyle property

Likes

Translate

Translate

Report

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 ,
Feb 04, 2022 Feb 04, 2022

Copy link to clipboard

Copied

Hi Philippe, as Uwe suggests language is a character property—if I try to get the appliedLanguage of a paragraph JS will return the language of the paragraph’s first character, even when the paragraph contains multiple languages.

 

This script will get all the languages in a selection of text as an array:

var s = app.documents[0].selection[0];
var c = s.characters
var a = []

for (var i = 0; i < c.length; i++){
    if (!checkItem(a, c[i].appliedLanguage.name)) {
        a.push(c[i].appliedLanguage.name)
    } 
};   

alert("The selected text includes these languages: " + a)

function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

 

 

Screen Shot 59.png

Likes

Translate

Translate

Report

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 ,
Feb 04, 2022 Feb 04, 2022

Copy link to clipboard

Copied

Hi Rob,

in a case like that I would gather all the textStyleRanges and check for property appliedLanguage.

Could be that parts of the text are formatted with different language attributes.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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 ,
Feb 04, 2022 Feb 04, 2022

Copy link to clipboard

Copied

Hi Uwe, I thought that was going to return a single language, but textStyleRanges returns the same array as characters. Thanks.

Likes

Translate

Translate

Report

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 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

"Indeed if the document has no text, it returns 60 languages, with text it behaves differently (???)"

 

Even if the document contains text, e.g, one text frame where two different languages are applied, I get 60 results with:

var myDoc=app.documents[0];
var langs=myDoc.languages.everyItem().getElements();
$.writeln(langs.length);

 German InDesign 2022 version 17.1 on Windows 10.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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
Enthusiast ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

If we are in "InsertionPoint", it gives 3 in my case, but there too the answer is not right...

 

Si nous somme en "InsertionPoint", il donne 3 dans mon cas, mais là aussi la réponce n'est pas juste  ...

 

Likes

Translate

Translate

Report

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