Skip to main content
LynxKx
Inspiring
August 26, 2022
Answered

Script to find/replace form field descriptions text?

  • August 26, 2022
  • 3 replies
  • 569 views

Is there a script I could write to search text within form field descriptions and then to replace text?

I have a document with multiple pages with form fields that have "Theme 1, Theme 2, etc" in the description, and the script should be able to search and replace the word Theme with Subject in all the descritions ... I'd like the script to bring up a prompt that I can type the word i'm searching and then type the word to replace but I'm not quite sure how to handle that either ... 

 

Here is the code I've got started to at least create the pop up

 

var myDoc = app.activeDocument;



function formFindReplace(p){
    var a = new Array;
    for(var i = 0; i < p.length; i++){
        a.push(p[i].name);
    }
    return a
}

//popup box
makeDialog();
function makeDialog(){

    var theDialog = app.dialogs.add({name:"Form Description find/replace", canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Find:"});
                    staticTexts.add({staticLabel:"Replace:"});
                }
                with(dialogColumns.add()){
                    descFind = ; //field to type
                    descReplace = ; //field to type                  
                } 
        }
        if(theDialog.show() == true){
            descFind = ; 
            replaceText = ;
            theDialog.destroy();
        }
    }
}
 //how do I complete this function?

 

This topic has been closed for replies.
Correct answer Laubender

@LynxKx said: "They are all anchored to text for ADA"

 

Ok. That is something we can handle differently.

 

The basic thing is: If all of them are anchored objects means that all of them can be reached through the document's story objects. Look up the Story object in the DOM documentation and you can see, that formFields is a property of Story as well.

 

To get all stories of a document and to get all form fields of all stories can be done with everyItem() like that:

var allAnchoredFormFieldsArray =
app.documents[0].stories.everyItem().formFields.everyItem().getElements();

 Note, that form fields in table cells are not caught this way. Also not the form fields that may be anchored to footnote texts.

Also check, if the items of that array in variable allAnchoredFormFieldsArray is not an array of arrays, but a flat one that only contains form fields. The above code could be an array of arrays of form fields if you access all open documents for example ( did not test that ) with everyItem() as well, but I do not do this and only access the first open document.

 

So if all turns out well you basically could use the loop through the formFields array I gave in my previous post to access every anchored form field and change the contents of the description property with a regular expression.

 

What do you mean by: "and are not pre-populated" ?
Is the description field of the anchored ones empty?

Hm. I thought you wanted to change the contents from "Theme" to "Subject"?

 

If you want to look more into the concept of collections, arrays and the power of everyItem() I can only highly recommend two articles written by Marc Autret:

https://www.indiscripts.com/post/2010/06/on-everyitem-part-1

https://www.indiscripts.com/post/2010/07/on-everyitem-part-2

 

Regards,
Uwe Laubender
( Adobe Community Professional )

3 replies

LaubenderCommunity ExpertCorrect answer
Community Expert
August 27, 2022

@LynxKx said: "They are all anchored to text for ADA"

 

Ok. That is something we can handle differently.

 

The basic thing is: If all of them are anchored objects means that all of them can be reached through the document's story objects. Look up the Story object in the DOM documentation and you can see, that formFields is a property of Story as well.

 

To get all stories of a document and to get all form fields of all stories can be done with everyItem() like that:

var allAnchoredFormFieldsArray =
app.documents[0].stories.everyItem().formFields.everyItem().getElements();

 Note, that form fields in table cells are not caught this way. Also not the form fields that may be anchored to footnote texts.

Also check, if the items of that array in variable allAnchoredFormFieldsArray is not an array of arrays, but a flat one that only contains form fields. The above code could be an array of arrays of form fields if you access all open documents for example ( did not test that ) with everyItem() as well, but I do not do this and only access the first open document.

 

So if all turns out well you basically could use the loop through the formFields array I gave in my previous post to access every anchored form field and change the contents of the description property with a regular expression.

 

What do you mean by: "and are not pre-populated" ?
Is the description field of the anchored ones empty?

Hm. I thought you wanted to change the contents from "Theme" to "Subject"?

 

If you want to look more into the concept of collections, arrays and the power of everyItem() I can only highly recommend two articles written by Marc Autret:

https://www.indiscripts.com/post/2010/06/on-everyitem-part-1

https://www.indiscripts.com/post/2010/07/on-everyitem-part-2

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Community Expert
August 26, 2022

Note: If all your form fields are not nested, you simply can get all of them like below.

Also note that you'll run into an error if you look into property description with a MultiStateObject, which is also a form field:

 

var formFields = app.documents[0].formFields.everyItem().getElements();

// Loop them:
for( var n=0; n<formFields.length; n++ )
{
	if( formFields[n].constructor.name == "MultiStateObject" ){ continue };
	
		formFields[n].description = 
		formFields[n].description.replace(/^Theme/, "Subject" );
};

 

You may also write a better regular expression to switch from "Theme" to "Subject".

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Community Expert
August 26, 2022

Hi @LynxKx ,

look up the DOM documentation for form fields:

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

 

What kind of form fields do you like to change?

Are all positioned plainly on the pages or are they perhaps also nested in groups or anchored to text or pasted inside graphic frames? Do they perhaps even populate states of MSOs that are not active?

 

Regards,
Uwe Laubender
( Adobe Community Professional )

LynxKx
LynxKxAuthor
Inspiring
August 26, 2022

Thank you so much for helping! I will study the link you sent and see what I can put together.

They are all anchored to text for ADA., and are not pre-populated.