Skip to main content
Inspiring
November 14, 2017
Answered

How to unset a characterStyle characterAttribute?

  • November 14, 2017
  • 2 replies
  • 1611 views

I am writing a script that will replace one Character Style with another:

  • OldStyle - size: 40pt · Font Family: Verdana · Font Style: Italic
  • NewStyle - Font Family: Comic Sans · Font Style: Bold

It was easy to apply the properties of the new style to the old style.

But I cannot find out how to remove the Size: 40pt from OldStyle. I have tried:

oldStyle['size'] = false;     // invalid text range

oldStyle['size'] = undefined; // numeric value expected

oldStyle['size'] = null;      // numeric value expected

oldStyle.characterAttributes['size'] = false;     //numeric value expected

oldStyle.characterAttributes['size'] = undefined; //numeric value expected

oldStyle.characterAttributes['size'] = null;      //numeric value expected

delete oldStyle.characterAttributes['size']; // no error but didn't work

delete oldStyle['size'];                     // no error but didn't work

oldStyle['size'].remove();

// oldStyle['size'].remove is not a function

oldStyle.characterAttributes['size'].remove();

// oldStyle.characterAttributes['size'].remove is not a function

oldStyle.characterAttributes['size'].remove;

// oldStyle.characterAttributes['size'].remove is not a function

oldStyle.characterAttributes['size'] = newStyle.characterAttributes['size'];

// the requested attribute is undefined for the text range

oldStyle['size'] = newStyle['size'];

// the requested attribute is undefined for the text range

I can't think of anything else to try, but I still need to remove the "size" property from the NewStyle style definition.

Anybody got any ideas?

This topic has been closed for replies.
Correct answer Andy Swift

Are you trying to simply copy the style from document 1 into document 2?

Can you create a graphic style based on the source text and then apply that graphic style to the destination text?


The answer is:

   characterStyle.clear();

You can clear the old Character Style then import the CharacterAttributes of the new style.

There are many aspects of ExtendScript that are not documented in the Adobe Scripting Reference.

To see them, open the ExtendScript Toolkit application (available for free from Adobe) and click Help › Object Model Viewer

For example:

- under Browser in the top left corner choose Adobe Illustrator XX Type Library

- under Classes scroll down to CharacterStyle

- under Properties and Methods, clear() is listed as method (it's not in the PDF reference)

The definition (on the right) is Remove all the attributes from this character style.

2 replies

Inspiring
November 14, 2017

I can't figure out how to edit my question but here is the actual program:

// copy attributes of "sourceCS" to "destinationCS"

// and delete attributes in "destinationCS" that are

// not present in "sourceCS"

for (var attrbt in sourceCS.characterAttributes {

  // new style has this attribute

  try{ thisattrbt = sourceCS.characterAttributes[attrbt]; }

  // new style does not have this attribute

  catch(err){ thisattrbt = null; }

  if (thisattrbt){

    // attribute exists so add it to destinationCS

    destinationCS.characterAttributes[attrbt] = thisattrbt

  } else {

  // attribute does not exist, need to make

    // sure it does not exist in destinationCS

    // this is the line I can't make work

    destinationCS.characterAttributes[attrbt].remove();

  }

}

Disposition_Dev
Legend
November 14, 2017

oldStyle.characterAttributes['size'] = 0;

perhaps?

Though i'm inclined to say that size is a necessary property of a character style, because text must have a size. Are you really trying to completely nullify that property, or just set it back to the default value?

Inspiring
November 14, 2017

I tried that and got "Error: invalid text range" (presumably because you can't create 0pt text).

The "size" attribute is just for the example. It could just as easily be Font Family or color.

Specifically, the attribute "size" is not necessary. The reason why I need to unset it is because it is undefined in the replacement style.

You can see in the Character Styles palette if you look at Character Style Options for a given style, that you can specify all or very few of the attributes.

Those that are not specified have a strange status in Javascript. They're not false, null or undefined, and any reference to a non-specified attribute throws an error.

Even trying to discover if a given attribute is set throws an exception if it's not.

The only way I could even tell was to use try {... code ...} catch(err) {... code ...}.

Disposition_Dev
Legend
November 14, 2017

can you share some examples of the data that you're working with? like an example of the sourceCS object and the destinationCS object you're trying to update?