Skip to main content
Known Participant
Mar 5, 2025 12:39 PM UTC
Question

Hi character.remove() is crashing the Indesign server.

    function removeBoundaryMarkers(document) {
        try {
            logMsg(logFile, "Fetching marker style: " + MARKER_STYLE_NAME);
            // Find our specific marker style
            var markerStyle = document.characterStyles.itemByName(MARKER_STYLE_NAME);
            if (!markerStyle.isValid) {
                return;
            }

            logMsg(logFile, "Resetting findGrepPreferences and changeGrepPreferences");
            app.findGrepPreferences = NothingEnum.NOTHING;
            app.changeGrepPreferences = NothingEnum.NOTHING;



            // Find all hair spaces
            logMsg(logFile, "Setting findGrepPreferences.findWhat");
            app.findGrepPreferences.findWhat = BOUNDARY_MARKER;



            // Remove them if they have the specific style
            logMsg(logFile, "Executing findGrep");
            var foundItems = document.findGrep();
            logMsg(logFile, "Found " + foundItems.length + " items to check and remove");
            for (var i = foundItems.length - 1; i >= 0; i--) {
                if (foundItems[i].appliedCharacterStyle == markerStyle) {
                    logMsg(logFile, "Removing marker : "+i);
                    if (foundItems[i].contents.length > 0) {
                        foundItems[i].remove();
                    }
                }
            }

            // Clean up the style
            logMsg(logFile, "Removing marker style");
            markerStyle.remove();
        } catch (e) {
            logMsg(logFile, "Error removing boundary markers: " + e.message);
        }
    }
            }

This funtion just gets the Hair Space content and compares with specific chracter style, and removes the character. But, the issue im facing is foundItems, is gettign around 130 characters, able to remove from 130 till 91 but at 90th character, when foundItems[i].remove(); InDesign server crashes. What could be the issue.
 

22 replies

Robert at ID-Tasker
Legend
Mar 5, 2025 1:47 PM UTC

@adarsh_0848

 

Why do you have to iterate through all results - instead of doing Find&Change?

 

Translate
Known Participant
Mar 5, 2025 1:51 PM UTC

I dont want to change all the found Charcters, In my Codebase I have created custom character style and applied to Characters for processing some other functionality (which I have not shared here). But Conclusion is I want to remove characters with perticular character style . if (foundItems[i].appliedCharacterStyle == markerStyle)

Translate
Robert at ID-Tasker
Legend
Mar 5, 2025 2:05 PM UTC

But you're looking for a specific marker: 

 

app.findGrepPreferences.findWhat = BOUNDARY_MARKER; 

 

So if you'll combine this marker WITH specific CharStyle - you can use Find&Change to remove ONLY instances where this specific marker has this specific CharStyle applied. 

 

Translate
Peter Kahrel
Community Expert
Community Expert
Mar 5, 2025 1:30 PM UTC

I've seen this a few times at the level of story, maybe the underltying problem is the same as with character. Anyway, try

 

foundItems[i].contents = ''

 

 

Translate
Known Participant
Mar 5, 2025 1:53 PM UTC

But this wont cause any issue right. as we are just assigning '' empty character and not removing the character itself.

Translate
Peter Kahrel
Community Expert
Community Expert
Mar 5, 2025 2:09 PM UTC

character.remove() and character.contents = '' are equivalent. They have the same effect. It won't cause any problems. Just try it.

Translate