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

how to find the object with format stroke : "Unnamed Color" with tint 50% in Indesign doc using JS

Engaged ,
Mar 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

KarthikSG_0-1678452651265.png

how to find the object with format stroke : "Unnamed Color" which is red in color and has color value "(C=11,M=99,Y=96,K=2)" and many Unnamed Color were present in Stroke swatch list. how to find exactly that cmyk red alone and replace its stroke value using indesign javascript

	app.findChangeObjectOptions.objectType= ObjectTypes.ALL_FRAMES_TYPE;	
	app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
     app.findObjectPreferences.strokeColor == "Unnamed Color";
   //  app.findObjectPreferences.strokeTint =60;            
	var foundItems = app.activeDocument.findObject(true);
	for (var i = 0; i < foundItems.length; i++) {
		foundItems[i].strokeTint =50;    
	}

 

TOPICS
How to , Scripting

Views

927

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 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

I think there is a bug in the scripting API for app.findObjectPreferences.strokeColor (and fillColor). I have created a bug report, so please vote for it.

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 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

In the meantime, you can use something like this:

function main() {

    var doc = app.activeDocument,
        colorBreakdown = [11, 99, 96, 2],
        targetColor = getColor(doc.swatches, undefined, colorBreakdown);

    if (targetColor == undefined) {
        alert('Could not find target color swatch with breakdown "' + colorBreakdown + '".');
        return;
    }

    var items = doc.allPageItems;

    for (var i = 0; i < items.length; i++) {
        if (
            items[i].strokeColor == targetColor
            && items[i].strokeTint != -1 // exclude 100% tint
        ) {
            items[i].strokeTint = 50;
        }
    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

/**
 * Gets a color from `colorItems`
 * with matching name and/or breakdown.
 * @author m1b
 * @version 2023-03-11
 * @Param {Swatches|Colors|Array<Swatch|Color>} colorItems - a collection of Swatches or Colors.
 * @Param {String} [name] - the name to match.
 * @Param {Array<Number>} [breakdown] - the colorValue array to match.
 * @Returns {Swatch|Color};
 */
function getColor(colorItems, name, breakdown) {

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

        var c = colorItems[i];

        if (!c.hasOwnProperty('colorValue'))
            continue;

        if (
            name != undefined
            && name != c.name
        )
            continue;

        if (
            breakdown != undefined
            && !arraysAreEqual(breakdown, c.colorValue)
        )
            continue;

        return c;

    }

};


/**
 * Returns true if both arrays are equal.
 * @Param {Array} arr1 
 * @Param {Array} arr2 
 * @Returns {Boolean}
 */
function arraysAreEqual(arr1, arr2) {

    if (arr1.length !== arr2.length)
        return false;

    for (var i = 0; i < arr1.length; i++) {
        if (arr1[i] !== arr2[i])
            return false;
    }

    return true;

};

Instead of using findObject, it just iterates over the document's page items. You can adjust the predicates. I set it so it will only change the tint to 50% if the tint isn't 100%.

- 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 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Thanks for the reply, this is also not working for me it thows error as item has multiple graphical properties

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 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Hi @Karthik SG , As Robert suggests you could invoke Add Unnamed Colors menu item. Does this work?

 

var menuItem =  app.menuActions.itemByID(16403)
if(menuItem.enabled){  
    menuItem.invoke();  
};

var cn = "C=11 M=99 Y=96 K=2"
app.findChangeObjectOptions.objectType= ObjectTypes.ALL_FRAMES_TYPE;
app.findObjectPreferences = app.findObjectPreferences = app.findObjectPreferences = null;	
app.findObjectPreferences.strokeColor = cn;           
var foundItems = app.activeDocument.findObject();
$.writeln(foundItems)

 

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 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Hi @rob day, a side question... does that script actually work for you? I cannot get it to work, and a search on the forum shows it's an old bug that's been around for many years. See bug report. When I run your code, I get this error: Invalid value for set property 'strokeColor'. Expected Swatch, String or NothingEnum enumerator, but received "C=11 M=99 Y=96 K=2" in VSCode when I run that script. If it works for you, it would be great to know what's different. - 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
Community Expert ,
Mar 12, 2023 Mar 12, 2023

Copy link to clipboard

Copied

Hi @m1b , Yes I’m seeing the error, but it’s intermitent and I realized it wasn’t happening if I ran a grep or text search for the color before the object search. Does this work for you?

 

var menuItem =  app.menuActions.itemByID(16403)
if(menuItem.enabled){  
    menuItem.invoke();  
};
var cn = "C=11 M=99 Y=96 K=2"
app.findObjectPreferences = app.findObjectPreferences = app.findObjectPreferences = null;

//setting the changeTextPreferences fillColor seems to fix the object bug
app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
app.changeTextPreferences.fillColor = cn;

app.findChangeObjectOptions.objectType= ObjectTypes.ALL_FRAMES_TYPE;
app.findObjectPreferences.strokeColor = cn;//randomly throws an error           
var foundItems = app.activeDocument.findObject();
alert(foundItems.length + " Items found")

 

Screen Shot 1.png

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 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

Hi @rob day, that's a great observation! I confirm the same behaviour that you see.

 

So first I run your script without this line:

app.changeTextPreferences.fillColor = cn;

and I see the error (mentioned in the bug report).

 

Then I add in the line above, and ... no error! And that bug doesn't reappear until I quit and relaunch Indesign.

- 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
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Hi @Karthik SGI don't know what you mean by multiple graphical properties—every item has multiple graphical properties.

 

However, if you would like to get better answers, please always help us by adding (a) the error message you saw (preferably a screen shot of it) and (b) a sample .indd that will throw the error when we run the script. If you provide those, you will get the quickest and best responses.

- 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
Community Expert ,
Mar 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

Are you unable to change the name in the UI first ?

 

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 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Yes unable to change the name on that indesign object stroke panel and also unable fetch the color value of that unnamed color swatch.

 

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 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Can you share the document ? Can be stripped out of most of the contents - but please leave few original objects.

 

On a different note 😉 You could Export your document as IDML - and edit it there 😉 It's a ZIPped plain text structure of the document.

 

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 16, 2023 Mar 16, 2023

Copy link to clipboard

Copied

LATEST

"Yes unable to change the name on that indesign object stroke panel and also unable fetch the color value of that unnamed color swatch."

 

Hi @Karthik SG ,

loop the colors collection of the document. Make it an array like that and loop it; read out all name values:

var allDocColorsArray = app.documents[0].colors.everyItem().getElements();

You could filter out the unnamed ones:

allDocColorsArray[n].name == "";

The name "Unnamed Color" you see in the UI is not the right value to look for.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

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