-
Notifications
You must be signed in to change notification settings - Fork 9
Styles
Fabian Morón Zirfas edited this page Jun 13, 2025
·
3 revisions
Create styles with HTML tag names from an object. The object matches the InDesign DOM. So you can pass them to the paragraphStyles.add() method directly.
//Wrap it up in an anonymous function
(function (thisObj) {
// paragraph style object
// these will be passed directly to the paragraphStyles.add() method
// so only use property names that exist in the ID DOM
var parstyles = [
{
name: "p",
appliedFont: "Museo Slab\t300",
},
{
name: "h1",
},
{
name: "h2",
},
{
name: "h3",
},
{
name: "h4",
},
{
name: "h5",
},
{
name: "h6",
},
{
name: "quote",
},
];
// same here for the character styles
var charstyles = [
{
name: "em",
},
{
name: "bold",
},
{
name: "link",
},
];
var doc = app.activeDocument; // current front most document
var prevp = null; // the get the based on working
// loop the parsytles
for (var i = 0; i < parstyles.length; i++) {
// check if there is already a style with that name
var pexists = doc.paragraphStyles.itemByName(parstyles[i].name);
if (pexists !== null) {
// there was one set it back to null and skip this one
pexists = null;
continue; //<-- This skips
}
// there was no style with this name. So create it
var p = doc.paragraphStyles.add(parstyles[i]);
if (prevp !== null) {
// if we already have a previous style set the basedOn property
p.basedOn = prevp;
}
prevp = p; // <-- set current to previous
}
// same here for the character styles
// but we don't set the basedOn feature
for (var j = 0; j < charstyles.length; j++) {
var cexists = doc.characterStyles.itemByName(charstyles[j].name);
if (cexists !== null) {
cexists = null;
continue;
}
var c = doc.characterStyles.add(charstyles[j]);
}
})(this);Style your text with nested styles. This script file needs to be saved to work.
/**
* With styles you can controll your text
*/
// a function
// this script has to be saved somewhere
// or this wont work
main();
function main() {
// make a doc
var pw = 210;
var ph = 500;
var doc = app.documents.add({
documentPreferences: {
pageWidth: pw,
pageHeight: ph,
},
});
// build a style with nested grep styles
var code = build_code_paragraphStyle(doc);
// get the page
var page = doc.pages[0];
// make a frame
var tf = page.textFrames.add({ geometricBounds: [10, 10, ph - 10, pw - 10] });
// load the content of the script
var scriptfile = File($.fileName);
alert(scriptfile);
var content;
if (scriptfile != false) {
scriptfile.open("r");
content = scriptfile.read();
} else {
alert("Bah!");
}
// add the content to the frame
tf.contents = content;
scriptfile.close(); // always close files after reading
// apply the style
tf.paragraphs.everyItem().appliedParagraphStyle = code;
}
// build a lot of styles
function build_code_paragraphStyle(d) {
var charStyles = new Array();
var keywords = d.characterStyles.add({
name: "keywords",
fillColor: d.swatches.item(5),
});
var comments = d.characterStyles.add({
name: "comments",
fillColor: d.swatches.item(3),
fillTint: 70,
});
var operators = d.characterStyles.add({
name: "operators",
fillColor: d.swatches.item(7),
});
var separators = d.characterStyles.add({
name: "separators",
fillColor: d.swatches.item(8),
});
var numbers = d.characterStyles.add({
name: "numbers",
fillColor: d.swatches.item(9),
});
var comment = d.characterStyles.add({
name: "comment",
fillColor: d.swatches.item(9),
});
var string = d.characterStyles.add({
name: "strings",
fillColor: d.swatches.item(5),
});
var code = d.paragraphStyles.add({
name: "code",
appliedFont: "Andale Mono",
pointSize: 10,
});
//change language (only in the paragraphStyle) to get the right "" for the code
code.appliedLanguage = app.languagesWithVendors.item("English: USA");
// do some nested grep styles
var grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = keywords;
grp.grepExpression =
"abstract|boolean|break|byte|case|catch|char\ |class|const|continue|debugger|default|delete|do\ |double|else|enum|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in\ |instanceof|int\ |interface|long|native|new|n ull|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|wh ile|with";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = operators;
grp.grepExpression = "is|new|sizeof|typeof";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = operators;
grp.grepExpression = "[-~\\[\\]!$%&*+/:<=>?^|]+";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = separators;
grp.grepExpression = "[(){},;\\s]+";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = numbers;
grp.grepExpression = "\\<[0-9]+(\\.[0-9]+)?([eE][-+]?[0-9]+)?";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = comments;
grp.grepExpression = "/\\*+[^*]*\\*+([^/*][^*]*\\*+)*/";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = comment;
grp.grepExpression = "//.*";
grp = code.nestedGrepStyles.add();
grp.appliedCharacterStyle = string;
grp.grepExpression = '".*?"';
return code;
}This wiki is mostly maintained by:
ff6347
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
- EugenTepin for several improvements.
- vamitul for improvements.
- abandonedbywolves for adding an example.
- Travis Weston
- Andy Dayton
Thanks to the students from my seminars for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- Executing Shell Commands
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read CSV
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- Isolate Layers With the Shy Setting
- Layer Duration Change
- Render And System Call
- Rename Layers
- SourceText to Keyframes from Array
- SourceText
- System Call
- app
- Colorbrewer
- ColorGroups
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Locked PageItems
- loops vs everyItem
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Scripting Labels
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Table Cells
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import