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

Script - Select groups as anchored objects

Community Beginner ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Hi all! 


Through searching the community posts i couldn't find a solution to my situation. This was most close to it: Link.

 

Situation:

Situation.jpg

Grouped objects (containing of an image and a textframe with number) as anchored objects in textframe. When all anchored groups each contain the placeholder image (placeholder.ai); all groups should be removed.

If one of these anchored groups has a different image; do nothing.

On a sidenote:

  • These anchored groups have a dedicated objectstyle (always with objectstyle "II-Step") and they should be targeted by this objectstyle.  
  • There are multiple pages in the document where such a textframe (always with objectstyle "Main") with these anchored groups can be found.

 

Code as of now:
The code is not yet finished but did the trick to gather groups (with objectstyle II-Step) and the image (placeholder.ai) into an array. To be deleted later on... But it doesn't work with anchored groups unfortunatly.

 

Please note i'm still learning Javascript.

 

//Declare variables & functions
var ImageCount;
var i = 0;
var j = 0;
var k = 0;
var PlaceholderAmountOnPage = 0;
const FoundGroups = [];
var document = app.activeDocument;
var PageCount = document.pages.length;

//Iterate through all pages
for (i=0;i<PageCount;i++) {
    FoundGroups.length = 0;

    //Collect all groups on page
    var FoundAllGroups = document.pages[i].groups;

    //Iterate all collected groups
    for (h=FoundAllGroups.length;h>0;h--) {
        
        //Check if group has II-Step objectstyle applied
        if (FoundAllGroups[h-1].appliedObjectStyle.name == "II-Step") {

            //Push results into new array
            FoundGroups.push(FoundAllGroups[h-1]);
        }
    }   
    
    var GroupCount = FoundGroups.length;

        //Iterate through II-step groups
        for (j=GroupCount;j>0;j--) {
            //Collect images in group
            var ImagesInGroup = FoundGroups[j-1].allGraphics;
            var ImagesInGroupCount = ImagesInGroup.length;
            
            //Iterate through every image in group
            for(k=ImagesInGroupCount;k>0;k--) {
                
                //Check if image is a placeholder image
                if (ImagesInGroup[k-1].itemLink.name == "InstallationInstructions_Placeholder.ai") {
                    
                    PlaceholderAmountOnPage = PlaceholderAmountOnPage + 1;
                    alert(PlaceholderAmountOnPage);
                }                                    
            }
       }
}

Thanks for any help!

TOPICS
How to , Scripting , SDK

Views

275

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 1 Correct answer

Community Expert , Apr 14, 2022 Apr 14, 2022

Hi @jonathand33458459 , I think you can get an array of the document groups, including anchored groups, using .allpageItems, something like this:

 

var doc = app.activeDocument
var api = doc.allPageItems;
var ga = [];

for (var i = 0; i < api.length; i++){
    if (api[i].constructor.name == "Group") {
        ga.push(api[i]);
    } 
};   

alert("There are " + ga.length + " groups in the document")

 

Screen Shot 35.png

Votes

Translate

Translate
Community Expert ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Hi @jonathand33458459 , I think you can get an array of the document groups, including anchored groups, using .allpageItems, something like this:

 

var doc = app.activeDocument
var api = doc.allPageItems;
var ga = [];

for (var i = 0; i < api.length; i++){
    if (api[i].constructor.name == "Group") {
        ga.push(api[i]);
    } 
};   

alert("There are " + ga.length + " groups in the document")

 

Screen Shot 35.png

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Well, anchored objects are part of the story where are they anchored.

So you will get an array of anchored groups like that:

var anchoredGroupsArray =
app.documents[0].stories.everyItem().groups.everyItem().getElements();

You should be able to loop that array and detect the ones that are relevant, the ones where object style "II-Step" is applied to. If you have detected such a group see into the allGraphics array of the group, loop that array, if your AI file is there by checking the itemLink of every item of that array. If found remove the group.

 

Regards,
Uwe Laubender

( ACP )

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 Beginner ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Thanks @rob day & @Laubender  for your input on this!

I went with Rob's approach and managed to get it working.

If anyone stumbles on this post; below is the result but can sure be improved upon.

//Declare variables & functions
var document = app.activeDocument;
var ImageCount;
var i = 0;
var j = 0;
var k = 0;
var l = 0;
var m = 0;
const IIStepGroups = [];
var PageCount = document.pages.length;

//Iterate through pages
for (i=0;i<PageCount;i++){
    //Reset counters for new page
    IIStepGroupsCount = 0;
    ItemsPerPageCount = 0;
    PlaceholderAmountOnPage = 0;
    ImagesInGroupCount = 0;
    IIStepGroups.length = 0;
    
    //Collect all items on page
    var ItemsPerPage = document.pages[i].allPageItems;
    //Amount of items collected on page
    ItemsPerPageCount = ItemsPerPage.length;

    //Iterate through items
    for (j = 0;j<ItemsPerPageCount;j++){
        //Check if item is group and has objectstyle II-Step applied
        if(ItemsPerPage[j].constructor.name == "Group" && ItemsPerPage[j].appliedObjectStyle.name == "II-Step") {
            //Push result in array
            IIStepGroups.push(ItemsPerPage[j]);
        }
    }
    
    IIStepGroupsCount = IIStepGroups.length;

    //Iterate through array with II-Step groups
    for (k=0;k<IIStepGroupsCount;k++) {

        //Collect images of group
        var ImagesInGroup = IIStepGroups[k].allGraphics;
        ImagesInGroupCount = ImagesInGroup.length;

        //Iterate through collected images
        for (l=0;l<ImagesInGroupCount;l++) {
            //Check if image is placeholder
            if (ImagesInGroup[l].itemLink.name == "InstallationInstructions_Placeholder.ai") {
                //Keep count of how many placeholder images
                PlaceholderAmountOnPage = PlaceholderAmountOnPage + 1;
            }
        }
    }
    
    //Check if all IIStepGroups contain placeholder image
    if (PlaceholderAmountOnPage == IIStepGroupsCount) {
        //Iterate through all IIStepGroups to delete them
        for (m=IIStepGroupsCount;m>0;m--) {
            IIStepGroups[m-1].remove();
        }   
    }
}

 

Again, thanks for helping out!

 

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

 

below is the result but can sure be improved upon

 

Hi @jonathand33458459 , do you really need to go page by page?

 

The .allPageitems property can also be a document property, so you might be able to skip some of your loops. Can’t test this, but does it work?

 

 

var doc = app.activeDocument
var api = doc.allPageItems;
var ga = [];
var ImagesInGroup;

//get all of the groups in the document with the object style "II-Step" applied as an array
for (var i = 0; i < api.length; i++){
    if(api[i].constructor.name == "Group" && api[i].appliedObjectStyle.name == "II-Step") {
        ga.push(api[i]);
    } 
}

//loop through the ga array
for (var j = 0; j < ga.length; j++){
    //loop through each group's graphics
    ImagesInGroup = ga[j].allGraphics;
    for (var k = 0; k < ImagesInGroup.length; k++){
        //if link name is InstallationInstructions_Placeholder.ai, delete the group
        if (ImagesInGroup[k].itemLink.name == "InstallationInstructions_Placeholder.ai") {
            ga[j].remove();
        }
    } 
};  

 

 

 

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 Beginner ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

LATEST

Good point 🙂 But it's supposed to work like this.
This 'Placeholder'-thing has to be evaluated per page.

 

This script will be part of an automated document creation workflow, that's why some things might look excessive.

 

 

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