Skip to content
fabiantheblind edited this page Nov 19, 2015 · 3 revisions

###Styles

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;        
            }   
Clone this wiki locally