Skip to main content
M.Hasanin
Inspiring
August 7, 2022
Answered

Apply Setting to TextFrames and Tables inside any Group of Page Items in Document

  • August 7, 2022
  • 3 replies
  • 1190 views

Hi Experts, 

Im Trying to Apply Setting to TextFrames and Tables (Language - Composer - Paragraph Direction - Story Direction) using InDesign ME inside any Group in the Document, the script not working and also no errors!, please help to identify the logic mistakes! ,i apperciate any help and thanks in advance, here is the script

 

var myDoc = app.activeDocument;
var pItems = myDoc.pages.everyItem().pageItems.everyItem().getElements();
var GroupsArray = [];

for(var j = 0; j < pItems.length; j++){ 
    if (pItems.constructor.name == "Group") {
         GroupsArray = GroupsArray.concat(pItems);
         
        for(var i = 0; i < GroupsArray.length; i++){   
             if(GroupsArray.constructor.name == "TextFrame" && GroupsArray.tables.length){
                //Apply Settings
                //
                GroupsArray.texts[i].appliedLanguage= "Arabic";
                GroupsArray.texts[i].composer = "Adobe World-Ready Paragraph Composer";
                GroupsArray.texts[i].paragraphDirection = 1379028068; //RTL
                GroupsArray.texts[i].storyPreferences.storyDirection = 1379028068; //RTL    
 
            } 
        } 
    }
}

 

 

This topic has been closed for replies.
Correct answer Laubender

Also note that tables do have the property tableDirection that could be set to TableDirectionOptions.RIGHT_TO_LEFT_DIRECTION.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

3 replies

Community Expert
August 8, 2022

Hi together,

why not accessing all stories of the document in one go?

 

app.documents[0].stories.everyItem().properties =
{
	appliedLanguage : "$ID/Arabic" ,
	composer : "$ID/HL Composer Optyca" ,
	paragraphDirection : ParagraphDirectionOptions.RIGHT_TO_LEFT_DIRECTION ,
	storyPreferences : { storyDirection : StoryDirectionOptions.RIGHT_TO_LEFT_DIRECTION }
};

 

 

Ok. That said, the above code will leave out:

 

[1] Table text cells

[2] Footnote texts

[3] Text in Notes objects

 

To get table cells one could access the tables via the story objects.

( But this will leave out tables inside table cells. )

 

 

app.documents[0].stories.everyItem().tables.everyItem().cells.everyItem().texts.everyItem().properties =
{
	appliedLanguage : "$ID/Arabic" ,
	composer : "$ID/HL Composer Optyca" ,
	paragraphDirection : ParagraphDirectionOptions.RIGHT_TO_LEFT_DIRECTION
};

 

This should work if at least one table in the document contains a single text cell.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

LaubenderCommunity ExpertCorrect answer
Community Expert
August 8, 2022

Also note that tables do have the property tableDirection that could be set to TableDirectionOptions.RIGHT_TO_LEFT_DIRECTION.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

M.Hasanin
M.HasaninAuthor
Inspiring
August 11, 2022

@Laubender 

This is awesome!, Thanks a lot

BestMohammad Hasanin
brian_p_dts
Community Expert
Community Expert
August 7, 2022

Just use the Document.allPageItems collection array, which gets page items in groups (and anchored objects too, which your current attempt misses):

 

var apis = app.activeDocument.allPageItems;
var i = apis.length;
while (i--) }
    if apis[i] instanceof TextFrame {
        //Do your thing
    }
}

 

 

willcampbell7
Legend
August 7, 2022

Yes that works too but just be aware it's an Array not a Collection. Probably not an issue in this case, so a good choice. But if needing any of the Collection features (everyItem(), itemByName(), lastItem(), etc., that sort of thing) these are absent from Array. Just something to keep in mind.

 

William Campbell
brian_p_dts
Community Expert
Community Expert
August 7, 2022

Good point. 

willcampbell7
Legend
August 7, 2022

A few things are odd but I'll take my best guess at what might work. First odd thing is getting pages, everyItem, then pageItems, everyItem, then elements. This is just not needed. Simply get doc.pageItems. But then you are correct that some of those are groups, inside of which are other pageItems, and that will require a recursive function. How your code deals with that isn't clear (to me anyway). Here is how I would approach the problem... but I don't have your document to test, so I can't guarantee this will work. It should, in theory anyway, which this code is just theortical out of my head at the moment....

var doc = app.activeDocument;
processItems(doc.pageItems);

function processItems(items) {
    var i;
    var item;
    var text;
    for (i = 0; i < items.length; i++) {
        item = items[i];
        if (item.getElements()[0].constructor.name == "Group") {
            // Recurse (call self)
            processItems(item.pageItems);
        } else {
            // Not a group
            if (item.getElements()[0].constructor.name == "TextFrame") {
                text = item.texts[0];
                //Apply Settings
                text.appliedLanguage = "Arabic";
                text.composer = "Adobe World-Ready Paragraph Composer";
                text.paragraphDirection = 1379028068; //RTL
                text.storyPreferences.storyDirection = 137902806;
            }
        }
    }
}

 

William Campbell
M.Hasanin
M.HasaninAuthor
Inspiring
August 7, 2022

Thanks a lot @willcampbell7 , it works great execpt the last line! :

text.storyPreferences.storyDirection = 137902806;

it says (object doesnt support property or method Story Prefrences)

BestMohammad Hasanin
willcampbell7
Legend
August 7, 2022

It's because 'storyPreferences' is not a property of text objects. It's a property of story objects. So remove that from the current code. Then you'll need to deal with that property separately with a loop over all of the stories in the document, like so...

var doc = app.activeDocument;
for (var i = 0; i < doc.stories.length; i++) {
    doc.stories[i].storyPreferences.storyDirection = StoryDirectionOptions.RIGHT_TO_LEFT_DIRECTION;
}

 

William Campbell