Skip to main content
Evan vR
Participant
November 16, 2023
Answered

Delete all hidden items script

  • November 16, 2023
  • 1 reply
  • 763 views
I'm working on a script to clean up a doc after a data merge. Essentially it'll set the visibility of a given item based on a text field value. Example below:
var QEBlue = app.activeDocument.pageItems.itemByName('QE 287');
var QEGreen = app.activeDocument.pageItems.itemByName('QE 347');
etc......
 
if (Panto.contents==('QE BLUE')){
QEBlue.visible = true;
QEGreen.visible = false;
QEBurg.visible = false;
QERed.visible = false;
QEBrown.visible = false;
QES57.visible = false;
QEReflex.visible = false;
QEBuff.visible = false;
}
else if (Panto.contents==('QE GREEN')){
QEBlue.visible = false;
QEGreen.visible = true;
QEBurg.visible = false;
QERed.visible = false;
QEBrown.visible = false;
QES57.visible = false;
QEReflex.visible = false;
QEBuff.visible = false;
 
Right now this works however I do end up with a lot of extra objects that could frankly get removed.
Option A was changing the script to run like:
if (Panto.contents==('QE BLUE')){
QEBlue.visible = true;
QEGreen.remove();
etc.....
But this doesn't really save a lot of time in writing the script, plus it creates a lot of steps for it to run through.
Option B would be something along the lines of:
if (app.activeDocument.pageItems.everyItem().visible = false){
app.activeDocument.pageItems.everyItem().remove();
}
But instead of removing everything it just hides everything.
 
In an ideal world, the script would read
if (Panto.contents==('QE BLUE')){
QEBlue.visible = true;
"everything else".remove();
 
Right now the doc has 3 layers and each layer has corresponding items, so just hiding & deleting layers isn't really a solution.
So in the end I've got a document with only the visible elements. If there's a way to write this efficiently it would be greatly appreciated, otherwise, I can brute force it by telling the script to change every item one by one.
This topic has been closed for replies.
Correct answer brian_p_dts

A short fuction that removes all invisibile items that you can run after your main block runs. You do have to iterate and check visibility: 

var pis = app.activeDocuments.allPageItems;
var i = pis.length;
while(i--) {
   if (!pis[i].visible) { pis[i].remove(); } 
}

 

1 reply

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
November 17, 2023

A short fuction that removes all invisibile items that you can run after your main block runs. You do have to iterate and check visibility: 

var pis = app.activeDocuments.allPageItems;
var i = pis.length;
while(i--) {
   if (!pis[i].visible) { pis[i].remove(); } 
}

 

Evan vR
Evan vRAuthor
Participant
November 17, 2023

Worked like a charm!

Although it should be activeDocument (no s), but after that fix it's fine.

Thanks a ton!