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

Text variable to add swatch names to text box.

Explorer ,
Jul 14, 2022 Jul 14, 2022

Does anyone know how to create a text variable to add the swatch names to a text box to be used as part of a slug line for the document?

TOPICS
How to , Scripting
4.3K
Translate
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

correct answers 2 Correct answers

Community Expert , Jul 14, 2022 Jul 14, 2022

 

var doc = app.activeDocument;
var swatches = doc.swatches;
var i = swatches.length;
var s = "Swatches Used: ";
var a = [];
while (i-- && i > 3) {
    app.findColorPreferences.findWhat = swatches[i];
    if (doc.findColor() > 0) {
         a.push(swatches[i].name);
    } 
}
a.sort();
s+= a.join(", ");
var label = "";
with (doc.documentPreferences) {
    label += "W x H: " + pageWidth + " x " + pageHeight + "; " + s;
}
var tf = app.selection[0];
tf.contents = label;

 

Translate
Explorer , Jul 15, 2022 Jul 15, 2022

Duh. Sorry about that.

thank you so much! I think this will work perfectly!!!!

Translate
Community Expert ,
Jul 14, 2022 Jul 14, 2022

Can you describe a little more what you need. A list of all swatches in the doc? 

 

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

I actually would like to create a text box for a print proof file that lists things like the file name, any USED swatches in the document, trim size...that sort of thing.

And that text box would be in a slug area outside of the trim box.

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

I think something like this should work. Haven't tested since my machine is tied up running a big process. Let me know if any bugs. 

 

var doc = app.activeDocument;
var swatches = doc.swatches;
var i = swatches.length;
var s = "Swatches Used: ";
var a = [];
while (i-- && i > 0) {
    app.findColorPreferences.findWhat = swatches[i];
    if (doc.findColor() > 0) {
         a.push(swatches[i].name);
    } 
}
a.sort();
s+= a.join(", ");
var tv;
try {
    tv = doc.textVariables.itemByName("Swatches");
    tv.variableOptions.contents = s;
} catch(e) {
    tv = doc.textVariables.add({
        variableType: VariableTypes.CUSTOM_TEXT_TYPE,
        name: "Swatches"
    });
    tv.variableOptions.contents = s;
}

 

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

Thank you so much for your help.

I am in Indesign 2022

Getting this error

 

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

Edited the code. Try again.

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022
 
Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

Getting another line of error. Almost there!

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

Updated the code. Should know better than to write without testing. Tried it on my other machine and it worked. 

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

Thanks for helping me. I have no clue what I am doing really.

I ran it an nothing appears on my Indesign page.

 

I put it in my scripts folder.

 

Am I doing it correctly?

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

It should have created a text variable called Swatches for you. The other info you requested could be easily filled out. 

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

Oh silly me. It did! That is pretty awesome.

Can you give me an example line where I could tell it to NOT include certain swatches like Paper or White?

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

you seem very knowledgeable about javascript.

I have a cool script I found but it uses the old jsxbin file.

I have no clue how to even read it to allow me to edit it. Do you?

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

jsxbin is binary, meaning the author intentionally encoded the code so as to not allow it to be edited. 

If you want to exclude the top 4 swatches in your list (None, Paper, White, Black), change the line that says 

i-- && i > 0

to 

i-- && i > 3

 

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

So I have this one that adds the text to the box rather than having to create the text variable each time.

Is it possible to do it this way so I can just run the javascript each time I open a doc?

The text variable does not stay if I open a new doc

 

var doc = app.activeDocument;
var label = "";

with (doc.documentPreferences) {
label += "W x H: " + pageWidth + " x " + pageHeight;
}

var tf = app.selection[0];
tf.contents = label;

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

 

var doc = app.activeDocument;
var swatches = doc.swatches;
var i = swatches.length;
var s = "Swatches Used: ";
var a = [];
while (i-- && i > 3) {
    app.findColorPreferences.findWhat = swatches[i];
    if (doc.findColor() > 0) {
         a.push(swatches[i].name);
    } 
}
a.sort();
s+= a.join(", ");
var label = "";
with (doc.documentPreferences) {
    label += "W x H: " + pageWidth + " x " + pageHeight + "; " + s;
}
var tf = app.selection[0];
tf.contents = label;

 

Translate
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
Explorer ,
Jul 15, 2022 Jul 15, 2022

Sorry. I may have confused things. I am not sure what the last script is supposed to do.

Your original script is nice, but I do not want to have to run it to create a text variable in the menu each time I open a doc. I need a script that can just be saved and run to fill in the used swatches in a document.

 

The end goal is to have something that can be used over and over to add a "label" of file content in the slug line each time I prepare a print file.

Translate
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 ,
Jul 15, 2022 Jul 15, 2022

The last script would add the used swatch list after the page height width and fill a selected text frame with that information, like the original script you posted does, jusr with swatches, too. 

Translate
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
Explorer ,
Jul 15, 2022 Jul 15, 2022

Getting this error. Thank you in advance

Translate
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 ,
Jul 15, 2022 Jul 15, 2022

You need to have a text frame selected, like you had to do with your pageHeight/width script. 

Translate
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
Explorer ,
Jul 15, 2022 Jul 15, 2022

Duh. Sorry about that.

thank you so much! I think this will work perfectly!!!!

Translate
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 ,
Jul 15, 2022 Jul 15, 2022

Sure. As a bit of future advice, please describe your problem more clearly next time and what you are trying to achieve. The title asked for a Text Varaiable as swatches. Posting that source code at the onset would have helped get you the solution more expeditiously. We are just volunteers. Have a good weekend. 

Translate
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
Explorer ,
Jul 15, 2022 Jul 15, 2022
LATEST

I appreciate the help.

In my head it made sense to me. Sometimes hard to describe.

Have a great weekend too.

Translate
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 ,
Jul 14, 2022 Jul 14, 2022

Hi @denisep726548 ,

why do you run that script from a Version 6.0 Scripts folder?

That would reset and restrict all scripting features to InDesign CS4 from about 14 years ago.

Do not do that with a recent version of InDesign and script code that is meant for that version.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Translate
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
Explorer ,
Jul 14, 2022 Jul 14, 2022

thanks

Translate
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