Skip to main content
Participant
May 4, 2023
Answered

How to determine whether the spot color and exclude the specified spot color

  • May 4, 2023
  • 1 reply
  • 434 views

I want to use a script to determine if a color is spot color and exclude specific spot colors, I used the following code and it works

if (doc.spots[i].typename == "Spot" && doc.spots[i].colorType != ColorModel.REGISTRATION && doc.spots[i].colorType != ColorModel.PROCESS && doc.spots[i].name !='dieline') {//do something}

 

But now I want to exclude multiple spot colors, so I'll have to

 

doc.spots[i].name !='spots1' && doc.spots[i].name !='spots2' %% doc.spots[i].name !='spots3' ......

This way should be too cumbersome,And it is not easy to solve when the 'spots1' is variable,

is there a simpler way to do ?

 

The above content is from the translation, because English is not my native language

This topic has been closed for replies.
Correct answer m1b

Hi @Dodo Bird, one approach is to use indexOf() to search an Array for a particular string. ExtendScript's Array doesn't come with an indexOf method, so we can make a simple one. Then I've put the filter into a function that also has the list of excluded names—it might be better to put that outside the function in, say, the script settings where you can edit them easily.

Hope that makes sense. 🙂

- Mark

 

/**
 * Using indexOf function to filter spot names.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/how-to-determine-whether-the-spot-color-and-exclude-the-specified-spot-color/m-p/13770334
 */
(function () {

    // example

    var doc = app.activeDocument,
        wantedSpots = [];

    for (var i = 0; i < doc.spots.length; i++) {
        if (spotIsWanted(doc.spots[i]))
            wantedSpots.push(doc.spots[i]);
    }

    $.writeln(wantedSpots);

})();


/**
 * Filters a Spot, based
 * on internal criteria.
 * @param {Spot} spot - an Illustrator Spot.
 * @returns {Boolean} - the result.
 */
function spotIsWanted(spot) {

    var excludeTheseNames = [
        'My Unwanted Spot',
        'Even Worse Spot',
        'Terrible Spot',
        'PANTONE 7607 C',
    ];

    return (
        spot.typename === 'Spot'
        && spot.colorType !== ColorModel.REGISTRATION
        && spot.colorType !== ColorModel.PROCESS
        && indexOf(spot.name, excludeTheseNames) === -1
    );

};


/**
 * Returns index of obj in arr,
 * or -1 if not found.
 * @param {Array<any>} arr - the array to search.
 * @param {any} obj - the object to search for.
 * @returns {Number} - the index.
 */
function indexOf(obj, arr) {
    for (var i = 0; i < arr.length; i++)
        if (obj == arr[i])
            return i;
    return -1;
};

Edit 2023-05-04: changed indexOf to a normal function, not Array.prototype, for clarity.

 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
May 4, 2023

Hi @Dodo Bird, one approach is to use indexOf() to search an Array for a particular string. ExtendScript's Array doesn't come with an indexOf method, so we can make a simple one. Then I've put the filter into a function that also has the list of excluded names—it might be better to put that outside the function in, say, the script settings where you can edit them easily.

Hope that makes sense. 🙂

- Mark

 

/**
 * Using indexOf function to filter spot names.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/how-to-determine-whether-the-spot-color-and-exclude-the-specified-spot-color/m-p/13770334
 */
(function () {

    // example

    var doc = app.activeDocument,
        wantedSpots = [];

    for (var i = 0; i < doc.spots.length; i++) {
        if (spotIsWanted(doc.spots[i]))
            wantedSpots.push(doc.spots[i]);
    }

    $.writeln(wantedSpots);

})();


/**
 * Filters a Spot, based
 * on internal criteria.
 * @param {Spot} spot - an Illustrator Spot.
 * @returns {Boolean} - the result.
 */
function spotIsWanted(spot) {

    var excludeTheseNames = [
        'My Unwanted Spot',
        'Even Worse Spot',
        'Terrible Spot',
        'PANTONE 7607 C',
    ];

    return (
        spot.typename === 'Spot'
        && spot.colorType !== ColorModel.REGISTRATION
        && spot.colorType !== ColorModel.PROCESS
        && indexOf(spot.name, excludeTheseNames) === -1
    );

};


/**
 * Returns index of obj in arr,
 * or -1 if not found.
 * @param {Array<any>} arr - the array to search.
 * @param {any} obj - the object to search for.
 * @returns {Number} - the index.
 */
function indexOf(obj, arr) {
    for (var i = 0; i < arr.length; i++)
        if (obj == arr[i])
            return i;
    return -1;
};

Edit 2023-05-04: changed indexOf to a normal function, not Array.prototype, for clarity.