Skip to main content
Participant
August 11, 2023
Answered

Scripting - Find all Form Fields and remove

  • August 11, 2023
  • 2 replies
  • 1853 views

I need to find and select all forms and delete from many documents. 

 

I have over 300 forms with up to 500 FormFields so manual is an option I would like to avoid.

 

I am not a scripter so this is way beyond my understanding, any assistance would be appreciated.

This topic has been closed for replies.
Correct answer m1b

Hi @Liam1965, you can try this script I just wrote. It will remove form fields from all open documents, and save each document with a suffix (so that it won't save over the original). Let me know if it's useful.

- Mark

 

 

 

/**
 * Remove all form fields from all open documents,
 * and save each document with the suffix '_NO_FIELDS'
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/scripting-find-all-form-fields-and-remove/m-p/14000885
 */
function main() {

    var fileSuffix = '_NO_FIELDS',
        thingPlural = 'formFields',
        docs = app.documents,
        counter = 0;

    for (var i = docs.length - 1; i >= 0; i--) {

        var doc = docs[i],
            path = doc.fullName.fsName.replace(/(\.[^\.]+)$/, fileSuffix + '$1'),
            things = doc[thingPlural].everyItem().getElements();

        if (doc.groups.length > 0)
            // also collect things in groups
            things = things.concat(doc.groups.everyItem()[thingPlural].everyItem().getElements());

        if (doc.stories.length > 0) {

            // also collect any anchored things
            things = things.concat(doc.stories.everyItem()[thingPlural].everyItem().getElements());

            if (doc.stories.everyItem().tables.length > 0)
                // also collect any thing in tables
                things = things.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem()[thingPlural].everyItem().getElements());
        }

        // remove each thing
        for (var j = things.length - 1; j >= 0; j--) {
            if (things[j].isValid) {
                things[j].remove();
                counter++;
            }
        }

        // save as new document and close
        doc = doc.save(File(path));
        doc.close(SaveOptions.NO);

    }

    alert('Removed ' + counter + ' form fields.');

} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Remove All Form Fields');

 

 

Edit 2023-08-11: as per @TᴀW and @Laubender's comments, I now guess that the OP wishes to remove all form fields, not just text fields, so I have updated. If you want just textBoxes, or radioButtons, etc, change the `thingPlural` string.

 

Edit 2023-08-11: as @Laubender suggested, I added an extra line to collect things from groups.

 

Edit 2024-01-11: added some checks to avoid errors in documents that don't have stories, groups, or tables! Thanks to @Joel Cherney for the testing.

2 replies

Community Expert
August 11, 2023

Hi together,

yes, one could remove all form fields of a document in one go with:

 

// Remove all form fields of the active InDesign document:
app.documents[0].formFields.everyItem().remove();

 

 

But: that would also mean you will remove all kinds of form fields like:

Button | MultiStateObject | CheckBox | ComboBox | ListBox | RadioButton | TextBox | SignatureField

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#FormField.html#d1e127934__d1e130891

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

m1b
Community Expert
Community Expert
August 11, 2023

Oh thanks @Laubender, that is good to know! I will update my script to use "formFields" rather than being specific. One note is that your single line will not remove formFields anchored in text or in a table.

- Mark

Community Expert
August 11, 2023

Hi Mark,

thanks for testing!

( What I did not do with "nested" elements. )

 

@m1b  said: "One note is that your single line will not remove formFields anchored in text or in a table."

 

Good to know! Did you test with form fields in groups as well?

 

Regards,
Uwe Laubender
( Adobe Community Expert )

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
August 11, 2023

Hi @Liam1965, you can try this script I just wrote. It will remove form fields from all open documents, and save each document with a suffix (so that it won't save over the original). Let me know if it's useful.

- Mark

 

 

 

/**
 * Remove all form fields from all open documents,
 * and save each document with the suffix '_NO_FIELDS'
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/scripting-find-all-form-fields-and-remove/m-p/14000885
 */
function main() {

    var fileSuffix = '_NO_FIELDS',
        thingPlural = 'formFields',
        docs = app.documents,
        counter = 0;

    for (var i = docs.length - 1; i >= 0; i--) {

        var doc = docs[i],
            path = doc.fullName.fsName.replace(/(\.[^\.]+)$/, fileSuffix + '$1'),
            things = doc[thingPlural].everyItem().getElements();

        if (doc.groups.length > 0)
            // also collect things in groups
            things = things.concat(doc.groups.everyItem()[thingPlural].everyItem().getElements());

        if (doc.stories.length > 0) {

            // also collect any anchored things
            things = things.concat(doc.stories.everyItem()[thingPlural].everyItem().getElements());

            if (doc.stories.everyItem().tables.length > 0)
                // also collect any thing in tables
                things = things.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem()[thingPlural].everyItem().getElements());
        }

        // remove each thing
        for (var j = things.length - 1; j >= 0; j--) {
            if (things[j].isValid) {
                things[j].remove();
                counter++;
            }
        }

        // save as new document and close
        doc = doc.save(File(path));
        doc.close(SaveOptions.NO);

    }

    alert('Removed ' + counter + ' form fields.');

} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Remove All Form Fields');

 

 

Edit 2023-08-11: as per @TᴀW and @Laubender's comments, I now guess that the OP wishes to remove all form fields, not just text fields, so I have updated. If you want just textBoxes, or radioButtons, etc, change the `thingPlural` string.

 

Edit 2023-08-11: as @Laubender suggested, I added an extra line to collect things from groups.

 

Edit 2024-01-11: added some checks to avoid errors in documents that don't have stories, groups, or tables! Thanks to @Joel Cherney for the testing.

TᴀW
Legend
August 11, 2023

Not all form fields are text boxes. There are buttons, radio buttons, signature fields, dropdown lists, and others. This script will only remove the text fields, I think?

id-extras.com | InDesign tools & scripts for typesetters, form designers, and translators
m1b
Community Expert
Community Expert
August 11, 2023

@TᴀW Right! This currently only removes text box form fields. UPDATE: I have updated the code to remove "formFields" which will remove all types of form fields. Thanks @Laubender!