Copy link to clipboard
Copied
I have several forms each eith 20 to 30 fields which have no formatting and are just text fields. I want to programmatically change all fields with name containing "date" to mm/dd/yyyyy format. Is this possible?
I am new to Adobe scripting but I know some javascript. I could not find a method to change the field format from Adobe's JS guide and some quick internet searches did not yield anything that I could work on.
Copy link to clipboard
Copied
You can not directly change the field format using JavaScript. But you can add a format script using the setAction() method. This method allows to add a JavaScript to a list of events of the Field processing event sequence.
What you'd have to know, however, is where the Date Object comes from (be it a variable, or be it just "now"). The example below assumes that the current date should be used.
In order to set the format of the date, we use the util.printd() method.
Example; you would run this code from the Console while the document is open.
this.getField("date").setAction("Format", "event.value = util.printd(\"mm/dd/yyyy\", new Date() ) ;") ;
And that should get you on the way.
Hope this can help.
Copy link to clipboard
Copied
Max, thanks for the response. I made some progress but I am stuck with this:
var mymatch = new RegExp("date");
for (var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
if (mymatch.test(fname)) {
var f = this.getField(fname)
f.setAction({cTrigger: "Format", cScript: "AFDate_Format(\"mm/dd/yyyy\")"});
f.setAction({cTrigger: "Keystroke", cScript: "AFDate_Keystroke(\"mm/dd/yyyy\")"});
}
}
It sets the fields as date fields but the format is "m/d". Not "mm/dd/yyyy". Any ideas?
Copy link to clipboard
Copied
I just discovered that the format is not enforced and I can type in any values in the box. The field property shows format as m/d but there is no restriction.
Copy link to clipboard
Copied
Unless you can precisely control these pre-cooked functions, don't use them (they are undocumented; until about Acrobat 5, the according script was available in the clear; later on, they replaced it with a precompiled library, with the goal to speed up the application loading time). It is very likely that the argument of AFDate_Format() and AFDate_Keystroke() are incorrect.
Also, I wonder why you are using the RegEx matching for the field name match; wouldn't
if (fname == "date")
be easier to deal with?
Copy link to clipboard
Copied
You are right. The right arguments are AFDate_FormatEx and AFDate_KeystrokeEx.
I manually inserted AFDate_FormatEx("mm/dd/yyyy") and AFDate_KeystrokeEx("mm/dd/yyyy") in the custom format options and it worked. Now, I need to figure out how to push this in there using JS.
f.setAction({cTrigger: "Format", cScript: "AFDate_FormatEx(\"mm/dd/yyyy\");"});
f.setAction({cTrigger: "Keystroke", cScript: "AFDate_KeystrokeEx(\"mm/dd/yyyy\");"});
I wonder if escaping quotes is not working.
I am looking at all fields which contain "date" in the name. There are fields like date of birth, date graduated, date first employed, etc.
Copy link to clipboard
Copied
OK, that explains the RegExp… you still could use something like
if (fname.toLowerCase().indexOf("date") >= 0)
It would be a long shot but try the direct syntax (not using the cTrigger / cScript)
You might also see whether it really inserts code by using something like app.beep() as script.
I may be wrong, but it is possible that if both, the Keystroke and the Format scripts are set, the date format dialog may appear instead of the custom script dialog.
Copy link to clipboard
Copied
cTrigger / cScript work. For example:
f.setAction({cTrigger: "Format", cScript: "AFSpecial_Format(0);"});
f.setAction({cTrigger: "Keystroke", cScript: "AFSpecial_Keystroke(0);"});
These set the field to Zip format and it works well. Similarly the following set it as Phone number format.
f.setAction({cTrigger: "Format", cScript: "AFSpecial_Format(3);"});
f.setAction({cTrigger: "Keystroke", cScript: "AFSpecial_Keystroke(3);"});
It is the double quotes enclosing "mm/dd/yyyy" that seems to create the problem.
Copy link to clipboard
Copied
OK, as single quotes are also legal, try putting the format string between single quotes; in that case, there should be no need for backslashes.
Copy link to clipboard
Copied
Late to the party here but I think I have an answer and it lies in a linked reference:
http://www.acrotex.net/blog/wp-content/uploads/2011/07/pdfblog_22.pdf
It goes into great detail how to apply formatting to a date field.
Copy link to clipboard
Copied
The acrotex site is down. Does anyone have some other source?
Copy link to clipboard
Copied
I guess this one is the same: https://www.math.uakron.edu/~dpstory/PDFBlog/pdfblog_22.pdf
I have attached it to this post.
Regards,
--Julio