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

[JS] select all bloc

Enthusiast ,
Feb 15, 2021 Feb 15, 2021

Bonsoir à tous,

 

Après la recheche d'une page type avec ce code  (voir code), je veux selectioné tous les blocs, et en fonction du la label, je veux manipulé le texte mais je contact que je manipule du "String", donc impossible de tester si j'ai par exemple de l'italique, ou même des notes.

Dans les blocs, je dois aussi retouvé un bloc image.

J'ai utilisé "pageItems", ce n'est pas la bonne soultion, je ne parviens pas à revenir au textFrame.

 

Est-il possible d'avoir un éclerage, sur la bonne pratique? Merci

   

 

 

var leDoc = app.activeDocument; 
var lesPages = leDoc.pages;

for (var y=0; y < lesPages.length; y++) { // For // 01
    if (lesPages[y].appliedMaster.name == 'Ed01-Edito01') 
        {
            /
            for (var i = 0; i < lesPages[y].pageItems.length; i++ ) { // FOR //02
               
                if (lesPages[0].pageItems[i].label == 'bloc_titre_contribution') 
                    {
                        
                       var leNewTexte = nettageText(lesPages[0].pageItems[i].contents);
                    }    

            
            } // fin du FOR // 02
}
    else { $.writeln ('pas ou plus de maquette Ed01-Edito01'); }    

}// fin du FOR // 01

}

function nettageText(leTexte) {

    var leTextLocal =  leTexte.replace(/ ?\n ?/g, " ").replace(/\xAD/g, ""); // ~-
    
    return (leTextLocal);

} // fin de la function nettageText()

 

 

TOPICS
Import and export , Print , Scripting
471
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 ,
Feb 15, 2021 Feb 15, 2021

Hi, Sorry if I’m not getting a good translation of your question, but pageItems wouldn’t necessarily get all of the page’s text frames. Use allPageItems if you need to get text frames in groups or tables. Here you can see the count of page items is 2 (a rectangle and a group), but the count of allPageItems is 6—the rectangle, the group, and the 4 text frames inside of the group

 

 

Screen Shot 12.png

This would get only the page’s text frames:

 

 

var d = app.documents.item(0);
var api = d.allPageItems;

for (var i = 0; i < api.length; i++){
    if (api[i].constructor.name == "TextFrame") {
        $.writeln(api[i].constructor.name)
    } 
};   

 

 

 

 

 

 

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 ,
Feb 15, 2021 Feb 15, 2021

If I understand, ".contents" does not allow you to test whether the text is italic, or contains a note. You'd have to write specific code to test for each instance. For example, 

var pi = lesPages[0].pageItems[i];
if (pi.notes.length > 0) { 
//there is at least one note; do something with them 
//act on them with, say, pi.notes.everyItem();
}

Same with italics,  if it's a text frame, and you want to see if any text contains italics, you basically have to examine each character for the italic, whether as a character style or font family name. You could get an array of all character styles applied to characters in a text frame with:

var pi = lesPages[0].pageItems[i];
var allCStyleNames = pi.characters.everyItem().appliedCharacterStyle;
for (var c = 0; c < allCStyles.length; c++) {
   if (allCStyles[c].name == "Italics") { //there are italics }
}

 This of course is on top of the advice Rob also gave to test allPageItems and to test whether it's a TextFrame. Hope that helps. 

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 ,
Feb 16, 2021 Feb 16, 2021

Collect the text frames as Rob indicated.

 

Then, after your line

var leNewTexte = nettageText(lesPages[0].pageItems[i].contents);

you don't do anything with the cleaned text. I guess you want to replace the content of each frame with the cleaned content -- if that's so, do this:

lesPages[0].pageItems[i].parentStory.contents = leNewTexte;

but you must realise that you replace possibly formatted text with an unformatted string, so you lose all character styles, index markers, etc.

 

If you don't want to lose all that formatting you need to do replacements on the frame's parent story, not its content:

app.findGrepPreferences.findWhat = ' ?\\n ?/';
app.changeGrepPreferences = ' ';
lesPages[0].pageItems[i].parentStory.changeGrep();

P.

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 ,
Feb 16, 2021 Feb 16, 2021

@brian_p_dts -- You can in fact check whether a text contains any italic text in a single statement:

if (myTextFrame.parentStory.characters.everyItem().fontStyle.join('').indexOf('Italic') >= 0) {
  // Italic text present
}

This works only if 'Italic' is part of the italic font style names, which is not necessarily always the case, but the line can be easily adapted to such a situation.

 

P.

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 ,
Feb 16, 2021 Feb 16, 2021
LATEST

I thought there should be a way, just didn't have enough coffee to think about it. Clever trick. Thanks, Peter!

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
Enthusiast ,
Feb 16, 2021 Feb 16, 2021

Merci Rob Day, Brianp311 et Peter,

J'ai vu mon erreur, je manipule texte et non l'objet TexteFrame (merci Peter de me l'avoir confimé, Super).

Au final je dois balise les style de caractaire (ex: italic) et replacer les notes dans le texte.

Merci à vous je ferai suivre le script corrigé

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
Enthusiast ,
Feb 16, 2021 Feb 16, 2021

voici un résumé de des proposition

var d = app.activeDocument;
//var api = d.pages[0].pageItems;
var api = d.pages[0].allPageItems;
//
//var allCStyles = styleDoc();


for (var i = 0; i < api.length; i++){
  // $.writeln("N° : "+ api.length + " -  api[i].constructor.name : " + api[i].constructor.name)
    if (api[i].constructor.name == "TextFrame") {
        //$.writeln(api[i].constructor.name);
        //$.writeln(api[i].label);
        if (api[i].label == 'bloc_titre_contribution'){
            // Solutution Peter
            if (api[i].parentStory.characters.everyItem().fontStyle.join('').indexOf('Italic') >= 0) {
                $.writeln('oui Italic');

            }
            // Solution Brianp311
            var allCStyleNames = api[i].parentStory.characters.everyItem().appliedCharacterStyle;
            for (var c = 0; c < allCStyleNames.length; c++) {
               if (allCStyleNames[c].name == "italique") { 
                $.writeln('oui sur le style');
               }
            }
            
            if (api[i].footnotes.length > 0) { 
                //there is at least one note; do something with them 
                //act on them with, say, pi.notes.everyItem();
                    $.writeln('Oui note');
                } else {
                    $.writeln('Non pase de note');
            }
        
        }
       

    } else if (api[i].constructor.name == "Image") {
          // API[i] est l'image qui est dans le bloc ... PAS le bloc qui cintien l'image  
        $.writeln('Parent : '+ api[i].parent.label); 
        $.writeln('Parent : '+ api[i].parent.constructor.name); 
        $.writeln('Parent : '+ api[i].parent.graphics[0].itemLink.name);
        $.writeln(api[i].itemLink.name);
    }

};

function styleDoc() {
	 
    var variableLocal = "Par defaut\n" + app.activeDocument.characterStyles.everyItem().name.join("\n");
    return variableLocal.split("\n");
    
}// fin de  la function
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