Skip to main content
Inspiring
February 1, 2022
Answered

Create list of character Styles

  • February 1, 2022
  • 2 replies
  • 637 views

Hello,

 

I want to create a script that creates a whole series of character Styles but I get stuck on line 11 " myStyles.forEach((styleName => {"
can someone help me

thank you

 

var new_Style = app.documents[0].characterStyles.add()
 
	const myStyles = [
		'Bold',
		'Italic',
		'A',
		'B',
		'C',
	];
	
	myStyles.forEach((styleName => {
		new_Style.name =(styleName)
	});

 

This topic has been closed for replies.
Correct answer Laubender

Are you writing this script in ExtendScript?

There is no native function forEach() in ExtendScript.

 

Why don't you simply loop the array with the names?

 

var namesArray = [ "Bold", "Italic", "A", "B", "C" ];

for( var n=0; n<namesArray.length; n++ )
{
	app.documents[0].characterStyles.add
	(
		{
			name : namesArray[n]
		}
	)
};

 

You could also add more property-value pairs in the object that is the argument of the add() function.

 

Regards,
Uwe Laubender

( ACP )

2 replies

Community Expert
February 1, 2022

Hi @lucc9908231,

Extendscript is based on ECMAScript 3 so modern features like for each, arrow functions etc are not available. You need to use simple constructs like traditional for loop as @Laubender mentioned.

-Manan

Inspiring
February 1, 2022

Thanks for the info Manan

rob day
Community Expert
February 1, 2022

There’s also for in:

 

var namesArray = [ "Bold", "Italic", "A", "B", "C" ];

for (x in namesArray) {
    app.documents[0].characterStyles.add({name:namesArray[x]})
}

 

LaubenderCorrect answer
Community Expert
February 1, 2022

Are you writing this script in ExtendScript?

There is no native function forEach() in ExtendScript.

 

Why don't you simply loop the array with the names?

 

var namesArray = [ "Bold", "Italic", "A", "B", "C" ];

for( var n=0; n<namesArray.length; n++ )
{
	app.documents[0].characterStyles.add
	(
		{
			name : namesArray[n]
		}
	)
};

 

You could also add more property-value pairs in the object that is the argument of the add() function.

 

Regards,
Uwe Laubender

( ACP )

Inspiring
February 1, 2022

Thanks Laubender

 

this works well
I work in visual studio code