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)
});
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 )
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 )
Copy link to clipboard
Copied
Thanks Laubender
this works well
I work in visual studio code
Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
Thanks for the info Manan
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]})
}