Get Empty characterStyles

Community Beginner ,
Mar 24, 2022 Mar 24, 2022

Copy link to clipboard

Copied

Hi Everyone,

 

I need to get empty characterStyles list through scripting. Which the characterstyles defined with nothing except a name of it. Is it possible find it.

Thanks in advance if anyone can help.

 

TOPICS
Scripting

Views

120

Likes

Translate

Translate

Report

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

Adobe Community Professional , Mar 28, 2022 Mar 28, 2022
Manan's method works on all types of style (I guess), but if you're interested in just character styles you can in fact do a version-independent script. Simply compare the properties of any given style to those of the base character style (characterStyles[0]), ignoring properties which must be different (name, id, index, etc.). // Compare styles to the [None] style var baseProperties = app.documents[0].characterStyles[0].properties; // The following properties should be ignored when doing ...

Likes

Translate

Translate
Adobe Community Professional ,
Mar 24, 2022 Mar 24, 2022

Copy link to clipboard

Copied

I did not find any direct method to do this. An indirect method would be to iterate all the properties and check if they are set or not. However, this method would not be an ideal appraoch and would need constant code updations with the changes/additions to the properties across different InDesign versions.

I looked into another method of parsing this information out of IDML and this seems worth investigating. In IDML the character style defintion includes only the properties that are changed from the default, so you just need to parse out all the character style definition and check if it has any properties added in addtion to the common ones. As an example I created two Character Styles, one with no changes and other with changes to two properties namely position and point size. Check the entries of these in the IDML

Style with no changes

 

<CharacterStyle Self="CharacterStyle/CS1" Imported="false" SplitDocument="false" EmitCss="true" StyleUniqueId="e204ef8b-ecbf-45fc-8d23-91d8af82d400" IncludeClass="true" ExtendedKeyboardShortcut="0 0 0" KeyboardShortcut="0 0" Name="CS1">
	<Properties>
		<BasedOn type="string">$ID/[No character style]</BasedOn>
		<PreviewColor type="enumeration">Nothing</PreviewColor>
	</Properties>
</CharacterStyle>

 

Style with changes to postion and pointsize property

 

<CharacterStyle Self="CharacterStyle/CS2" Imported="false" SplitDocument="false" EmitCss="true" StyleUniqueId="525af245-a0b5-4af1-83eb-1343f02e4c0c" IncludeClass="true" ExtendedKeyboardShortcut="0 0 0" KeyboardShortcut="0 0" Name="CS2" PointSize="14" Position="Superscript">
	<Properties>
		<BasedOn type="object">CharacterStyle/CS1</BasedOn>
		<PreviewColor type="enumeration">Nothing</PreviewColor>
	</Properties>
</CharacterStyle>

 

So the workflow could be as follows

  • Export the IDML of the document
  • Extract the IDML contents and locate the Styles.xml file
  • Parse the entries for each character style and use the logic discussed above to get your list

-Manan

Likes

Translate

Translate

Report

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
Community Beginner ,
Mar 24, 2022 Mar 24, 2022

Copy link to clipboard

Copied

Hi Manan,

 

Thanks for your valuable response. I will check thae suggested method and get back.🤟🏼

 

Thanks,

Singaaram

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Mar 28, 2022 Mar 28, 2022

Copy link to clipboard

Copied

Manan's method works on all types of style (I guess), but if you're interested in just character styles you can in fact do a version-independent script. Simply compare the properties of any given style to those of the base character style (characterStyles[0]), ignoring properties which must be different (name, id, index, etc.).

 

 

// Compare styles to the [None] style
var baseProperties = app.documents[0].characterStyles[0].properties;

// The following properties should be ignored when doing 
// the comparison, they're always different

var skip = { 
  'basedOn': true,
  'name': true,
  'previewColor': true,
  'id': true,
  'index': true,
  'styleUniqueId': true
}


// Compare a style's properties with 
// those of the [None] style

function isEmptyStyle (cstyle) {
  var cstyleProperties = cstyle.properties;
  for (var i in cstyleProperties) {
    if (cstyleProperties[i] !== baseProperties[i] && !skip[i]) {
      return false;
    }
  }
  return true;
}

// Compare all character styles to the [None] style
var cstyles = app.documents[0].allCharacterStyles;
var empties = [];
for (var i = 1; i < cstyles.length; i++) {
  if (isEmptyStyle (cstyles[i])) {
    empties.push (cstyles[i].name);
  }
}

if (empties.length > 0) {
  alert ('Empty styles:\r\r' + empties.join('\r'));
} else {
  alert ('No empty styles in this document');
}

 

 

P.

Likes

Translate

Translate

Report

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
Community Beginner ,
Mar 28, 2022 Mar 28, 2022

Copy link to clipboard

Copied

LATEST

Hi @Peter Kahrel,

 

Thank you so much for the code and it works perfectly as needed...🤩

 

Regards,

Singaaram

Likes

Translate

Translate

Report

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