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

Indesign Script for swatches and object style's names

Community Beginner ,
Nov 27, 2019 Nov 27, 2019

Copy link to clipboard

Copied

Hi! I would like to add a suffixe (with a prompt) to all the swatches and object style's name of a document. I do that to the style of character, paragraphes, cell and table but, but i'm not able for these two things...

 

Here is the «script» that i tried for the swatches :

 

var before = prompt("Entrer le suffixe voulu", "_");

var myDoc = app.activeDocument;

var mySwatches = myDoc.swatches;

for (var s = 2; i < mySwatches.length; s++){

var mySwatch = myDoc.swatches[s];

var name = mySwatch.name;

var myColorValueArray = before + name; }

 

And here the script that work for the other style:

 

var allParaStyles = app.activeDocument.allParagraphStyles;

var allCharStyles = app.activeDocument.allCharacterStyles;

var allTablStyles = app.activeDocument.allTableStyles;

var allCellStyles = app.activeDocument.allCellStyles;

 

var before = prompt("Entrer le suffixe voulu", "_");

 

for (var i = 2; i < allCharStyles.length; i++) {

  var curStyle = allCharStyles[i];

  var curName = curStyle.name;

  if (curName.indexOf(before) == -1) {

    var newName = before + curName;

    curStyle.name = newName;

  }

}

for (var i = 2; i < allParaStyles.length; i++) {

  var curStyle = allParaStyles[i];

  var curName = curStyle.name;

  if (curName.indexOf(before) == -1) {

    var newName = before + curName;

    curStyle.name = newName;

  }

}

for (var i = 2; i < allTablStyles.length; i++) {

  var curStyle = allTablStyles[i];

  var curName = curStyle.name;

  if (curName.indexOf(before) == -1) {

    var newName = before + curName;

    curStyle.name = newName;

  }

}

for (var i = 2; i < allCellStyles.length; i++) {

  var curStyle = allCellStyles[i];

  var curName = curStyle.name;

  if (curName.indexOf(before) == -1) {

    var newName = before + curName;

    curStyle.name = newName;

  }

}

 

alert("Le script a été exécuté avec succès.");

 

Thank you!

 

 

TOPICS
Scripting

Views

847

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 ,
Nov 27, 2019 Nov 27, 2019

Copy link to clipboard

Copied

You are not assigning the new name to mySwatch.name as you do in your working code. Add the following line before the closing braces in your code

mySwatch.name = myColorValueArray

 

-Manan

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 ,
Nov 27, 2019 Nov 27, 2019

Copy link to clipboard

Copied

Thanks for the reply 🙂

THe line you make me add, makes me a error.

error number: 90889. Cannot edit this color property.

Can you help?

And do you have an idea for a script in this kind that will add a suffixe at the name of all object style?

Thanks 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 ,
Nov 27, 2019 Nov 27, 2019

Copy link to clipboard

Copied

Try the following code

var before = prompt("Entrer le suffixe voulu", "_");
var myDoc = app.activeDocument;
for (var s = 0; s < myDoc.swatches.length; s++)
{
	var mySwatch = myDoc.swatches[s];
	try{
		mySwatch.name = before + mySwatch.name
	}catch(e){}
}

 

The reason it was crashing is that we can't change the name of some of the predefined swatches, for this same reason your code was starting the loop with index 2, but it seems there are more than 2 such swatches or maybe the ordering of swatches returned is not the same everytime(did not test this). So what i did is enlose the statement that changes the name into a try catch block so even if the statement fails for a swatch the code does not crash

 

For object styles just copy this code and change myDoc.swatches with myDoc.objectStyles, it should work

 

-Manan

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 ,
Nov 28, 2019 Nov 28, 2019

Copy link to clipboard

Copied

LATEST

Thanks Manan!!!!! It works perfectly 🙂

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 ,
Nov 28, 2019 Nov 28, 2019

Copy link to clipboard

Copied

Hi Manan,

because you can move around all swatches in the Swatches Panel you cannot rely on the default position of swatches.

And therefore you cannot rely on the index position of [None], [Registration], [Paper], [Black].

 

Default names after index in my German InDesign CC 2019:

 

DefaultSwatches-1.PNG

 

0 None
1 Registration
2 Paper
3 Black
4 C=100 M=0 Y=0 K=0
5 C=0 M=100 Y=0 K=0
6 C=0 M=0 Y=100 K=0
7 C=15 M=100 Y=100 K=0
8 C=75 M=5 Y=100 K=0
9 C=100 M=90 Y=10 K=0

 

Now be prepared for a little surprise.

Especially watch the index of [None]!

Swatches moved around in the panel:

SwatchesMoved-Watch-NONE-1.PNG

Result after index:

0	None
1	C=100 M=0 Y=0 K=0
2	C=0 M=100 Y=0 K=0
3	C=0 M=0 Y=100 K=0
4	C=15 M=100 Y=100 K=0
5	Black
6	Paper
7	C=75 M=5 Y=100 K=0
8	Registration
9	C=100 M=90 Y=10 K=0

 

Another composition:

 

SwatchesMoved-Watch-NONE-2.PNG

 

And the result listed after index:

0	C=100 M=0 Y=0 K=0
1	C=0 M=100 Y=0 K=0
2	None
3	C=0 M=0 Y=100 K=0
4	C=15 M=100 Y=100 K=0
5	Black
6	Paper
7	C=75 M=5 Y=100 K=0
8	Registration
9	C=100 M=90 Y=10 K=0

This time the index of [None] reflects its position in the Swatches Panel.

 

Code to build the list:

var s = app.documents[0].swatches;

for( var n=0; n<s.length; n++ )
{
	$.writeln( n +"\t"+ s[n].name );
}

 

Regards,
Uwe Laubender

( ACP )

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 ,
Nov 28, 2019 Nov 28, 2019

Copy link to clipboard

Copied

Hi Uwe,

 

Thanks for sharing your investigation results, i had anticipated this behaviour that is why i kept my loop starting from 0 and used a try catch block to avoid the code crash. The behaviour of None is interesting though, it seems its random and not something that is following a pattern.

 

-Manan

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