3

I need a plugin, or script that deletes frames with a specific applied style. Or if there is already a tool exists in Indesign please guide me there.

3 Answers 3

6

If you need a more fine grained search engine you could also use the FindChange capabilities built into InDesign (you still need a little bit of code to delete all the objects though).

You can set the object style to search for or just specific properties in the FC panel and get all the found elements with this little script snippet.

 app.doScript(function() {
var objectsList = app.findObject();
for (var i = objectsList.length -1; i >=0 ; i --){
        objectsList[i].remove()
    }
  }, ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, 'Object removal via script');

Update 2018-01-15: Make it a one step undo mode

enter image description here

5
  • hey there fabian... a great workaround and I think its great for detailed object properties... looks simple on coding which made it works fast... is there a way to made a One-Undo-level for it?
    – Roozbeh E
    Jan 15, 2018 at 6:10
  • Yes you can. See the changes Jan 15, 2018 at 9:50
  • 1
    thanks fabian... works great. also with help of your script, I added Undo to the script of Wolff (the script in the first answer) and it works well there too. ;)
    – Roozbeh E
    Jan 15, 2018 at 10:16
  • This is cool! I didn't know you could use Find/Change like that. I like the one-undo idea as well.
    – Wolff
    Jan 15, 2018 at 19:29
  • 1
    good to hear had somtin' new for you here. thanks to fabian helped us both!
    – Roozbeh E
    Jan 16, 2018 at 4:56
5

This little script should do the job.

The main functionality of the script (the first part) is commented and quite simple (if you know javascript). I have added a small input dialog to make the script easy to use, but I haven't commented that part of the code.

Copy the code below to a text editor and save it as a .jsx file (for example RemoveAllObjectsWithACertainObjectStyle.jsx) in the folder for user scripts.

In InDesign, run the script and a dialog box will appear. Choose which Object Style to remove and press "OK".

Does this work for you?


// REMOVE ALL OBJECTS WITH A CERTAIN OBJECT STYLE
// Copyright (c) 2018 Mads Wolff
// This script is distributed under the MIT License.


// MAIN FUNCTIONALITY

// Make a reference to the Object Styles of the active document.
var objectStyles = app.activeDocument.objectStyles;

// Make a reference to the Page Items of the active document.
var pageItems = app.activeDocument.allPageItems;

// Removes all Objects with a certain Object Style.
// Takes 2 arguments:
//   objectStyleName  the name of the Object Style
//   removeFrom          the kind of Spreads to apply to (0: all, 1: only normal Spreads, 2: only Master Spreads)
function removeAllObjectsWithACertainObjectStyle(objectStyleName, removeFrom) {

  // Iterate through the Page Items.
  // (Since we are going to delete some of the items while iterating, we need to "loop backwards".)
  for (i = pageItems.length- 1; i >= 0; i--) {

    // Make a reference to the Page Item.
    var pageItem = pageItems[i];

    // Make a reference to the Page Item's Spread type
    var spreadType = pageItem.parentPage.parent.constructor.name;

    // Check if the Page Item has the right Object Style and Spread type.
    if (
      pageItem.appliedObjectStyle.name === objectStyleName &&
      (removeFrom === 0 || (removeFrom === 1 && spreadType === "Spread") || (removeFrom === 2 && spreadType === "MasterSpread"))
    ) {

      // Remove the Page Item.
      pageItem.remove();

    }

  }

}


// DIALOG

// Stores all the names of the document's Object Styles in an array.
function getObjectStylesNames() {
  var objectStyleNames = new Array;
  for (i = 0; i < objectStyles.length; i++){
    objectStyleNames.push(objectStyles.item(i).name);
  }
  return objectStyleNames;
}

// Displays the input dialog.
function displayDialog(){
  var dialog = app.dialogs.add({name:"Remove all Objects with a certain Object Style"});
  var objectStyleNames = getObjectStylesNames();
  with (dialog) {
    with (dialogColumns.add()) {
      with (borderPanels.add()) {
        with (dialogColumns.add()) {
          staticTexts.add({staticLabel:"Object Style:"});
          staticTexts.add({staticLabel:"Remove from:"});
        }
        with (dialogColumns.add()) {
          var objectStyleNameDropdown = dropdowns.add({stringList: objectStyleNames, selectedIndex: 0, minWidth: 200});
          var radiobuttonGroup = radiobuttonGroups.add();
          with (radiobuttonGroup) {
            radiobuttonControls.add({staticLabel: "All Spreads", checkedState: true})
            radiobuttonControls.add({staticLabel: "Normal Spreads only"})
            radiobuttonControls.add({staticLabel: "Masters Spreads only"})
          }
        }
      }
      with (borderPanels.add()) {
      }
    }
  }
  var dialogReturn = dialog.show();
  if (dialogReturn == true) {
    var objectStyleName = objectStyleNames[objectStyleNameDropdown.selectedIndex];
    var removeFrom = radiobuttonGroup.selectedButton;
    dialog.destroy();
    removeAllObjectsWithACertainObjectStyle(objectStyleName, removeFrom);
  } else {
    dialog.destroy();
  }
}

displayDialog();

Update 14/1 2018

The script now works with anchored objects and makes the user choose whether to remove objects from normal Spreads, Master Spreads or both.

12
  • Amazing script... works like a charm and easy to use. thanks a lot wolff. is it possible to add a check box to exclude the master pages from process?
    – Roozbeh E
    Jan 14, 2018 at 8:25
  • Oops... found that script won't work on anchored frames... is there a way to do that too?
    – Roozbeh E
    Jan 14, 2018 at 9:08
  • Glad you liked it @RoozbehE. I have updated the script to work with anchored objects and added a setting for which type of Spreads to remove from. Please let me know if it works as intended!
    – Wolff
    Jan 14, 2018 at 12:27
  • thanks alot wolff... the options are made this script perfect. but strangely I run the script within a sample file (which is fresh and new document) it runs smooth and flawless... but I have big document and when I run the script there after it removes only a few found ones (about three) it closes script with an error I linked it below as an image: drive.google.com/open?id=1bmsARn3EgK5TFB24bnHY3FaEgFI2faSX
    – Roozbeh E
    Jan 15, 2018 at 5:44
  • ... also I'm using CS6 ver8 middle east version, if its an issue with java script version objects or such.
    – Roozbeh E
    Jan 15, 2018 at 5:48
1

Thank you so much for the amazing script. If I have not defined an object style, the script does not work for me claiming that: "null is not an object". What needs to be changed so that it deletes all the boxes in the document?

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.