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

Delete all hidden items script

Community Beginner ,
Nov 16, 2023 Nov 16, 2023
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.
TOPICS
Scripting
727
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

correct answers 1 Correct answer

Community Expert , Nov 16, 2023 Nov 16, 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(); } 
}

 

Translate
Community Expert ,
Nov 16, 2023 Nov 16, 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(); } 
}

 

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 Beginner ,
Nov 17, 2023 Nov 17, 2023
LATEST

Worked like a charm!

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

Thanks a ton!

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