Copier le lien dans le Presse-papiers
Copié
I have a problem: i generate many pdf-formulars for many companies. I often have to change the format for over 50 fields at the same time. 50 fields will change from standard to integer format. Sometimes i have to change 50 fields from integer format with two decimal places to one decimal place.
How can i do that for several fields at the same time? Currently i change one filed by another. And that is no clean solution!
Best regards and thanks for your help
Copier le lien dans le Presse-papiers
Copié
Paste this script in the JS Console, select all, and hit the Enter key:
for (var i = 0 ; i < this.numFields ; i++) {
var oFld = this.getField(this.getNthFieldName(i));
if ((oFld.type == "text") && (/total/.test(oFld.name))) {
oFld.setAction("Format","event.value = util.printf(\"%,2.0f\",event.value) ;");
}
}
It will change the Format of all text fields whose name contains total like this:
You can assign the format you want by setting printf:
Copier le lien dans le Presse-papiers
Copié
I am trying to use the code above to change multiple text boxes at the same time from integer to 1 decimal point but it doesn't seem to work. The code I am using is below and all I have changed is for the file name to contain 'TestCPL' and the format from '%,2.0f' to %,2.1f (1 decimal point).
I have then selected all the fields on the page and hit enter but nothing seems to change in the field names that contain TestCPL. Can you please tell me what I am doing wrong?
Copier le lien dans le Presse-papiers
Copié
You don't need to select the fields. You need to select the code in the JS Console and press Ctrl+Enter to execute it.
Copier le lien dans le Presse-papiers
Copié
Thank you for responding.
I now seem to get the message 'undefined' under the code after I execute it
You can see the field names in the screenshot below and there are definitely a number of them in the first two rows that contain 'TestCPL' as part of the name.
Copier le lien dans le Presse-papiers
Copié
"undefined" is not an error, the opposite, actually.
Check if the fields were actually edited.
Copier le lien dans le Presse-papiers
Copié
They didn't change. My fields are currently set to zero decimal points and I am trying to set them to 1 decimal point. After executing that code they still remain at zero decimal points.
Copier le lien dans le Presse-papiers
Copié
Daphne5C23 since it's a grid of fields you don't need to use a script.
Remove all fields except the first one, set the Format, then right-clic on the field and use "Create multiple copies".
This should take about 30 seconds.
Copier le lien dans le Presse-papiers
Copié
Thanks for your suggestion. I do use the 'Create Multiple Copies' function when starting a form from scratch but sadly that is not an option for me on this occassion.
I actually have several existing forms to amend, each one with many fields in a grid pattern. Almost all of those fields contain individual validation scripts checking target values against actual values and formatting the text colour accordingly so if I used the create multiple copies method I would then have to amend all those validation scripts individually instead so it would actually take several hours.
Copier le lien dans le Presse-papiers
Copié
That's because the code you got will not set the built-in Number option of the Format tab.
Try this:
oFld.setAction("Format", "AFNumber_Format(1, 0, 0, 0, \"\", false)");
oFld.setAction("Keystroke", "AFNumber_Keystroke(1, 0, 0, 0, \"\", false)");
Copier le lien dans le Presse-papiers
Copié
That works perfectly! Thank you so much for all your help.
Copier le lien dans le Presse-papiers
Copié
I have used the script below to edit multiple formats at once, but specified them with the name of the fields it needs to change, but sadly, it doesn't work. Am I doing something wrong? I have the newest version, but I did make the form in Indesign (also newest version) before exporting it to an interactive pdf. In adobe Acrobat i added the script, because otherwise I needed to edit the format of all the fields by hand.
var names = this.getFieldNames();
var kostenCount = 0;
var datumCount = 0;
for(var i=0; i<names.length; i++){
var f = this.getField(names[i]);
if(!f || f.type !== "text") continue;
// ===== KOSTENVELDEN =====
if(f.name.indexOf("Kosten_") === 0 ||
f.name === "Totaal-kosten-alles" ||
f.name.indexOf("Prijs-stuk_") === 0){
if(f.value === "") f.value = 0; // dummy waarde
f.setAction("Format", "AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value; // trigger format
kostenCount++;
}
// ===== DATUMVELDEN =====
if(f.name.indexOf("Deadline_") === 0 ||
f.name.indexOf("Start-datum_") === 0 ||
f.name.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000"; // dummy datum
f.setAction("Format", "AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke", "AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value; // trigger format
datumCount++;
}
}
// Bevestigingspopup
app.alert("Formatting applied!\nKostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);
Copier le lien dans le Presse-papiers
Copié
Check the JavaScript console for errors .
Copier le lien dans le Presse-papiers
Copié
I do not get an error, just the undefined, but it still doesn't work as it should i guess
Copier le lien dans le Presse-papiers
Copié
What are the names of the fields you're trying to edit?
Can you share the file?
Copier le lien dans le Presse-papiers
Copié
I have attached the file, please note that it is in Dutch. I want to do the following:
Is this possible?
Copier le lien dans le Presse-papiers
Copié
Ja, dat is geen probleem! 🙂
There's no such method as getFieldNames, though. You probably got it off some kind of AI model that invented it. To access all the fields in a file use the following:
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f==null) continue;
// do something with this field
}
I did that, and it worked fine. It edited 192 "Kosten" fields and 140 "Datum" fields. I would recommend adding a command to switch off calculations before running it, though.
Add this before your code:
this.calculate = false;
And this at the end of it:
this.calculate = true;
Copier le lien dans le Presse-papiers
Copié
You should have, because (as I wrote below above) getFieldNames doesn't exist, so the first line of code would generate this error:
TypeError: this.getFieldNames is not a function
1:Console:Exec
Copier le lien dans le Presse-papiers
Copié
I added the fnction as you said it and for me it didn't change anything it seems. Although it did give the note "True" after i did ctrl + enter. Am I doing something wrong? The full script should be as below, right? I only needed to change the top part of my original script.
this.calculate = false;
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f==null) continue;
// do something with this field
}
// ===== KOSTENVELDEN =====
if(f.name.indexOf("Kosten_") === 0 ||
f.name === "Totaal-kosten-alles" ||
f.name.indexOf("Prijs-stuk_") === 0){
if(f.value === "") f.value = 0; // dummy waarde
f.setAction("Format", "AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value; // trigger format
kostenCount++;
}
// ===== DATUMVELDEN =====
if(f.name.indexOf("Deadline_") === 0 ||
f.name.indexOf("Start-datum_") === 0 ||
f.name.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000"; // dummy datum
f.setAction("Format", "AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke", "AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value; // trigger format
datumCount++;
}
}
// Bevestigingspopup
app.alert("Formatting applied!\nKostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);
this.calculate = true;
Copier le lien dans le Presse-papiers
Copié
Select the whole script before you execute it.
Copier le lien dans le Presse-papiers
Copié
I used the code below, selected it al before I "Ctrl+enter"ed, but now I got a "SyntaxError: syntax error
31:Console:Exec
undefined"
What am I not seeing and/or doing wrong?
this.calculate = false;
for (var i=0; i<this.numFields;i++){
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f=nul) continue;
// Do something with this field
}
// Kostenvelden
if(fname.indexOf("Kosten_") === 0||
fname === "Totaal-kosten-alles" ||
fname.indexOf("Prijs-stuk_") === 0){
if(f.value === "")f.value=0;
f.setAction("Format","AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value
kostenCount++;
}
// Datumvelden
if(fname.indexOf("Deadline_") === 0||
fname.indexOf("Start-datum_") === 0||
fname.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000";
f.setAction("Format","AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke","AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value
datumCount++;
}
}
//Bevestigings pop-up
app.alert("Formatting applied!\Kostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);
this.calculate = true
Copier le lien dans le Presse-papiers
Copié
You have one curly bracket too many, after this line:
// Do something with this field
Use the code I provided below.
Copier le lien dans le Presse-papiers
Copié
No, it needs to be this:
this.calculate = false;
var kostenCount = 0;
var datumCount = 0;
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f==null) continue;
if(!f || f.type !== "text") continue;
// ===== KOSTENVELDEN =====
if(f.name.indexOf("Kosten_") === 0 ||
f.name === "Totaal-kosten-alles" ||
f.name.indexOf("Prijs-stuk_") === 0){
if(f.value === "") f.value = 0; // dummy waarde
f.setAction("Format", "AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value; // trigger format
kostenCount++;
}
// ===== DATUMVELDEN =====
if(f.name.indexOf("Deadline_") === 0 ||
f.name.indexOf("Start-datum_") === 0 ||
f.name.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000"; // dummy datum
f.setAction("Format", "AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke", "AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value; // trigger format
datumCount++;
}
}
// Bevestigingspopup
app.alert("Formatting applied!\nKostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);
this.calculate = true;
Copier le lien dans le Presse-papiers
Copié
Thank you! It works now! Last two questions
I am new at making these scripts, really want to become better at it and I really appriciate the help! Thanks a lot
Copier le lien dans le Presse-papiers
Copié
1. Yes, there's no need to do that.
2. Yes. After '€' in both function calls, add a "true" parameter (without the quotes).
Préparez-vous ! Une expérience Adobe Community améliorée arrive en janvier.
En savoir plus