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

How to remove Unused Object Stlyes Via JavaScript

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

Best
Mohammad Hasanin
TOPICS
Scripting

Views

450

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

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. 

Votes

Translate

Translate
Advisor ,
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

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

Copy link to clipboard

Copied

LATEST

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

Best
Mohammad Hasanin

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

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

Best
Mohammad Hasanin

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