Grouping objects around a contour

Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

I have objects on the page, and I want only objects that are inside a square with a contour (with fill magenta) to be in a group. How do you do that?

TOPICS
Scripting

Views

333

Likes

Translate

Translate

Report

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

Adobe Community Professional , Feb 19, 2021 Feb 19, 2021
Hi, I think I knowhow your objects look like. I have edited @Silly-V script to group items inside the object which has stroke color as CutContour. /// <reference types="Path to Illustrator TS Types folder on your system (get it at: https://github.com/bbb999/Types-for-Adobe/tree/master/Illustrator/2015.3)"/>; //#target illustrator function test() { /** @TyPe {Document} */ var doc = /** @TyPe {Application} */(app).activeDocument; /** @TyPe {PathItem} */ var landingZone; /** @...

Likes

Translate

Translate
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Screen Shot 2021-02-17 at 16.48.47.png

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

The rectangle seems to have a magenta Stroke, not a magenta Fill, so I don’t quite follow, could you please post screenshots including the pertinent Panels (Layers, …) to illustrate?  

What is the result supposed to be exactly – just a Group, Clipping Mask, …? 

Likes

Translate

Translate

Report

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

just a group All in one layer

 

I meant the contour is magenta
And not the filling
Please look up I have attached a screenshot

Likes

Translate

Translate

Report

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
Enthusiast ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

For the simplest case:  Iterate through the items and test whether or not the "position" property of each item is within the "geometricBounds" of the magenta rectangle; if it is, move to a group; if it isn't, don't.  

Likes

Translate

Translate

Report

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Right, so....?

Likes

Translate

Translate

Report

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

pay attention
That if there are 2 sets of such objects (or more) in the document
The group will be performed for each set individually
And not for all sets

Likes

Translate

Translate

Report

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
Enthusiast ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

What's in any of these "sets" you refer to? 

Likes

Translate

Translate

Report

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
Enthusiast ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

This is the basic idea, as explained above. How you want to reference the "magenta rectangle" is up to you. Here, it's referenced by name (so name it as such before running either snippet).

 

1. Group pathItems completely inside the magenta rectangle.

 

var group0 = app.activeDocument.groupItems.add();
var paths = app.activeDocument.pathItems;
for (var i = 0; i < paths.length; i++) {
    var b1 = paths[i].geometricBounds;
    var b2 = paths["magenta rectangle"].geometricBounds;
    if ( (b1[0] > b2[0] && b1[2] < b2[2]) && (b1[1] < b2[1] && b1[3] > b2[3]) ) {
        paths[i].moveToEnd(group0);
    }
}

 

2. Group pathItems completely or partly inside the magenta rectangle. 

 

var group0 = app.activeDocument.groupItems.add();
var paths = app.activeDocument.pathItems;
for (var i = 0; i < paths.length; i++) {
    var b1 = paths[i].geometricBounds;
    var b2 = paths["magenta rectangle"].geometricBounds;
    if ( (b1[2] > b2[0] && b1[0] < b2[2]) && (b1[3] < b2[1] && b1[1] > b2[3]) ) {
        paths[i].moveToEnd(group0);
    }
}

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

but how can i change it to color stroke?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Here, try this script:

/// <reference types="Path to Illustrator TS Types folder on your system (get it at: https://github.com/bbb999/Types-for-Adobe/tree/master/Illustrator/2015.3)"/>;
#target illustrator
function test () {
	/** @TyPe {Document} */
	var doc = /** @TyPe {Application} */(app).activeDocument;
	/** @TyPe {PathItem} */
	var landingZone;
	/** @TyPe {PathItem} */
	var thisItem;
	/** @TyPe {RGBColor} */
	var itemColor;
	for (var i = 0; i < doc.pageItems.length; i++) {
		thisItem = doc.pageItems[i];
		itemColor = thisItem.strokeColor;
		// change this to CMYK colors if using CMYK.
		if (itemColor.red == 255 && itemColor.blue == 255 && itemColor.green == 0) {
			landingZone = thisItem;
			break;
		}
	}
	if (typeof(landingZone) == "undefined") {
		alert("Landing zone not found.");
		return;
	}
	var firstArtboard = doc.artboards[0];
	var storedRect = firstArtboard.artboardRect;
	doc.selection = null;
	landingZone.selected = true;
	firstArtboard.artboardRect = landingZone.visibleBounds;
	doc.selectObjectsOnActiveArtboard();
	firstArtboard.artboardRect = storedRect;
	landingZone.selected = false;
	app.executeMenuCommand("group");
};
test();

 

I just have it working in CMYK documents so you'll need to change the color part to suit your needs.

Silly-V_0-1613590047860.png

 

Likes

Translate

Translate

Report

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Thanks, I'll check in at the end of the day

Likes

Translate

Translate

Report

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Thanks!!! I have 2 comments:
When there are 2 such objects in the document, the script is executed for only one object in the document. I would appreciate if you change so that it changes in the document for all objects.
2. I need the group to contain the contour itself as well.

Thanks!

Likes

Translate

Translate

Report

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
Explorer ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Another important note:
It is important that the script work even when there is some fill to the object.
Now it only works if the filling is none.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

Hi,

I think I knowhow your objects look like. I have edited @Silly-V script to group items inside the object which has stroke color as CutContour.

 

/// <reference types="Path to Illustrator TS Types folder on your system (get it at: https://github.com/bbb999/Types-for-Adobe/tree/master/Illustrator/2015.3)"/>;
//#target illustrator
function test() {
    /** @TyPe {Document} */
    var doc = /** @TyPe {Application} */(app).activeDocument;
    /** @TyPe {PathItem} */
    var landingZone;
    /** @TyPe {PathItem} */
    var thisItem;
    /** @TyPe {RGBColor} */
    var itemColor;
    for (var i = 0; i < doc.pageItems.length; i++) {
        thisItem = doc.pageItems[i];
        itemColor = thisItem.strokeColor;
        // change this to CMYK colors if using CMYK.
        //if (itemColor.red == 255 && itemColor.blue == 255 && itemColor.green == 0) {
        if (thisItem.stroked && thisItem.strokeColor.spot && thisItem.strokeColor.spot.name == 'CutContour') {
            landingZone = thisItem;
            var firstArtboard = doc.artboards[0];
            var storedRect = firstArtboard.artboardRect;
            doc.selection = null;
            landingZone.selected = true;
            firstArtboard.artboardRect = landingZone.visibleBounds;
            doc.selectObjectsOnActiveArtboard();
            firstArtboard.artboardRect = storedRect;
            app.executeMenuCommand("group");
        }
    }
};
test();

 

Give it a try.

Best regards

Likes

Translate

Translate

Report

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
Explorer ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot!!!!!!

Likes

Translate

Translate

Report

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