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

Scripting - Find all Form Fields and remove

Community Beginner ,
Aug 11, 2023 Aug 11, 2023

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.

TOPICS
Scripting
1.3K
Translate
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 11, 2023 Aug 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() {

    v
...
Translate
Community Expert ,
Aug 11, 2023 Aug 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.

Translate
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
People's Champ ,
Aug 11, 2023 Aug 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?

Translate
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 11, 2023 Aug 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!

Translate
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 Beginner ,
Aug 11, 2023 Aug 11, 2023

Mark thank you very much. With the save it does more than expected.

Appreciate the help and it will certainly help and be a huge timesaver.

*Just a small note: for some reason it misses a few of the "checkboxes" - Not an issue for me as it removes 95-99% of what I need removed so am stoked. Thanks again for the assist.

Translate
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 11, 2023 Aug 11, 2023

You're welcome, @Liam1965. I've updated the script now, with @Laubender's suggestion, and I think it may now work with the missing CheckBoxes, too.

- Mark

Translate
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 Beginner ,
Aug 11, 2023 Aug 11, 2023

Mark thank you very much. With the save, it does more than expected.

Appreciate the help and it will certainly help and be a huge timesaver.

*Just a small note: for some reason, it misses a few of the "checkboxes" - Not an issue for me as it removes 95-99% of what I need to be removed so am stoked. Thanks again for the assist.

Translate
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
Contributor ,
Feb 22, 2024 Feb 22, 2024

Is there a way to just find all "text fields" in InDesign. I need to find them and apply an object style to them. Last minute client request to add shading on a 300 page forms job. Thanks.

Translate
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 ,
Feb 23, 2024 Feb 23, 2024

Hi @rcoda1, change the line

thingPlural = 'formFields',

to

thingPlural = 'textBoxes',

 and I think that will do it.

- Mark

Translate
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
Contributor ,
Feb 23, 2024 Feb 23, 2024

Thanks but I think you minderstood. I don't want to remove them (they are both form fields and text frames... not check boxes or radio buttons). I want to be able to apply an object style to them. Thanks

Translate
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 ,
Feb 23, 2024 Feb 23, 2024

Oops, sorry. I still don't understand exactly what you need. For example I don't know what you mean by "they are both form fields and text frames". But do you mind asking a new question and include a demo indesign document? That would make it fast. We can use 95% of the script here, but it needs some changes and it seems messy to clutter this thread with any further back-and-forth about this variation on the question.

- Mark

Translate
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
Contributor ,
Feb 23, 2024 Feb 23, 2024

H! Thank you for the offer. By form field and also text box I mean the form field "type" is a text field.
I made a quick 2-page sample INDD file from the actual file, packaged. Zip file here: Test INDD file 

Translate
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 ,
Feb 23, 2024 Feb 23, 2024

Okay, here's a modified version for you. Let me know how it goes. - Mark

/**
 * Apply object style to every Text FormField.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/scripting-find-all-form-fields-and-remove/m-p/14000885
 */
function main() {

    var objectStyleName = 'Text Field'; // edit this

    var doc = app.activeDocument,
        objectStyle = doc.objectStyles.itemByName(objectStyleName),
        thingPlural = 'textBoxes';

    if (!doc.hasOwnProperty(thingPlural))
        throw Error('Bad `thingPlural` supplied.');

    if (!objectStyle.isValid)
        throw Error('Could not find object style "' + objectStyleName + '".');

    var 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());

    }

    // apply objectStyle to each thing
    var counter = 0;
    for (var i = 0; i < things.length; i++) {
        if (things[i].isValid) {
            things[i].appliedObjectStyle = objectStyle;
            counter++;
        }
    }

    alert('Applied object style to ' + counter + ' ' + thingPlural);

} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Apply Object Style To Form Fields');
Translate
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
Contributor ,
Feb 23, 2024 Feb 23, 2024

That is absolutely brilliant! It worked great! Can I buy you a coffee or a bottle of your favorite? Thanks again, Rich.

P.S. Would you mind if I posted this on InDesign Secrets? If so, please modify it as you wish with your contact info. I will only do this if you agree.

Translate
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 ,
Feb 23, 2024 Feb 23, 2024
LATEST

Great! Haha, thank you, no need for anything. Script was free, no strings attached. Feel free to post anywhere you like. You can link to my profile here if you like, if anyone wants to get in touch.

- Mark

Translate
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 11, 2023 Aug 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 )

 

Translate
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 11, 2023 Aug 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

Translate
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 11, 2023 Aug 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 )

Translate
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 11, 2023 Aug 11, 2023

Hi @Laubender, you nailed it again! It wasn't collecting formFields from groups. Fixed now.

- Mark

Translate
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