Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
app.findPreferences = NothingEnum.nothing;Wow! Thats a very old script. I don't write them like that any more.
app.changePreferences = NothingEnum.nothing;
for (i=(myStyles.length - 1); i>0; i--)
{
//avoid 1 to ignore No Paragraph Style
//start at end to facilitate immediate deletion
app.findPreferences.appliedParagraphStyle = myStyles;
myFound = app.documents[0].search("",false,false);
if (myFound == "")
{
myStyles.remove();
}
}
Copy link to clipboard
Copied
var myDoc = app.activeDocument;
var myParStyles = myDoc.paragraphStyles;
for (i = myParStyles.length-1; i >= 2; i-- ){
removeUnusedStyle(myParStyles)
}
function removeUnusedStyle(myStyle) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = myStyle;
var myFoundStyles = myDoc.findText();
if (myFoundStyles == 0) {
myStyle.remove();
}
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
}
Copy link to clipboard
Copied
Hi Kasyan,
Here I am again.
I have modified little to your javascript for removing paragraph styles. It is working fine in CS3 and not in CS4. In CS4, in the ExtendScript Toolkit, it is not throwing any error message but nothing is happening in the indesign file. I hope some references might got changed in the later version. Here is the code:
var myDoc = app.activeDocument;
var myParStyles = myDoc.paragraphStyles;
var myCharStyles = myDoc.characterStyles;for (i = myParStyles.length-1; i >= 2; i-- ){
removeUnusedParaStyle(myParStyles)
}
for (i = myCharStyles.length-1; i >= 2; i-- ){
removeUnusedCharStyle(myCharStyles)
}
function removeUnusedParaStyle(myPaStyle) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = myPaStyle;
var myFoundStyles = myDoc.findText();
if (myFoundStyles == 0) {
myPaStyle.remove();
}
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
}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;
}
Could you please advise me what to do to make this script work in CS4? and also where can I find the relevent javascript reference for CS4 application?
Thanks in advance for your great help.
Regards,
Muthuraj. D
Copy link to clipboard
Copied
Here are a few alternative versions of the script that were posted on the forum a while ago:
This script was written by Harbs
ArrayCompress=function(array){
var str = array.sort().join('\r')+'\r'
str = str.replace(/([^\r]+\r)(\1)+/g,'$1')
str = str.replace(/\r$/,'')
return str.split('\r')
}
IsInArray = function (item,array){
for(var i=0;i<array.length;i++){
if(array == item){return true;}
}
return false;
}
var doc = app.documents[0];
var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;
try{
styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle);
}catch(e){}
try{
styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle);
}catch(e){}
var names = [];
for(var i=0;i<styles.length;i++){
names = styles.name;
}
names = ArrayCompress(names);
var allStyles = doc.allParagraphStyles;
for(var i=allStyles.length-1;i>=0;i--){
if(IsInArray(allStyles.name,names)){
allStyles.splice(i,1);
}
}
alert(allStyles.length + " unused styles");
and this one by Jongware
var allpars = app.activeDocument.stories.everyItem().paragraphs.everyItem().appliedParagraphS tyle;
var pStyles = app.activeDocument.paragraphStyles;
var parlist = new Array;
rmvPStyles();
function rmvPStyles(){
for (a=0; a<allpars.length; a++)
{
add_me = true;
for (b=0; b<parlist.length; b++)
{
if (parlist == allpars)
{
add_me = false;
break;
}
...
);
}
for (c=pStyles.length-1; c>=2; c--){
myStyle=pStyles;
if (styleInArray(mySt...
where can I find the relevent javascript reference for CS4 application?
It is here.
Kasyan
Copy link to clipboard
Copied
This works for me:
var myDoc = app.activeDocument;
var myParStyles = myDoc.paragraphStyles;
var myCharStyles = myDoc.characterStyles;
for (j = myParStyles.length-1; j >= 2; j-- ) {
removeUnusedParaStyle(myParStyles);
}
for (i = myCharStyles.length-1; i >= 1; i-- ) {
removeUnusedCharStyle(myCharStyles);
}
function removeUnusedParaStyle(myPaStyle) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = myPaStyle;
var myFoundStyles = myDoc.findText();
if (myFoundStyles == 0) {
myPaStyle.remove();
}
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
}
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;
}
Copy link to clipboard
Copied
Hi Kasyan,
Thanks again for your great help. Now the script is working fine in Indesign CS4 as well. I am in the process of creating a Javascript to cleanup a indesign document (remove unused paragraph and character styles, remove unused swatches, remove unused master pages and save as a copy to remove document history). I have got the right script for to remove the unused styles with your help.
I have tried the following script to remove the unused swatches. It is working fine but the problem is, this script removes the available gradeient information in the indesign document and changes into black color.
#target indesign
var myIndesignDoc = app.activeDocument;
var myUnusedSwatches = myIndesignDoc.unusedSwatches;for (var j=myIndesignDoc.unusedSwatches.length; j > 0; j--)
myIndesignDoc.unusedSwatches[j-1].remove();
I could not attach the screenshot image of the gradient here and I have sent you through mail (askoldich@yahoo.com).
Please look into it and advise. Thanks a lot.
Muthuraj. D
Copy link to clipboard
Copied
Hi.
I'm using this script in my documents but character styles in bullets are removed too.
Have any way to preserve in these cases?
Any body can help me?
Thanks and Hug.
Copy link to clipboard
Copied
Sorry to resurrect this old thread, but along with Beto Correa above, I too have found a catastrophic issue with using Kasyan's script.
If a paragraph style has bullets applied that have a character style applied, Kasyan's script will delete the character style applied to the bullet, largely because the character style isn't used in areas of the document where the find/change can look (i.e. applied to a bullet from a paragraph style).
Copy link to clipboard
Copied
Can we find the applied bullet character style name
Copy link to clipboard
Copied
Copy link to clipboard
Copied
One way would be to call the menu items as shown by Kasyan Servetsky: How to open any menu item by script in InDesign
A down and dirty script that can be achieved from cutting and pasting from Kasyan's site will yield a script that gives a false impression that it will work:
var myMenu = app.menus.item("$ID/CharStylePanelPopup");
var myMenuItem = myMenu.menuItems.item("Select All Unused");
var myMenuAction = myMenuItem.associatedMenuAction;
myMenuAction.invoke();
var myMenu = app.menus.item("$ID/CharStylePanelPopup");
var myMenuItem = myMenu.menuItems.item("Delete Style...");
var myMenuAction = myMenuItem.associatedMenuAction;
myMenuAction.invoke();
The above script will work if using the English version of ID that has a file that contains unused character styles.
However, the script will throw an error if there are no unused character styles (line 6 specifically). I'd imagine that it would throw an error at line 2 if running this script on a different locale.
I'm sure the above script would work for English users if lines 5-8 were called upon using an if statement based on the selection that was just made. The embarrassing thing is that I'm sure it's something very simple to code, but I personally don't know how I'd write that code.
Copy link to clipboard
Copied
However, the script will throw an error if there are no unused character styles (line 6 specifically). I'd imagine that it would throw an error at line 2 if running this script on a different locale.
Hi Colin,
1. Try to enclose the code within a try-catch block so if there are no styles (an error occurs), either nothing happens, or the script gives a warning like this:
Here's my version of your script:
try {
var menu = app.menus.item("$ID/CharStylePanelPopup");
var menuItem = menu.menuItems.item("$ID/SelectAllUnused");
var menuAction = menuItem.associatedMenuAction;
menuAction.invoke();
menu = app.menus.item("$ID/CharStylePanelPopup");
menuItem = menu.menuItems.item("$ID/Delete Style...");
if (!menuItem.isValid) {
menuItem = menu.menuItems.item("$ID/Delete Styles...");
}
menuAction = menuItem.associatedMenuAction;
menuAction.invoke();
}
catch(err) {
if (err.number == 53762) alert("There are no unused character styles in the document.", "Remove unused character styles script", false);
}
2. I guess (though I can't test it) the script will work in all locales if you use app.menus.item("$ID/SomeName");
If memory serves me right, the problem with other locales occurs when you use app.menus.item("SomeName"); or app.menus.itemByName("SomeName");
You can get the locale-independent string(s) from the internal string localization database using app.findKeyStrings("Some Menu Command"); like so:
3. Note: the menu name changes depending on how many styles are selected: plural or singular.
Hope this helps.
— Kas
Copy link to clipboard
Copied
Almost there...
I found that if the Character Styles panel was not open, then the script would throw the alert, despite having character styles that could be deleted.
This means opening the Character Styles panel and closing it after the script is run.
The following would change the code:
app.panels.itemByName("$ID/Character Styles").visible = true;
try {
var menu = app.menus.item("$ID/CharStylePanelPopup");
var menuItem = menu.menuItems.item("$ID/SelectAllUnused");
var menuAction = menuItem.associatedMenuAction;
menuAction.invoke();
menu = app.menus.item("$ID/CharStylePanelPopup");
menuItem = menu.menuItems.item("$ID/Delete Style...");
if (!menuItem.isValid) {
menuItem = menu.menuItems.item("$ID/Delete Styles...");
}
menuAction = menuItem.associatedMenuAction;
menuAction.invoke();
}
catch(err) {
if (err.number == 53762) alert("There are no unused character styles in the document.", "Remove unused character styles script", false);
}
app.panels.itemByName("$ID/Character Styles").visible = false;
I can get that to work successfully.
Copy link to clipboard
Copied
I just need a script that deletes all character styles and “Preserves Formatting” and “Apply to All” and “Replace with:[None]” (screenshot below).
Thanks in advance to anyone who can help.
Delete all character styles after “[None]”.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now