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

Create list of character Styles

Explorer ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

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

Views

361

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

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 )

Votes

Translate

Translate
Community Expert ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

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 )

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

Copy link to clipboard

Copied

Thanks Laubender

 

this works well
I work in visual studio 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
Guide ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

Thanks for the info 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 Expert ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

There’s also for in:

 

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

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

 

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