Skip to main content
Participant
June 12, 2025
Question

Use a group of shapes to select all intersecting shapes from another group

  • June 12, 2025
  • 1 reply
  • 217 views

Hello!
I have been working for a while trying to find a way to create a new layer with all the shapes that intersect another group of shapes. Specifically, in my scenario I have the word "SOL" (expanded into shapes, compound path for the O though it doesnt work even with a filled O), and a background floral art design that is composed with a bunch of shapes as seen below.

I want to create something like this, but without manually deleting each shape that doesn't appear to intersect the letters. Is there a way to do this? I tried getting ChatGPT to generate scripts that use Illustrators hitTest, but they never worked. ChatGPT also made scripts using Illustrators pathfinder, but they took so long I had to force quit Illustrator. Now my clipping masks arent even working, and are just returning empty letters as you can see below.

The following is the hitTest script ChatGPT made. I don't know why this doesn't work, and nor do I know why it would possibly work because I don't know about hitTest or jsx.

"

if (app.documents.length === 0) {
    alert("Open a document first.");
} else {
    var doc = app.activeDocument;
    var sel = doc.selection;

    if (sel.length !== 1 || !(sel[0].typename === "GroupItem")) {
        alert("Select the 'SOL' group (must be a group of paths).");
    } else {
        var refGroup = sel[0];
        var refShapes = [];

        // Collect all individual paths from the selected group (the letters)
        function collectPaths(group, arr) {
            for (var i = 0; i < group.pageItems.length; i++) {
                var item = group.pageItems[i];
                if (item.typename === "GroupItem") {
                    collectPaths(item, arr);
                } else if (item.typename === "PathItem" || item.typename === "CompoundPathItem") {
                    arr.push(item);
                }
            }
        }

        collectPaths(refGroup, refShapes);

        // Create result layer
        var resultLayer = doc.layers.add();
        resultLayer.name = "Overlapping Shapes";

        var count = 0;
        var addedItems = [];

        // Search entire document
        function getAllPathsInDocument(doc) {
            var arr = [];
            for (var i = 0; i < doc.pageItems.length; i++) {
                var item = doc.pageItems[i];
                if (item.locked || item.hidden || item.layer.name === resultLayer.name) continue;
                if (item === refGroup || item.parent === refGroup) continue;

                if (item.typename === "GroupItem") {
                    collectPaths(item, arr);
                } else if (item.typename === "PathItem" || item.typename === "CompoundPathItem") {
                    arr.push(item);
                }
            }
            return arr;
        }

        var allTargets = getAllPathsInDocument(doc);

        // Compare every target against every SOL letter
        for (var i = 0; i < allTargets.length; i++) {
            var target = allTargets[i];

            for (var j = 0; j < refShapes.length; j++) {
                try {
                    if (target.hitTest(refShapes[j], true)) {
			target.selected = true;
                        if (addedItems.indexOf(target) === -1) {
                            target.duplicate(resultLayer, ElementPlacement.PLACEATBEGINNING);
                            addedItems.push(target);
                            count++;
                        }
                        break;
                    }
                } catch (e) {
                    // skip on error
                }
            }
        }

        alert(count + " overlapping shapes copied to: " + resultLayer.name);
    }
}

"

1 reply

Silly-V
Legend
June 13, 2025

I really like this project, curious about more information on how you intend to use this. Is the goal to be able to write any text, run it through this procedure which may involve this or other "background shapes" files, and create typographic designs?
Well, anyway - the issue is due to the amount of paths. The way Mr GPT did it, is quite basic, so it thinks you have a typical document with just a text-frame and one square and one circle, etc.

But, what you have is a rich background with many many shapes, after a while it catastrophically slows down the scripting when it tries to go through them.
To fix this, you could:

  1.  Consolidate the shapes into distinct groups, so in your Layer on the layers panel, it shows a minimal amount of top-level items.
  2.  Ensure that these groups have ONE clear boundary shape (this can be a pain, but use Actions to go through the shapes and duplicate + add them to form the contour), and label it some certain name, so that this contour can be used for the hit tests.
  3.  Explain to ChatGPT the situation and what you are doing, so it re-makes the script taking into account that you now only have top-level groups in some layer, and inside each of the top-level groups you have one path-item called something, which is your hit-test contour.
  4.  Try the new code to see if it doesn't crash your application and does your action as you want.

 

UPD: I just looked at some of the code and it appears that ChatGPT had misled you in a case of classic hallucinatory well-meaning advice.

No optimization would fix this code, as I do not believe any such thing as .hitTest() exists.

 

So, next what to do: well, you can:

  1. Still consolidate the shapes into some groups that have a contour that could be used by a potential true hit test. 
  2. Get it to spew out the proper math function that does the hit test, whether using mathematics or using Pathfinder.
  3.  If none of this is going well, maybe you could try this:
    • Pre-make all your letters, slice them up into cubes like they are chopped potatoes.
    • Make such a script that will place your letter groups,
    • then go through the 'cubes' in the sliced-up letters, convert each one at a time, to a small artboard.
    • then have your script use a tried & true hit-test using artboards and selecting objects on active artboard.
    • Now, you can have this script isolate the selected items to copy to a final layer and remove any duplicates which may have been used for utility in this operation.