Copy link to clipboard
Copied
Hi,
I am looking for an Indesign .jsx script that...
1) Finds all picture boxes in the active document with a certain Object Style applied to them.
2) Then finds all of the above that are empty (ie have no image inside), then deletes them.
Something like this (my apologies for the rubbish attempt at Javascript, I don't know it!)...
// create a variable for finding all objects with the Object Style called "Delete If Empty"
var MyObjectStyle = app.findObjectPreferences.appliedObjectStyles = "Delete If Empty";
// write an if statement so if the frames are empty, delete them
if (MyObjectStyle.GraphicFrame.length < 0;) {
MyObjectStyle.GraphicFrame.delete;
}
Any help would be most appreciated,
Thank you 🙂
Think this should do it.
app.findObjectPreferences = null;
app.findObjectPreferences.appliedObjectStyles = "Delete If Empty";
var finds = app.activeDocument.findObject();
var i = finds.length;
while(i--) {
if (!finds[i].graphics.length) { finds[i].remove(); }
}
Another way - probably not as effective / speedy as @brian_p_dts's solution:
main();
function main(){
var myPageItems = app.documents[0].allPageItems;
for(var n = myPageItems.length-1 ; n>=0 ; n--)
{
if(myPageItems[n].hasOwnProperty("graphics"))
{
if(!myPageItems[n].graphics.length)
{
if(myPageItems[n].appliedObjectStyle.name == "Delete If Empty")
{
myPageItems[n].remove();
}
};
}
}
}
Copy link to clipboard
Copied
Think this should do it.
app.findObjectPreferences = null;
app.findObjectPreferences.appliedObjectStyles = "Delete If Empty";
var finds = app.activeDocument.findObject();
var i = finds.length;
while(i--) {
if (!finds[i].graphics.length) { finds[i].remove(); }
}
Copy link to clipboard
Copied
Thanks so much for helping. I just need to get my systems administrator to add that file. I will let you know how it goes.
Copy link to clipboard
Copied
Thanks @brian_p_dts - this worked like a charm...
Copy link to clipboard
Copied
Another way - probably not as effective / speedy as @brian_p_dts's solution:
main();
function main(){
var myPageItems = app.documents[0].allPageItems;
for(var n = myPageItems.length-1 ; n>=0 ; n--)
{
if(myPageItems[n].hasOwnProperty("graphics"))
{
if(!myPageItems[n].graphics.length)
{
if(myPageItems[n].appliedObjectStyle.name == "Delete If Empty")
{
myPageItems[n].remove();
}
};
}
}
}
Copy link to clipboard
Copied
You'd probably also want to do allPageItems to catch anything anchored or grouped.
Copy link to clipboard
Copied
You'd probably also want to do allPageItems to catch anything anchored or grouped.
By @brian_p_dts
Yeah, you are right, I've "ignored" it in the first, test version.
Fixed.
BUT ... it will probably fail - yours as well as you don't have try...catch either - when Group have only two items - like in one of the recent posts?
Copy link to clipboard
Copied
Thanks so much. I can't test it just yet, as my systems administrator will need to add that file. I will let you know if it works.
Copy link to clipboard
Copied
@Robert at ID-TaskerYours works perfectly too, thank you 🙂