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

How to remove Unused Object Stlyes Via JavaScript

Enthusiast ,
Aug 20, 2020 Aug 20, 2020

Hi Pros..

I tried to Write this script to remvoe Unused Object Styles but it not working!, i dont know why, maybe i miss something..

//Remove Unused Object Styles
removeUnusedObjStyles();

function removeUnusedObjStyles() {
	var ObjStyles, found,
	doc = app.activeDocument,
	ObjStyles = doc.objectStyles;
	for (var i = ObjStyles.length - 1; i >= 1; i--) { // skip default style: [None]
		try {
			ObjStyles = ObjStyles[i];
            app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
            app.findObjectPreferences.appliedObjectStyles = ObjStyles;
			found = doc.findObject();
			if (found.length == 0) {
				ObjStyles.remove();
			}
		}
		catch(err) {
            var g
			g.WriteToFile(doc.name + " - " + err.message + ", line: " + err.line);
		}		
	}
    app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
}   

Please Help fixing the code

Best

medo 

Best
Mohammad Hasanin
TOPICS
Scripting
774
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

Community Expert , Aug 20, 2020 Aug 20, 2020

You're setting ObjStyles to the iterated style within the loop (ObjStyles = ObjStyles[i]). This is probably throwing things off. Give the individual element its own variable. 

Translate
Advisor ,
Aug 20, 2020 Aug 20, 2020

Hello.

Your code seems to work fine except for what's shown below,  I'm not sure what you're trying to achieve with this.

 

var g
g.WriteToFile(doc.name + " - " + err.message + ", line: " + err.line);

 

Regards,

Mike

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 ,
Aug 21, 2020 Aug 21, 2020
LATEST

it showing error if happend but i get rid of it, thank you

Best
Mohammad Hasanin
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
Community Expert ,
Aug 20, 2020 Aug 20, 2020

You're setting ObjStyles to the iterated style within the loop (ObjStyles = ObjStyles[i]). This is probably throwing things off. Give the individual element its own variable. 

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 ,
Aug 21, 2020 Aug 21, 2020

Thank you very much,, now the code working, the final code is :

//Remove Unused Object Styles
removeUnusedObjStyles();

function removeUnusedObjStyles() {
	var ObjStyles, found,
	doc = app.activeDocument,
	ObjStyles = doc.objectStyles;
	for (var i = ObjStyles.length - 1; i >= 1; i--) { // skip default style: [None]
            app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
            app.findObjectPreferences.appliedObjectStyles = ObjStyles[i];
			found = doc.findObject();
			if (found.length == 0) {
				ObjStyles[i].remove();
		}		
	}
    app.findObjectPreferences = app.changeObjectPreferences = NothingEnum.NOTHING;
}   

 Best

medos20

Best
Mohammad Hasanin
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