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:
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:
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!
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")
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")
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 )
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!
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();
}
}
};
Copy link to clipboard
Copied
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.