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

Enthusiast ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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    
 
            } 
        } 
    }
}

 

 

Best
M.Hasanain
TOPICS
Scripting

Views

163

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

correct answers 6 Correct answers

Engaged , Aug 07, 2022 Aug 07, 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 gua

...

Likes

Translate

Translate
Community Expert , Aug 07, 2022 Aug 07, 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
    }
}

 

 

Likes

Translate

Translate
Engaged , Aug 07, 2022 Aug 07, 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;
}

 

Likes

Translate

Translate
Community Expert , Aug 07, 2022 Aug 07, 2022

Good point. 

Likes

Translate

Translate
Community Expert , Aug 08, 2022 Aug 08, 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

...

Likes

Translate

Translate
Community Expert , Aug 08, 2022 Aug 08, 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 )

Likes

Translate

Translate
Engaged ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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)

Best
M.Hasanain

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
Engaged ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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
    }
}

 

 

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
Engaged ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Good point. 

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Thanks a lot @brianp311 @willcampbell7  for your valuable help, i wish a great day for you, i learned new skills today

Best
M.Hasanain

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 ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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 )

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 ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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 )

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 ,
Aug 11, 2022 Aug 11, 2022

Copy link to clipboard

Copied

LATEST

@Laubender 

This is awesome!, Thanks a lot

Best
M.Hasanain

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