Highlighted

Need Help creating Script for InDesign / form fields

New Here ,
Dec 10, 2019

Copy link to clipboard

Copied

Ive been unable to find a solution to changing the default font for InDesign form fields, currently defaults to Minion Pro  size: 12 pt. 

 

Wondering if i could create a script to run that would automatically change all the form fields that I have to Arial Italic size: Auto

 

Could someone help with what that sript would look like?

 

Thank you!

Adobe Community Professional
Correct answer by Laubender | Adobe Community Professional

Hi Beth,

thank you for your test document.

There is one textBox form field directly on the page and four others are anchored.

My initial script code is working as expected on the one that is not anchored. For me no need to run script code two times.

Here the code again:

 

app.documents[0].textBoxes.everyItem().properties =
{
	 appliedFont : "Arial" ,
	 fontStyle : "Italic" ,
	 fontSize : 0
};

 

 

Well, you are working on Mac OS X, I have InDesign installed on Windows 10. And we are using slightly different versions of the same main version. Could make a difference, but shouldn't in this case. You already debugged the code and you do not run fontStyle when you first apply the object to the properties, but obviously you need to apply the fontStyle property separately to make it work. On my Windows system it is working in one go.

 

However, for the anchored textBox objects we need a different approach.

 

As I already suggested:

You could loop the allPageItems array of the document, filter out the textBox objects and apply the properties.

Tested OK with your document:

 

 

var doc = app.documents[0];
var allPageItemsArray = doc.allPageItems;
var allPageItemsArrayLength = allPageItemsArray.length;

var newProperties = 
{
	 appliedFont : "Arial" ,
	 fontStyle : "Italic" ,
	 fontSize : 0
};

for( var n=0; n<allPageItemsArrayLength; n++ )
{
	// Do nothing, if the current page item is NOT a text box form field:
	if( allPageItemsArray[n].constructor.name != "TextBox" ){ continue };
	
	allPageItemsArray[n].properties = newProperties ;
	
};

// Done
alert( "Text Box Form Fields changed!" );

 

 

Regards,
Uwe Laubender

( ACP )

TOPICS
Scripting

Views

189

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Need Help creating Script for InDesign / form fields

New Here ,
Dec 10, 2019

Copy link to clipboard

Copied

Ive been unable to find a solution to changing the default font for InDesign form fields, currently defaults to Minion Pro  size: 12 pt. 

 

Wondering if i could create a script to run that would automatically change all the form fields that I have to Arial Italic size: Auto

 

Could someone help with what that sript would look like?

 

Thank you!

Adobe Community Professional
Correct answer by Laubender | Adobe Community Professional

Hi Beth,

thank you for your test document.

There is one textBox form field directly on the page and four others are anchored.

My initial script code is working as expected on the one that is not anchored. For me no need to run script code two times.

Here the code again:

 

app.documents[0].textBoxes.everyItem().properties =
{
	 appliedFont : "Arial" ,
	 fontStyle : "Italic" ,
	 fontSize : 0
};

 

 

Well, you are working on Mac OS X, I have InDesign installed on Windows 10. And we are using slightly different versions of the same main version. Could make a difference, but shouldn't in this case. You already debugged the code and you do not run fontStyle when you first apply the object to the properties, but obviously you need to apply the fontStyle property separately to make it work. On my Windows system it is working in one go.

 

However, for the anchored textBox objects we need a different approach.

 

As I already suggested:

You could loop the allPageItems array of the document, filter out the textBox objects and apply the properties.

Tested OK with your document:

 

 

var doc = app.documents[0];
var allPageItemsArray = doc.allPageItems;
var allPageItemsArrayLength = allPageItemsArray.length;

var newProperties = 
{
	 appliedFont : "Arial" ,
	 fontStyle : "Italic" ,
	 fontSize : 0
};

for( var n=0; n<allPageItemsArrayLength; n++ )
{
	// Do nothing, if the current page item is NOT a text box form field:
	if( allPageItemsArray[n].constructor.name != "TextBox" ){ continue };
	
	allPageItemsArray[n].properties = newProperties ;
	
};

// Done
alert( "Text Box Form Fields changed!" );

 

 

Regards,
Uwe Laubender

