• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
0

defaultFillColor Error thrown inconsistently.

Engaged ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

I'm trying very simply to determine if the there is more than one color being used in some art, and if there is no error run a script. It appears that if I make 2 squares filled with black and white in a CMYK doc I get this error:

Eval Error (#1239): "Specified value greater than maximum allowed value"

If I add a Pantone color to that equation, I'll get [Gray Color] as a result. What I'm trying to do, is catch that error consistently since the defaultFillColor doesn't have a length. Can it be done?
The script I've been testing with is:

 

app.executeMenuCommand("selectall");
alert(app.activeDocument.defaultFillColor);

 

 

TOPICS
Scripting

Views

334

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

try this

 

- select all

- count selected items

- deselect all

- set default fill color = first pageItems fill color

- select all items with the same fill color as the current default using

app.executeMenuCommand ( "Find Fill Color menu item" )

- count selected items

- compare to total count in step 2

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

Hi @iamwickedtall, it's weird that you get that error (I get the same error when I run your script) and I don't know what's going on there. However, I can't really understand what you are trying to learn? Do you want to know when every item in the document doesn't have the same fillColor? Using defaultFillColor doesn't seem to be the right thing to do here. It might be better to loop over all the page items and just check if they have the same fillColor?

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

Thanks for the response. If this threw a consistent error I was going to use it to make a compound path if there was only 1 fill. Sounds like I'm going to have to loop. I prefer not to because of more complicated files.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

LATEST

In my opinion, looping is a better way because you can carefully curate each item based on its properties. If something goes wrong, you can look at the object that broke it, and adjust your logic to exclude/handle it.

 

I've written a script to get started. It is very simple in terms of the logic but I think it can handle many cases. If you decide to try it out, let me know how it goes.

- Mark

 

 

/**
 * Make a CompoundPathItem out of document's path items
 * ONLY IF they all have the same fillColor value.
 * Note: this will work even if the items are locked or hidden.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/defaultfillcolor-error-thrown-inconsistently/m-p/13651215
 */
(function () {

    var doc = app.activeDocument;

    // only PathItems can go into CompoundPathItems
    var items = doc.pathItems,
        uniformItems = getUniformFillColoredPathItems(items);

    if (
        uniformItems == undefined
        || uniformItems.length < 1
    ) {
        alert('Document doesn\'t have uniformly-colored path items.');
        return;
    }

    // make a compoundPathItem out of the uniformItems
    var cp = doc.compoundPathItems.add();
    for (var i = uniformItems.length - 1; i >= 0; i--)
        uniformItems[i].move(cp, ElementPlacement.PLACEATBEGINNING);

})();


/**
 * Will return an array of items ONLY IF those
 * items all have the same fillColor value.
 * Note: items without a fillColor property
 * are ignored.
 *  m1b
 *  2023-03-15
 *  {Array<PageItem>} items - the items to check.
 *  {Array<PathItem>} - the uniform-colored items.
 */
function getUniformFillColoredPathItems(items) {

    var uniformFillColoredItems = [],
        theFillColor;

    for (var i = 0; i < items.length; i++) {

        var item = items[i];

        if (item.typename !== 'PathItem')
            continue;

        if (theFillColor == undefined) {

            if (
                item.fillColor != undefined
            ) {
                theFillColor = item.fillColor;
                uniformFillColoredItems.push(item);
            }
            else {
                continue;
            }

        }

        else {
            if (objectsAreEqual(theFillColor, item.fillColor))
                // found matching fillColor
                uniformFillColoredItems.push(item);
            else
                // this one didn't match so game over
                return;
        }

    }

    return uniformFillColoredItems;

};


/**
 * Returns true if the two objects have
 * matching values for each property.
 *  m1b
 *  2023-03-15
 *  {Object} obj1 - an object to compare.
 *  {Object} obj2 - an object to compare.
 *  {Boolean}
 */
function objectsAreEqual(obj1, obj2) {
    for (key in obj1) {
        if (
            obj1.hasOwnProperty(key)
            && obj1[key] != obj2[key]
        )
            return false;
    }
    return true;
};

 

P.S. this script is fairly strict about whether color A matches color B. You may want to make it less strict, such comparing the color channel values.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines