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

Create list of character Styles

Explorer ,
Feb 01, 2022 Feb 01, 2022

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)
	});

 

TOPICS
Scripting
480
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 1 Correct answer

Community Expert , Feb 01, 2022 Feb 01, 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 )

Translate
Community Expert ,
Feb 01, 2022 Feb 01, 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 )

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 ,
Feb 01, 2022 Feb 01, 2022

Thanks Laubender

 

this works well
I work in visual studio code

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
Guide ,
Feb 03, 2022 Feb 03, 2022
LATEST

Hi @Laubender 

 

Actually there is a for each loop in ExtendScript (although most scripters didn't notice or don't use it.)

 

var namesArray = [ "Bold", "Italic", "A", "B", "C" ];
var CS = app.documents[0].characterStyles;
var nm;

for each( nm in namesArray ) CS.add({ name : nm });

 

Works fine with Object instances and regular Array instances. (By ‘regular,’ I mean an array whose own properties are indexes, no extra keys being declared.)

 

Best,

Marc

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 ,
Feb 01, 2022 Feb 01, 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

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 ,
Feb 01, 2022 Feb 01, 2022

Thanks for the info Manan

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 ,
Feb 01, 2022 Feb 01, 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]})
}

 

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