( ACP )

TOPICS
Scripting

Views

190

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 10, 2019 0
Adobe Community Professional ,
Dec 10, 2019

Copy link to clipboard

Copied

Hi Beth,

changing the default for a textBox object is not possible, I think.

What's your exact version of InDesign? What operating system? What is your scripting language you like to use?

 

With ExtendScript (JavaScript):

Changing the value for appliedFont and the value for fontStyle of an existing textBox object is possible.

The value for fontSize is a bit unclear if you want "Auto" and you are looking at DOM documentation:

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

 

Did a little experiment. Set fontSize to "Auto" with a selected textBox in my InDesign 2020.

Then I read the value of fontSize with the textBox selected:

 

app.selection[0].fontSize

 

The returned value was 0. So you could simply assign value 0 to get "Auto" with fontSize.

 

So if all your text boxes are just plain objects on the pages of your document, if they are not nested, e.g. grouped, you could do this job like that:

 

app.documents[0].textBoxes.everyItem().properties =
{
	 appliedFont : "Arial" ,
	 fontStyle : "Italic" ,
	 fontSize : 0
};

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 10, 2019 0
New Here ,
Aug 05, 2020

Copy link to clipboard

Copied

I have to run the script twice but it works! thank you!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 05, 2020 0
Adobe Community Professional ,
Dec 11, 2019

Copy link to clipboard

Copied

changing the default for a textBox object is not possible

Yes I confirm.

 

Open your PDF form with Acrobat Pro, open the JavaScript Console (CTRL/CMD + J), clear the Console if needed, paste this script, select all, and hit the Enter key to run it (Return key is not the Enter key).

 

for (var i=0; i<this.numFields; i++) {
var oFld = this.getField(this.getNthFieldName(i));
if (oFld.type == "text") {
oFld.textFont = "Arial";
oFld.textSize = 0,
}}

app.alert("Done!");

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 11, 2019 1
Adobe Community Professional ,
Aug 06, 2020

Copy link to clipboard

Copied

Beth said: "I have to run the script twice but it works! thank you!"

 

Hi Beth,

what happens if you run the script code only one time?

I assume that the script should work the first time. Hm. Maybe InDesign is not showing the result immediately?

 

Try the following:

[1] Run the script once on your InDesign document.

[2] Save, close and re-open the document.

 

Are the changes made?

 

Thanks,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 06, 2020 0
New Here ,
Aug 06, 2020

Copy link to clipboard

Copied

If i run it one time it changes to Arial regular, if i run it again, it will adjust to italic.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 06, 2020 0
Adobe Community Professional ,
Aug 07, 2020

Copy link to clipboard

Copied

Hi Beth,

thank you for reporting this. That's rather strange…

 

Could you share a sample InDesign document where you see this issue?

Best put in on Dropbox and post the download link.

 

Thanks,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 07, 2020 0
New Here ,
Aug 11, 2020

Copy link to clipboard

Copied

Hi Uwe,

Thanks for helping! I will try and pull together a sample doc and send to you, right now I have work docs that Im using and cant forward those. I adjusted the script and pulled out the italic to happen in a second step and then it worked.

 

Another question. If I have forms anchored in a text box, the script doesnt work... 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 11, 2020 0
Adobe Community Professional ,
Aug 11, 2020

Copy link to clipboard

Copied

Hi Beth,

if you have nested textBox form field objects, objects that are grouped, anchored to text, pasted inside frames, we need more code to get them. Currently only textBox objects that are directly positioned on the spreads are found by the script.

 

FWIW: Do not confuse textBox objects with textFrame objects. textBox objects are form fields:

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

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

 

You could loop the allPageItems array of the document and single out the textBox objects and assign the properties object I suggested before to the properties property if a textBox is found. But in case you have form field textBox objects inside not-active states of buttons ( or multistate objects or other form fields with states ) we need a completely different approach.

 

Best make a sample document available that I can test.

 

Thanks,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 11, 2020 0
Community Beginner ,
Aug 11, 2020

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 11, 2020 0
New Here ,
Aug 12, 2020

Copy link to clipboard

Copied

I didnt realize I signed into an old accound before. I've also attached the file in case that link doesnt work!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 12, 2020 0