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

Script to group certain objects

Explorer ,
Feb 21, 2020 Feb 21, 2020

I am using inDesign 15.0.1. I have an indd file with thousands of single pages. On each page are three layers named: Text, Images, and Background.

 

Within the Text layer are 5 non-grouped objects. Assume they are named: A through E.

 

How can I write a script that will go through all of the pages in the indd file, and group objects A, C, and E within the Text layer and give it a name of Group1?

 

Obviously, the names of all of the elements above are just examples/placeholders.  I will modify any script provided with the appropriate information.  Thank you.

TOPICS
How to , Scripting
3.9K
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 21, 2020 Feb 21, 2020

Think about what the process is manually, and write pseudocode to solve. Then it's just understanding the DOM to execute: 

 

for each page, check all text frames on X layer, if a frame matches the need, select it. Then group all selections. 

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
Explorer ,
Feb 21, 2020 Feb 21, 2020

While I appreciate the comment, I am obviously posting here for more concrete information as to how to accomplish this task ... as in a code sample as I do NOT know the underlying DOM. Thus the reason for coming to the Support forum for inDesign.

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 21, 2020 Feb 21, 2020

Check out the snippet I provided you yesterday from code you were working off of: 

 

 

for(var t = myDoc.pages[p].textFrames.length -1; t >= 0; t--){
    var tf = myDoc.pages[p].textFrames[t];
    if (!tf.itemLayer.visible) {
       tf.remove();
    }
}

 

 

What are we doing here? We're iterating through textframes on each page, and checking if a condition is met, then doing something. How can we adopt that for this purpose? Well, if we look at the textFrame object in the DOM, we can find the select method. Here is a link: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#TextFrame.html 

 

Select can take some arguments. In this case, we want selectionOptions.ADD_TO

 

So after the condition is met, we want tf.select(SelectionOptions.ADD_TO); 

 

Once we're done with our loop, then we need to access our selections and group them. Selection is an array of everything you've selected, and accessed through app.selection;

 

The next step in understanding how to manipulate and add items is to understand the collection object. Each page has a Groups collection. So, when we're done with the inner loop, for the outer page loop, we want to add our selection: var group = myDoc.pages[p].groups.add(app.selection);

 

We went over naming objects in another thread, so after you've created the group, you can give it a name: group.name = "someName"

 

Since we want to group by page, after we add the group, we want to clear the selection before moving on to the next page. We do that through myDoc.select(NothingEnum.NOTHING);

 

Take a shot at building it through this. This is all done in the interest of "leading a horse to water." Take a look through the link above; it provides a nice overview of how the DOM works. Others here have spent a long time learning, and we (generally) are passionate about helping others learn, too. 

 

A community forum like this is a good place to go if you are stuck, but if you want a full custom solution developed for you, then you should be willing to engage a developer and compensate them for their time, rather than piecing it together through small asks in this forum. Cheers.

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
Explorer ,
Feb 21, 2020 Feb 21, 2020

I don't know what you are used to, but I don't come to support forums UNLESS I have FIRST spent a considerable amount of time trying to figure things out  on my own. Usually I have found these support forums and the people participating in them both approachable and helpful. This is NOT an example of such a thread. If you feel that my challenges are beneath you, or simply not worth your time, you can choose the path of least effort and simply not participate at all. You most certainly do not have to take a condescending, dismissive tone. That is most certainly NOT helpful.

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 21, 2020 Feb 21, 2020

Wasn't trying to be condescending or dismissive. Sorry you took it that way. Trying to help you learn. I provided you all the information you need to solve this request, but someone else will probably write the code for you. I'll do as you suggest and just not engage anymore. Have a nice weekend. 

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
Explorer ,
Feb 21, 2020 Feb 21, 2020

I am going to send this to you via PM as well, just so I make sure you see it.  I want to apologize for my response early this AM.  I was in the midst of a nasty situation, and I think I misread your post as being more to it than it was.  For that, I am truly sorry.

 

I am going to take the information you provided and see what I can make of things.  I do appreciate the efforts you put forth.

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 21, 2020 Feb 21, 2020
LATEST

Appreciate the apology, and I also realized that using select is not necessary here, since groups.add can take any array of page items (though using select is what you would do manually, and if you want to learn ExtendScript, learning select is important). 

 

Instead, here is a full solution, minus the condition (try to come up with that yourself by looking at the textFrame DOM link I sent you!): 

 

 

 

var pages = app.activeDocument.pages;
for (var i = 0; i < pages.length; i++) {
   var tfs = pages[i].textFrames;
   var arrayToGroup =[];
   for (var j = 0; j < tfs.length; j++) {
       if (/*condition is met*/) {
           arrayToGroup.push(tfs[j]);
       }
   }
   var group = pages[i].groups.add(arrayToGroup);
   group.name = "someName";
}

 

 

 

arrayToGroup is an Array object. Arrays are native to Javascript. You can make a new one like I did, with the empty brackets, then use .push to add elements to it. This is essentially what I was describing above when describing how select and app.selection works, but doing it programmatically rather than "manually" with select. 

 

I do encourage you to spend some time digging in to ExtendScript, reading sample code, try to understand how the DOM works, get in the habit of writing out pseudocode. I see you are new here and new to ExtendScript. It's a lot of fun. Cheers. 

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