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

Remove unused style

Engaged ,
Apr 29, 2015 Apr 29, 2015

Copy link to clipboard

Copied

Hi All

I am using the below code to remove the unused style form the application file

But it's removing all GREP style mapped character style

How to fix this issue please advice

for (i = myCharStyles.length-1; i >= 1; i-- ) { 

   removeUnusedCharStyle(myCharStyles); 

}

function removeUnusedCharStyle(myChStyle) { 

   app.findTextPreferences = NothingEnum.nothing; 

   app.changeTextPreferences = NothingEnum.nothing; 

   app.findTextPreferences.appliedCharacterStyle = myChStyle; 

   var myFoundStyles = myDoc.findText(); 

      if (myFoundStyles == 0) { 

         myChStyle.remove(); 

      } 

   app.findTextPreferences = NothingEnum.nothing; 

   app.changeTextPreferences = NothingEnum.nothing; 

}

TOPICS
Scripting

Views

319

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
Contributor ,
May 01, 2015 May 01, 2015

Copy link to clipboard

Copied

LATEST

Hi,

try the following snippet. (test on cs5)

you can get appliedCharacterStyles' id in all NestedXXXStyles as Array.

on removing character styles, you can exclude these styles by ids.

thank you.

var used_charstyle_ids_in_nestedstyle = function (parastyles) {

 

  var ret = [];

  for (var i=0, len=parastyles.length; i < len ; i++) {

   var used_c_style_ids = [];

   var p_style = parastyles;

  

   var ng_styles = p_style.nestedGrepStyles;

   var nl_styles = p_style.nestedLineStyles;

   var n_styles  = p_style.nestedStyles;

  used_c_style_ids = used_c_style_ids.concat(used_charstyle_ids(ng_styles));

  used_c_style_ids = used_c_style_ids.concat(used_charstyle_ids(nl_styles));

  used_c_style_ids = used_c_style_ids.concat(used_charstyle_ids(n_styles));

  

  ret = ret.concat(used_c_style_ids);

  };

  return ret

}

var used_charstyle_ids = function (x_styles) {

 

  var ret = [];

  for (var i=0, len=x_styles.length; i < len ; i++) {

   var c_style = x_styles.appliedCharacterStyle;

  ret.push(c_style.id);

  };

  return ret

}

var is_included = function (item, list) {

  for (var i=0, len=list.length; i < len ; i++) {

   if (list === item) {

   return true

  }

  };

  return false

}

// main

var doc = app.documents[0];

$.writeln(all_used_charstyle_ids(doc));

function all_used_charstyle_ids(doc) {

 

  var no_charstyle_id = doc.characterStyles.item("$ID/[No Character Style]").id;

  var p = doc.allParagraphStyles;

  var ids = used_charstyle_ids_in_nestedstyle(p).concat(no_charstyle_id);

  return ids

}

Votes

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