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;
}
}
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;
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 )
Copy link to clipboard
Copied
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.