Highlighted

How to remove Unused Object Stlyes Via JavaScript

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

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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. 

Topics

Scripting

Views

55

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

How to remove Unused Object Stlyes Via JavaScript

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

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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. 

Topics

Scripting

Views

56

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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
medos20 LATEST
Engaged ,
Aug 21, 2020

Copy link to clipboard

Copied

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

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
Reply
Loading...
Adobe Community Professional ,
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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...