Search code examples
adobe-illustrator

adobe illustrator script- rectangle resize


יello everybody! I tried to create a script that creats a rectangle of a given size, make a group and a clipping mask and after all that resize it to a diffrent size in mm.

for example I have a graphic with the name "fish400" and I clip it in a rectangle of 400X400 that I create with the script. so far so good. my problem is that I want to resize the clipping with all it's content to 382. when I set the height to be the rec. size-18 it gets the height of 385.1

I'm not a very skilled programmer and the script could be written better but I really don't get my mistake.

here is my code:

var doc = app.activeDocument; 
var layers = doc.layers;  
var myLayer = layers["Layer 1"]; //this defines the layer that you want to get the selection from 

var vals = 0;
var tzim = 0;
var side = 0; //width || height

//mm convertor
function mm(n) {return n * 2.83464566929134;}

doc.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.

var found = false;
for(var a=0 ;a<doc.groupItems.length; a++)
{
    if (doc.groupItems[a].name == "fish400") {vals = mm(400); tzim = mm(18); side = 1; found = true;}
}

if (found = true)
{
    var rect = doc.pathItems.rectangle(0,0,vals,vals);

    app.executeMenuCommand("selectall");

    var groupItem = doc.groupItems.add();
    var count = selection.length;
    for(var i = 0; i < count; i++) 
    {
        var item = selection[selection.length - 1];
        item.moveToBeginning(groupItem);
    }

    groupItem.clipped = true;
    
    item.top = center_point[0] + (item.height/2);
    item.left = center_point[1] - (item.width/2);
    
    if (side == 1) {groupItem.height -= tzim;} else if (side == 0) {groupItem.width -= tzim;}
}

Solution

  • If your script works fine for you (it doesn't work for me, though) and all you want is to add resizing for your picture from 400x400 to 382x382 mm you can just to add at the end of your script these lines:

    var k = 382/400*100;
    groupItem.resize(k,k);
    

    Or:

    app.executeMenuCommand("selectall");
    var sel = app.selection[0];
    var k = 382/400*100;
    sel.resize(k,k);