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

Creating an object style with a script

Guest
May 06, 2010 May 06, 2010

I'm slowly teaching myself scripting, but my first script has a bug I can't seem to fix. Part of it creates an object style called "plain text" that has certain attributes. One of those attributes is that the first baseline offset should be "Fixed" at a certain leading. When I run the script in InDesign CS3, sometimes it works, and the first baseline offset is "Fixed", but sometimes it doesn't work, and the first baseline offset remains at the default value of "Ascent." I need it to work in InDesign CS4, too, but it doesn't work there at all. I've looked through the DOM, but it looks like everything is coded properly.

Below is a portion of the script. What am I missing?

//Creating the "plain text" object style.
var myPlainTextObjectStyle = myDocument.objectStyles.add();
with(myPlainTextObjectStyle){
name = "plain text";
enableFill = true;
enableStroke = true;
enableStrokeAndCornerOptions = false;
enableTextFrameBaselineOptions = true;
enableTextFrameGeneralOptions = true;
fillColor = myDocument.swatches.item(0);
strokeColor = myDocument.swatches.item(0);
textFramePreferences.firstBaselineOffset.firstBaseline = FirstBaseline.FIXED_HEIGHT;
textFramePreferences.minimumFirstBaselineOffset = myLeading;
textFramePreferences.textColumnCount = 1;
textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
}

TOPICS
Scripting
2.8K
Translate
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

Advocate , May 06, 2010 May 06, 2010

Your problem is that you have an extra "firstBasline" in the line that sets the firstBaselineOffset. Your script should read:

//Creating the "plain text" object style.
var myPlainTextObjectStyle = myDocument.objectStyles.add();
with(myPlainTextObjectStyle){
name = "plain text";
enableFill = true;
enableStroke = true;
enableStrokeAndCornerOptions = false;
enableTextFrameBaselineOptions = true;
enableTextFrameGeneralOptions = true;
fillColor = myDocument.swatches.item(0);
strokeColor = myDocument.swatches.it

...
Translate
Advocate ,
May 06, 2010 May 06, 2010

Your problem is that you have an extra "firstBasline" in the line that sets the firstBaselineOffset. Your script should read:

//Creating the "plain text" object style.
var myPlainTextObjectStyle = myDocument.objectStyles.add();
with(myPlainTextObjectStyle){
name = "plain text";
enableFill = true;
enableStroke = true;
enableStrokeAndCornerOptions = false;
enableTextFrameBaselineOptions = true;
enableTextFrameGeneralOptions = true;
fillColor = myDocument.swatches.item(0);
strokeColor = myDocument.swatches.item(0);
textFramePreferences.firstBaselineOffset = FirstBaseline.FIXED_HEIGHT;
textFramePreferences.minimumFirstBaselineOffset = myLeading;
textFramePreferences.textColumnCount = 1;
textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
}

Although, there's a better syntax you could be using:

//Creating the "plain text" object style.
var myPlainTextObjectStyle = myDocument.objectStyles.add();
myPlainTextObjectStyle.properties = {
     name : "plain text",
     enableFill : true,
     enableStroke : true,
     enableStrokeAndCornerOptions : false,
     enableTextFrameBaselineOptions : true,
     enableTextFrameGeneralOptions : true,
     fillColor : myDocument.swatches.item(0),
     strokeColor : myDocument.swatches.item(0),
     textFramePreferences : {
          firstBaselineOffset : FirstBaseline.FIXED_HEIGHT,
          minimumFirstBaselineOffset : myLeading,
          textColumnCount : 1,
          verticalJustification : VerticalJustification.TOP_ALIGN
     }
}

What makes this better is that there is just the one interaction with the object model rather than one for every property of the object style.

Dave

Translate
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
Enthusiast ,
May 06, 2010 May 06, 2010

or maybe with only 1 interaction

var props = {      name : "plain text",      enableFill : true,      enableStroke : true,      enableStrokeAndCornerOptions : false,      enableTextFrameBaselineOptions : true,      enableTextFrameGeneralOptions : true,      fillColor : myDocument.swatches.item(0),      strokeColor : myDocument.swatches.item(0),      textFramePreferences : {           firstBaselineOffset : FirstBaseline.FIXED_HEIGHT,           minimumFirstBaselineOffset : myLeading,           textColumnCount : 1,           verticalJustification : VerticalJustification.TOP_ALIGN      } } var myPlainTextObjectStyle = myDocument.objectStyles.add(props);

Translate
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
Guest
May 06, 2010 May 06, 2010

Thanks for your help! That solved it. I'll try to keep those streamlining methods in mind.

Translate
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
New Here ,
Apr 11, 2016 Apr 11, 2016
LATEST

Hello Dave,

This is very nice to read. But I am facing a problem with the script. My requirement is to color the logo text.  Logo files are either .eps or .tiff files and have a text on them. I need to colorize the text which is on Image file before exporting to pdf using Indesign server. Could you please help me out. Below is the script we are using,

function imageColors(myDoc, iName, iColor, curTemplate) {

  var isColorized = false;

    try {

       // logToCon("in imageColors ::::::: "+iName);

        changeColors(myDoc, iName, iColor, curTemplate);

        var myObjectStyle = myDoc.objectStyles.item(iName);

  //logToCon("in myObjectStyle ::::::: "+myObjectStyle);

  

        try {

           

            var myName = myObjectStyle.name;

  //logToCon("in myObjectStyle name Before catch statement::::::: "+myName);

        }

        catch (e) {

            //The object style did not exist, so create it.

            myObjectStyle = myDoc.objectStyles.add({ name: iName, fillColor: iName, strokeColor :iName });

        }

  //for(var i = 0; i < myDoc.objectStyles.length; i++){ 

  // logToCon("myDocument.objectStyles["+i+"].name  "+myDoc.objectStyles.name);

  

    //}

  for(var i = 0; i < myDoc.allPageItems.length; i++){ 

      if ( myDoc.allPageItems.label == iName) {

                myDoc.allPageItems.applyObjectStyle(myObjectStyle, true, true);

  //myDoc.allPageItems.fillColor = iName;

  isColorized = true;

            }

  }

    

    } catch (e) {

        updateResponse("code4", "Error while processing template " + curTemplate + " for imageColors for Name " + iName + " for color " + iColor + ". Error Message: " + e.message, -1);

    }

    return isColorized;

}@@@@

Translate
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