How to remove Unused Object Stlyes Via JavaScript

Engaged ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

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 

TOPICS
Scripting

Views

115

Likes

translate

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 , 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. 

Likes

translate

Translate

Translate
Enthusiast ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

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

Likes

translate

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

Copy link to clipboard

Copied

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

Likes

translate

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

Copy link to clipboard

Copied

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. 

Likes

translate

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

Copy link to clipboard

Copied

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

Likes

translate

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