Skip to main content
Inspiring
March 21, 2024
Answered

ideas for updating doc.groupItems.length

  • March 21, 2024
  • 7 replies
  • 877 views

I'm trying to detect corrupt compoundPathsItems (those with groupItems within them) I'm asking here before I'm going the SDK route. I've seen different solutions to this here:

Solved: Dealing with Compound Paths made of Groups - Adobe Community - 8826626

 

My simple idea to do this blazingly fast is to:

1. count doc.groupItem.length

2. duplicate the compoundPathItem

3. check if the doc.groupItem.length has increased.

 

Simple. The problem is that it ut doesn't update the groupItem count after the duplication. redraw() doesn't have an effect. The only thing I found so far to update the count is to:

 item.selected = true; // select something
 doc.selection = null; // deselect everything

Maybe you guys have some trix up your sleaves?

 

Thanks!

This topic has been closed for replies.
Correct answer CarlosCanto

oh ok, try this

 

var idoc = app.activeDocument;

alert(checkIfCorrupt (idoc.compoundPathItems[0]));

function checkIfCorrupt(item) {
  var doc = app.activeDocument;
  var itemCpy = item.duplicate(item, ElementPlacement.PLACEAFTER);
  
  // Update groupItem count.
  itemCpy.translate(10,10);

  //item.selected = true;
  //doc.selection = null;

  for (var i = 0; i < doc.groupItems.length; i++) {
    if (doc.groupItems[i].parent == itemCpy) {
        itemCpy.remove();
      // Return true if corrupt
      return true;
    }
  }
  itemCpy.remove();
}

 

** I would move the copy to the very top of the document to check GroupItems[0] parent instead of looping thru all group items in the document

7 replies

CarlosCanto
Community Expert
Community Expert
March 21, 2024

from all the wonderfull workarounds in the link you posted, I would go with 

 

1. checking all groupItem's parent

2. if parent == compoundPath, problem item

3. release, ungroup, remake

iLLMonkeyAuthor
Inspiring
March 21, 2024

Hi, I tried all the workarounds in that thread, they are very slow I'm afraid. It takes about 10 seconds on my documents.

 

My idea is superfast but I'm looking for a better method to "update" the groupItem count.

 

Selecting/deselecting works but feels a bit clitchy/hacky maybe there's a better way.

 

function findByComparing(item) {
  var doc = app.activeDocument;
  var preNum = doc.groupItems.length;
  var itemCpy = item.duplicate(item, ElementPlacement.PLACEAFTER);

  // update groupItems count
  item.selected = true;
  doc.selection = null;
  
  if (preNum - doc.groupItems.length != 0) {
    itemCpy.remove();
    return true;
  } else {
    itemCpy.remove();
    return false;
  }
}
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
March 21, 2024

oh ok, try this

 

var idoc = app.activeDocument;

alert(checkIfCorrupt (idoc.compoundPathItems[0]));

function checkIfCorrupt(item) {
  var doc = app.activeDocument;
  var itemCpy = item.duplicate(item, ElementPlacement.PLACEAFTER);
  
  // Update groupItem count.
  itemCpy.translate(10,10);

  //item.selected = true;
  //doc.selection = null;

  for (var i = 0; i < doc.groupItems.length; i++) {
    if (doc.groupItems[i].parent == itemCpy) {
        itemCpy.remove();
      // Return true if corrupt
      return true;
    }
  }
  itemCpy.remove();
}

 

** I would move the copy to the very top of the document to check GroupItems[0] parent instead of looping thru all group items in the document