Skip to main content
Timothy Bennett
Inspiring
March 20, 2021
Answered

Drop Cap Style - remove and preserve formatting, find and replace others using same

  • March 20, 2021
  • 1 reply
  • 786 views

Hiya,

I've written the below code to "flatten" (I'm more a Photoshop guy, so that makes more sense to me as a term) nested styles, and find and replace any other text using the same nested styles. 
I'd like to do the same for Drop Cap Styles but am a bit of a loss where to start. There are no corresponding textPreferences for Drop Caps so that's out, beyond that I'm lost. 

Any pointers would be greatly appreciated,
Tim

 

 

    function removeStyles(check, whitelist) {
        for (i = doc.allParagraphStyles.length - 1; i >= 0; i--) {
            var clientItem = whitelist[3].arr[i];
            var hostItem = doc.allParagraphStyles[i];
            try {
                if (clientItem.checked === check && clientItem.name === hostItem.name) {
                    applyNestedStyles(hostItem, hostItem.nestedStyles);
                    applyNestedStyles(hostItem, hostItem.nestedLineStyles);
                    hostItem.dropCapStyle.remove();
                    hostItem.remove();
                }
            }
            catch (e) {
            }
        }
    };

    function applyNestedStyles(hostItem, nestedStyles) {
        for (j = nestedStyles.length -1; j>=0; j--) {
            resetFindReplace();

            var charStyle = hostItem.nestedStyles[j].appliedCharacterStyle;

            app.findTextPreferences.appliedParagraphStyle = hostItem;
            app.findTextPreferences.appliedCharacterStyle = charStyle;
            app.changeTextPreferences.appliedCharacterStyle = charStyle;

            doc.changeText();
        }
    }
    
    function resetFindReplace() {
        app.findTextPreferences = null;
        app.changeTextPreferences = null;
        app.findChangeTextOptions.properties = {
            includeLockedStoriesForFind: true,
            includeLockedLayersForFind: true,
            includeHiddenLayers: true,
            includeMasterPages: true,
            includeFootnotes: true,
            wholeWord: false,
            caseSensitive: false
        }
    }
}

 

This topic has been closed for replies.
Correct answer Timothy Bennett

Are you trying to imitate an initial cap without setting drop cap lines and characters? Can you do that manually in the UI using other character and paragraph formatting?


I've solved it 😄

I think that I possibly didn't articulate myself very well in my OP.

The function is passed a paragraph style, and if it has a drop cap style it then applies that drop cap style as a normal character style to all text present in the document that has the same paragraph style and drop cap applied. This is part of a larger script that removes all styles from a document whilst preserving formatting.
The solution was to change:

var charStyle = hostItem.nestedStyles[j].appliedCharacterStyle;

to

var charStyle = hostItem.nestedStyles[j];

because the drop cap style being passed as nestedStyles to the function already is a CharacterStyle.

1 reply

rob day
Community Expert
Community Expert
March 21, 2021

I'd like to do the same for Drop Cap Styles 

 

I don’t see how—don’t think it’s even possible to do it manually.

 

You can get the dropChapChracters and dropCapLines from the paragraph properties and from there you could calculate the point size for the dropcap character, but if you try to baseline shift the cap character, the text is not going to wrap. You would have to create a textframe for the cap and anchor that in the text.

 

 

 

 

Timothy Bennett
Inspiring
March 23, 2021

Hi Rob,

thanks for your reply.
I've actually managed to have some success with my existing code by passing the dropCapStyle object to applyNestedStyles() as an array:

applyNestedStyles(hostItem, new Array(hostItem.dropCapStyle));

 It's not quite behaving, and at the moment I've not managed to clearly pinpoint what's happening. I just wanted to reply so as not to be rude! I shall return once I've managed to do some more debugging...

rob day
Community Expert
Community Expert
March 23, 2021

Are you trying to imitate an initial cap without setting drop cap lines and characters? Can you do that manually in the UI using other character and paragraph formatting?