稍后在脚本中使用功能

时间:2018-11-09 12:12:15

标签: javascript jsx

您必须原谅我,我不太擅长jsx,我在这里苦苦挣扎。

我有一个函数,该函数返回一个pantone色板数组,有关该数组的结果,请参见下文:

var spots = new Array(
    "PANTONE Yellow C", 
    "PANTONE Yellow 012 C",
);

我可以使用警报显示结果,但是我不知道如何使用数组。

这是我生成数组的方式:

main();

function main() {
    var doc = app.activeDocument;
    var selectedSwatches = doc.swatches;
    var pageNumber = 2;
    var count = 0;
    if (selectedSwatches.length > 0) {
        var text = 'var spots = new Array(\n';
        for (var i = 0; i < selectedSwatches.length; i++) {
            var swatch = selectedSwatches[i]
            var color = swatch.color;
            // Spot
            if (color.typename == "SpotColor") {
                count++;
                text += '"' + color.spot.name + '", ' + "\n";
                color = color.spot.color;
                if (count % 7 == 0)
                    pageNumber++;
            }

        }
        var textend = ');';
        var yepworking = text + textend;
        alert(yepworking)
    }
}

我现在需要使用警报的结果,因为以下行使用了Spots变量。

var pages = Math.ceil(spots.length/7);

这是完整的最后一步;

var pages = Math.ceil(spots.length/7);
var Xoffset = 144;
var Yoffset = -684;
var lineLength = 19;

function makePages(){
    var cPage = 2;
    var shiftX = Xoffset * cPage;
    var shiftY = 0;
    var X = 1;
    var Y = 0;
    var pageOne = app.activeDocument.layers.getByName("PAGE 2");
    while(cPage <= pages){

        if(X >= lineLength){
                X = 0;
                Y++;
        }
        cPage++;
        var nextPage = app.activeDocument.layers.add();
        nextPage.name = "PAGE " + cPage.toString();
        var content = pageOne.pageItems.getByName("LAYOUT");
        var newContent = content.duplicate();
        newContent.move(nextPage, ElementPlacement.INSIDE);
        newContent.textFrames.getByName("PAGE").contents = cPage.toString();
        newContent.translate(Xoffset*X,Yoffset*Y);
        X++;
    }
}

function fillPages(){
    var pCount = 2;
    var cCount = 0;
    for(var i = 1; i <= pages; i++){
            var cPage = app.activeDocument.layers.getByName("PAGE " + pCount.toString());
            var layout = cPage.groupItems.getByName("LAYOUT");
            for(var j = 1; j < 8; j++){
                    var cBlock = layout.groupItems.getByName(j.toString());
                    if(spots[cCount]=="NextPage"){
                        cCount++;
                        break;
                    }
                    cBlock.textFrames.getByName("COLORNAME").contents = spots[cCount].replace("", "");
                    cBlock.pathItems.getByName("FILL").fillColor = app.activeDocument.swatches.getByName(spots[cCount]).color;
                    cCount++;
                    if(cCount >= spots.length){
                        return;
                    }
            }
            pCount++;
    }
}

makePages();
fillPages();

基本上,如果我手动将数组粘贴到jsx文件中,它可以工作,但是如果不这样做我就无法使其工作?

0 个答案:

没有答案