Juan-man
Juan-man

Reputation: 478

Creating an array from a script then alert the contents

I've created a script to create a new array called spots, here is the script:

main();
function main() {
    var doc = app.activeDocument;
    var selectedSwatches = doc.swatches.getSelected();
    var pageNumber = 1;
    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 % 10 == 0)
                    pageNumber++;
            }

        }
        var textend = ');';
        var textArray = text + textend;

        alert(textArray);
    } else {
        alert("No Swatches Selected.");
    }
}

This script alerts the following:

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

How do I now alert the contents of that array i.e. Yellow 012 C, Bright Red C

I have tried using:

alert(spots);

But I get the error undefined maybe because the array is created on the fly and its not placed in the script?

UPDATE:

As per the comments, I have edited the script adding:

var spots = [];
spots.push(color.spot)
alert(spots);

I now get the following error: undefined is not an object

Here's the full script

main();
    function main() {
    var doc = app.activeDocument;
    var selectedSwatches = doc.swatches.getSelected();
    var pageNumber = 1;
    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 % 10 == 0)
                    pageNumber++;
            }

        }
        var textend = ');';
        var textArray = text + textend;
        var spots = [];
        spots.push(color.spot)
        alert(spots);
    } else {
        alert("No Swatches Selected.");
    }
}

Upvotes: 0

Views: 53

Answers (1)

Dominique Fortin
Dominique Fortin

Reputation: 2238

Try

    function main() {
        //var doc = app.activeDocument;
        var selectedSwatches
         = [{"color":{"spot":{"color":"#ff0000","name":"red"},"typename":"SpotColor"}}
           ,{"color":{"spot":{"color":"#000000","name":"black"},"typename":"SpotColor"}}]; 
           // = doc.swatches.getSelected();

        var pageNumber = 1;
        var count = 0;
    
        var spots = [];

        if (selectedSwatches.length > 0) {
            for (var i = 0; i < selectedSwatches.length; i++) {
                var swatch = selectedSwatches[i]
                var color = swatch.color;
                // Spot
                if (color.typename == "SpotColor") {
                    count++;
                    spots.push(color.spot.name);
                    color = color.spot.color;
                    if (count % 10 == 0)
                        pageNumber++;
                }
    
            }
            alert(spots.toString());
        } else {
            alert("No Swatches Selected.");
        }
    }
<button onclick="main()">Main</>

Upvotes: 1

Related Questions