首页 > 解决方案 > adobe illustrator 脚本 - 矩形调整大小

问题描述

大家好!我试图创建一个脚本来创建一个给定大小的矩形,创建一个组和一个剪贴蒙版,然后将其调整为不同的 mm 大小。

例如,我有一个名为“fish400”的图形,我将它剪辑在我用脚本创建的 400X400 的矩形中。到目前为止,一切都很好。我的问题是我想将剪辑的所有内容调整为 382。当我将高度设置为记录时。size-18 它的高度为 385.1

我不是一个非常熟练的程序员,脚本可以写得更好,但我真的不明白我的错误。

这是我的代码:

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;}
}

标签: adobe-illustrator

解决方案


如果您的脚本适合您(但它对我不起作用)并且您想要的只是将图片的大小从 400x400 调整为 382x382 毫米,您可以在脚本的末尾添加以下几行:

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

或者:

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

推荐阅读