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

InDesign Java Script to return Swatches used by object

Community Beginner ,
Apr 30, 2024 Apr 30, 2024

Copy link to clipboard

Copied

Hello, beginning js / indesign user here. I have the following question:

 

Is there a way to get the swatches used by an object via js? I already know that objects don't usually have a swatches-property, however in indesign you can create a dynamic caption that will display these same swatches by putting their names in a text frame.

 

If the dynamic caption can display this, but objects do not have assigned swatches, where does it get the information necessary from? And can I use this to get a list/array/string or similar in js? I am specifically looking for a method or a partial script.

TOPICS
Scripting

Views

279

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 ,
Apr 30, 2024 Apr 30, 2024

Copy link to clipboard

Copied

Object can have ObjectStyle applied. 

 

And are you looking for fill / stroke only or for all glows / shadows / etc. colors as well?

 

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 ,
Apr 30, 2024 Apr 30, 2024

Copy link to clipboard

Copied

Hi @SquirrelsInASuit , If by object you mean pageItems or texts, they can have a fillColor and strokeColor property, which returns the color or swatch object used:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#PageItem.html

 

Screen Shot 12.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 Beginner ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

Thank you for your answers, I should have clarified. The object is a rectangle, but it is not filled with a singel color, instead it is an actual picture. The reason why I need the swatches is that this page is to be printed, and in the method of printing we use we print the CMYK colors on top of each other until they come together to form the finished picture (not only cmyk of course, hence the swatches problem).

As you can see in the attached picture there is a setting for dynamic captions wherein the "swatches used" of the object the caption is linked to will be displayed as text. I need this information in script form. 

My only workauround so far is a little clunky, so I am trying to streamline my code.

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 ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

@SquirrelsInASuit

 

Have you applied ObjectStyle to the caption's TextFrame?

 

I'm pretty sure, by default, "neutral" ObjectStyle is applied - "[Basic TextFrame]".

 

Then, there is a color specified in the ParaStyle. 

 

Can you maybe post an example screenshot - with indication of which exactly color you would like to get a reference to?

 

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 Beginner ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

There is no object style applied. If you look at the screenshots you might be able to see what I mean: if I move the caption to the image, the information I seek is displayed in the textframe. My workaround is to first create the captions and then have a script to read the literal string from them, but I want to know if I can skip the captions.Test1.PNGTest2.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 ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

@SquirrelsInASuit

 

So you want to know inks / plates - swatches are completely different thing.

 

Question - why? 

 

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 Beginner ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

In short, I want to extract this infomration and add circles underneath the image cointaining the colors. It is important, that only those colors are added, wchich are non-standard.

My script does this already, but as I said it takes the information from a previously generated caption and I wanted to circumvent this.

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 ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

@SquirrelsInASuit

 

You can create Captions "on demand" - then delete them.

 

There was a thread about that. 

 

I'll try to find 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 Beginner ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

Thank you,  if you find it please let me know, all I found was generating static captions...

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 ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

@SquirrelsInASuit

 

@rob day posted a sample code - it was pretty much the same - using invoke() to execute menu option - then remove the TextFrame.

 

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 ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

So when you generate the caption you get a result—the caption is not <No Data from Link>?

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 Beginner ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

No, if you look at the screenshots I posted above I get the correct caption - what I seek is how to get the information that those captions get without actually creating the captions in the first place, because I don't need those

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 ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

The only other way I can think of would be to use a Bridgetalk object to open the linked file and get its metadata. Not sure that would have any advantage over getting the metadata via a caption.

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 Beginner ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

It would be more convenient in my mind, if this is a considerable effort then I will just use the script as is. I will look into Bridgetalk, thank you!

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 ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

I will look into Bridgetalk, thank you!

 

With BridgeTalk you would have to open the link in its application, which surely would not be any faster.

 

Not sure if this is better than what you have—it’s an adaption of a caption making function I use. It gets the .resultText property of the passed in metadata property rather than getting the contents of the caption frame. The caption frame is removed before it’s drawn on the page.

 

Here I’m getting the image’s color profile:

 

Screen Shot 13.png

 

var s = app.activeDocument.selection[0]
var r = getImageMeta(s, "ICC Profile")
//var r = getImageMeta(s, "Swatches Used")
alert(r)

/**
* Gets a link‘s metadata value
* @ param a linked object
* @ param the metadata string property to get
* @ return the metadata property value 
*/

function getImageMeta(img, p){
  
  var v = "No Value";
  //check if img is a linked object
  if (!img.hasOwnProperty("itemLink")) {
    alert("Please Select a Linked Object")
    return
  } 
  //select the image’s container and make a caption
  img.parent.select()
  var makeCap =  app.menuActions.itemByName("Generate Live Caption")
  makeCap.invoke();

  //get the caption result text
  var tv = app.activeDocument.textVariables
  for (var i = 0; i < tv.length; i++){
      try {
          var tvo = tv[i].variableOptions
          if (tvo.constructor.name == "CaptionMetadataVariablePreference") {
              tv[i].variableOptions.metadataProviderName = p
              v = tv[i].associatedInstances[0].resultText
          } 
      }catch(e) {}  
  } 
  //remove the caption
  app.activeDocument.textFrames[0].remove();
  img.select()
  return v
}

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 Beginner ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

That looks like what I'm looking for, thanks a lot! I'll try it out, seems like a great solution!

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 Beginner ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

OK, I really am a beginner so... when I execute your code it says that the image has no itemLink, but it is a linked item... what does that mean?

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 ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

LATEST

Are you direct selecting the image and not its container frame?

 

The first parameter is a linked image or graphic not a frame, so var s could also be this which would be the document’s first linked object:

 

 

var s = app.activeDocument.links[0].parent

 

 

 And this would return the ICC Profile for every link in the doc:

 

var s = app.activeDocument.links.everyItem().getElements();

for (var i = 0; i < s.length; i++){
    var r = getImageMeta(s[i].parent, "ICC Profile")
    $.writeln(r)//returns the link’s assigned profile
};   

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 ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

quote

It would be more convenient in my mind, if this is a considerable effort then I will just use the script as is. I will look into Bridgetalk, thank you!


By @SquirrelsInASuit

 

There are two more options - but as @rob day pointed out - probably not faster - opening file on Photoshop or analysing file directly and look for metadata - unless metadata is always at the beginning or end of the file. 

 

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