Copy link to clipboard
Copied
I want to determine if the styles below exist before I run another script, and if they don't, then create new ones.
Paragraph style: pa
Character style: ch
Object style: ob
Table style:: mytable
cell style: cel
Thank you very much.
Paragraph style: pa
Hi @dublove , I use this function to get or set a style. This makes a new style named "pa", then I can set the new or existing style’s properties. You can do the same for Character and Object styles
var ps = makeParaStyle(app.activeDocument, "pa");
//set the properties of the style named pa
ps.properties = {basedOn:"[No Paragraph Style]", appliedFont:"Myriad Pro Regular", pointSize:8, leftIndent:0};
/**
* Makes a new named Paragraph Style or get the style named n
* @ Can you tell me the table style, cell object style their names.
You can search the entire scripting API. Here are the ExtendScript document methods and objects:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html
To find proberties there is the document’s cellStyle, tableStyle, paragraphStyle, characterStyle, ObjectStyle.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CellStyle.html#d1e456077
Note for creating a new style you use the plural (i.e., d.cell
...You can also have a universal function by referencing the document's relevant property via a string reference:
var makeStyle = function(doc, styleName, collection) {
doc[collection].add....
}
makeStyle(app.activeDocument, "pa", "paragraphStyles");
Copy link to clipboard
Copied
Depends if styles are in groups, but if not:
if (app.activeDocument.paragraphStyles.itemByName("pa").isValid) { ...
Copy link to clipboard
Copied
Are other style judgments similar?
Copy link to clipboard
Copied
Try it and report back!
Copy link to clipboard
Copied
if(app.documents[0].paragraphStyles.item("pa").isValid)
{var a=6;}
else{
app.activeDocument.paragraphStyles.add("pa");
}Copy link to clipboard
Copied
if(app.activeDocument.paragraphStyles.itemByName("pa").isValid)
{var a=6;}
else{
app.activeDocument.paragraphStyles.add({name:"pa"});
}There's a parenthesis missing.
Copy link to clipboard
Copied
What advantage do you expect with scripting styles? What should the script do what you can't do without scripting?
Copy link to clipboard
Copied
Paragraph style: pa
Hi @dublove , I use this function to get or set a style. This makes a new style named "pa", then I can set the new or existing style’s properties. You can do the same for Character and Object styles
var ps = makeParaStyle(app.activeDocument, "pa");
//set the properties of the style named pa
ps.properties = {basedOn:"[No Paragraph Style]", appliedFont:"Myriad Pro Regular", pointSize:8, leftIndent:0};
/**
* Makes a new named Paragraph Style or get the style named n
* @ param the document to add the style to
* @ param style name
* @ returns the new paragraph style
*/
function makeParaStyle(d, n){
var ps;
try {
d.paragraphStyles.add({name:n});
}catch(e) {
ps = d.paragraphStyles.itemByName(n);
}
return d.paragraphStyles.itemByName(n);
}Copy link to clipboard
Copied
This one's good.
Thank you very much.
Can you tell me the table style, cell object style their names.
I went through so many scripts written by others to get to the last sentence.
Copy link to clipboard
Copied
Can you tell me the table style, cell object style their names.
You can search the entire scripting API. Here are the ExtendScript document methods and objects:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html
To find proberties there is the document’s cellStyle, tableStyle, paragraphStyle, characterStyle, ObjectStyle.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CellStyle.html#d1e456077
Note for creating a new style you use the plural (i.e., d.cellStyles.add())
Here is a makeCellStyle function:
var cs = makeCellStyle(app.activeDocument, "My Cell Style");
//set the properties of the style named cs
cs.properties = {fillColor:"Black"};
/**
* Makes a new named Cell Style or get the style named n
* @ param the document to add the style to
* @ param style name
* @ returns the new cell style
*/
function makeCellStyle(d, n){
var cs;
try {
d.cellStyles.add({name:n});
}catch(e) {
cs = d.cellStyles.itemByName(n);
}
return d.cellStyles.itemByName(n);
}
Copy link to clipboard
Copied
You can also have a universal function by referencing the document's relevant property via a string reference:
var makeStyle = function(doc, styleName, collection) {
doc[collection].add....
}
makeStyle(app.activeDocument, "pa", "paragraphStyles");
Copy link to clipboard
Copied
Hi @brian_p_dts , Thanks that works great. I think @dublove , still would need to check if the style already exists (I get an error running more than once) so maybe this?
makeStyle(app.activeDocument, "pa", "paragraphStyles");
/**
* Make a new collection
* @ param the document
* @ param the name
* @ param the collection class
* @ return the named collection
*/
function makeStyle(d, n, collection) {
if (d[collection].itemByName(n).isValid) {
return d[collection].itemByName(n);
} else {
return d[collection].add({name:n})
}
}Copy link to clipboard
Copied
Hi brian_p_dts rob day
Thank you very much.
That's enough.
I actually had enough with just this form:
if(app.activeDocument.paragraphStyles.itemByName("pa").isValid)
{var a=6;}
else{
app.activeDocument.paragraphStyles.add({name:"pa"});
}
I'm actually still stuck at the level of thinking of one command in one sentence.
Copy link to clipboard
Copied
I'm actually still stuck at the level of thinking of one command in one sentence.
Then you shouldn’t need the if else, you can do if not (! before the statement):
if(!app.activeDocument.paragraphStyles.itemByName("pa").isValid) {
app.activeDocument.paragraphStyles.add({name:"pa"})
}
Copy link to clipboard
Copied
Another trick I learned.
Thank you,
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more