Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Script to select objects of the same width & height

New Here ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

Hi, I'm trying to make a script that can select objects of the same dimensions as the selected object. 

 

So far I have this, which doesn't throw up any errors but it also doesn't work. Can anyone help me fix this? I'm new to scripting in Illustrator so I'm not even sure how to check if it's getting the width/height from the currently selected object. Thanks very much!

 

var docRef=app.activeDocument;

var objWidth = app.selection[0].width;
var objHeight = app.selection[0].height;

var selectionArray = 0;
var str = 0;

var items = docRef.pageItems;
var n = items.length;

for ( i = 0; i < n ; i++ )
{
    var item = items[i];
    if ( item.width == objWidth
    && item.height == objHeight )
    {
        selectionArray [ selectionArray.length ] = item;
    }
}

  

TOPICS
Scripting, Tools

Views

67

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 ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

you're almost there, but you have a couple of issues.

1. you're not actually having the script select anything, you're just trying to add matching items to the selectionArray 

2. however, there's no actual array variable since you previously declared selectionArray as Number by assigning the value of 0

 

one way of solving the issue is by selecting them instead of collecting the items in an array

 

change this line

selectionArray [ selectionArray.length ] = item;

 

to this line

item.selected = true;

 

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
Contributor ,
Oct 28, 2021 Oct 28, 2021

Copy link to clipboard

Copied

I suspect OP meant to write:

var selectionArray = [];
…
        selectionArray.push(item);
…
docRef.selection = selectionArray;

But your way is simpler.

 

OP may also want to allow for slight differences in object size, as widths and heights may not be exactly identical depending on how the objects were created:

 

var margin = 5; // the amount of variation to allow, in pts
if ( item.width >= objWidth - margin && item.width <= objWidth + margin
  && item.height >= objHeight - margin && item.height <= objHeight + margin )

 

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 ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

LATEST

hi hhas, I agree, let's see if th OP comes back with more details.

 

I like your advice and sampe code about adding some buffer to the sizes.

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