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

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
Mohammad Hasanin
TOPICS
Scripting

Views

425

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

Enthusiast , 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

...

Votes

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

 

 

Votes

Translate

Translate
Enthusiast , 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;
}

 

Votes

Translate

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

Good point. 

Votes

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

...

Votes

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 )

Votes

Translate

Translate
Enthusiast ,
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

Votes

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
Mohammad Hasanin

Votes

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

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

Votes

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

 

 

Votes

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

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

Votes

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. 

Votes

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
Mohammad Hasanin

Votes

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 )

Votes

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 )

Votes

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
Mohammad Hasanin

Votes

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