These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Help with Batch Changes to Fields

tomGusp
Registered: Dec 5 2011
Posts: 3
Answered

Hi Everyone,
 
I'm a bit turned around here, and I'm hoping someone can point me in the right direction. I am trying to make a batch process to update the contents of a Combobox that I have on a few hundred forms. The form is an FCC document and the Combobox contains addresses for different locations in the US that I ship to frequently(I have hundreds of them because of the numerous different commodities that need to be shipped). The Combobox is set up to populate a text field in the form with the correct address based on the location name chosen from the list. To do this I have a custom keystroke script that I have input in the "format" tab of the Combobox. This script matches an items export value with an array containing the addresses, and then populates the appropriate field. At this point everything is working well. The problem now is that I need to add a couple of addresses to the array, and the corresponding items to the Combobox, and I need to distribute that change throughout all of my forms.
 
I have been trying to set up a batch action that will create a new Combobox with the appropriate items and export values. The part I am stuck on is how to get the custom Keystroke script that contains my address array into the newly created Combobox. Would this be done through setAction using the cScript parameter? If so then maybe I need a bit of help with how to properly add a script within that parameter (syntax etc.) I should also mention that if there is a way to overwrite the script in the combobox I already have set up, that would be a preferable course.
 
I am quite new to JavaScript, but have managed to bungle my way in this far with the help of this forum and our good friend Google. If anyone can help me clarify this I would greatly appreciate it.

My Product Information:
Acrobat Pro 10.1, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You have a number of options here. First, it is a simple process to modify the items in the existing combobox and to modify the script. You don't need to create a new field. As you've discovered, the script is modified with field.setAction() function. This function will overwrite the existing keystroke script.

You find detailed info on modifying list fields(i.e. combobox) in this article.
http://acrobatusers.com/tutorials/2007/js_list_combo_livecycle/

I suspect that you need to modify the script because the address information is contained in it? And that this script is very long? Since you are new to JS this is going to cause a problem. There are a lot of small details for getting this process right, such as setting up the scripts as a string, which means escaping quotes and line feeds. However, once you create the batch process it well become easy to make future changes.

Now, I mentioned that you have lots of options. It may be easier to go another route. One thing you can do is remove the location selection from the forms altogether. Look at this article:
http://acrobatusers.com/articles/getting-external-data-acrobat-x-javascript

Using the information in this article, you can create a folder level script that allows the keystroke script to access data in an external file. Or you can create an automation script that removes the process entirely from the actual form.

I also just wrote up a set of really detailed articles and examples on lists and auto-populating form data at www.pdfscripting.com. This is a membership site all about learning and using Acrobat JavaScript. I think you'll find it massively useful.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

tomGusp
Registered: Dec 5 2011
Posts: 3
Hi Thomp,

Thanks for your reply. You have certainly given me a number of options. You are right about my script containing address info and being quite long. I would be interested to see how to set up a script as a string, and what a the code would end up looking like if I went this route. Could I deal with the escaping quotes by using a single quote for my setAction, and a double quote for the enclosed script? I'm afraid I don't know how line feeds would look in this situation. I am starting to get an idea of how it would work, but I need some help with how to properly code after cScript.(and I am now working within the combobox that is already there, thanks for that)

var f = this.getField("District Name");
f.setAction({cTrigger:'Keystroke', cScript:'(function () {
if (!event.willCommit) {

// Set up an array of addresses. \r = carriage return
var aAddr = [];
aAddr[0] = "address 1";
aAddr[1] = "address 2";
aAddr[2] = "address 3";

// Get the export value of the selected item
var ex_val = event.changeEx;

// Get the corresponding address
var addr = aAddr[ex_val];

// Populate the text field with the address
getField("FCC-8").value = addr;
}

})();


Everything after "cScript:" is the shortened but otherwise verbatim keystroke script, and is obviously not formatted properly for this situation. I'm also not sure how to close it all off, but maybe getting a sense of how a multi-line script within the cScript parameter functions will help me to fill that in.

I do like the idea of calling to an external document to populate my address text field. I get a sense it would be cleaner that way. Since it is a totally different direction than I have been going it may take me a bit to wrap my head around it, but I am definitely going to explore the links you added. www.pdfscripting.com is intriguing, I will pay a visit. Thanks again!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You have to close it off with the same quote symbol you used to open it. The single-quote (') in this case.
To spread a string variable over multiple lines you need to finish each line with a backslash, like so:

var str = "This is the start,\
and this is the rest of the string";

If you want to insert actual line breaks, use the escape character "\n";

var str = "This is the first line\nand this is the second line of the string";

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
Mr. 67 has pointed out only a few of the issues you'll run into trying to manually build a long string definition (And he didn't explain the line continuation character;) It may be much simpler to use a different technique to convert it into a sting.

Try this, create a new blank PDF and add a single text field to it. Make the field big. Set the options so the field is multiline, and scrolls. Name the field "MyJScript". Now paste your script into the field.

This code, run from the console window, will convert the field text into the definition of a string object.

this.getField("MyJScript").value.toSource();

This gets you going on your original idea. Try if with some short text before doing the whole script, so you get a feel for how it works.

But before going down this path I would suggest trying out the example in the article I referenced above:
http://acrobatusers.com/articles/getting-external-data-acrobat-x-javascript

You may like this method much better, and it is much easier to change.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

tomGusp
Registered: Dec 5 2011
Posts: 3
Wow, thank you both for the responses.

try67,

That helps to understand how the concept and syntax work, thanks for that.

thomp,

What a fantastic way of converting a multi-line function into string. This worked perfectly, and has me at least up and running to perform the changes I need to on these documents. My "shortened for this example" code now looks like this:

var f = this.getField("District Name");
f.setAction({cTrigger:'Keystroke', cScript:'(function () {\rif (!event.willCommit) {\r\r// Set up an array of addresses. \\r = carriage return\rvar aAddr = [];\raAddr[0] = \"address 1\";\raAddr[1] = \"address 2\";\raAddr[2] = \"address 3\";\r\r// Get the export value of the selected item\rvar ex_val = event.changeEx;\r\r// Get the corresponding address\rvar addr = aAddr[ex_val];\r\r// Populate the text field with the address\rgetField(\"FCC-8\").value = addr;\r}\r\r})();'})

You're right, I don't fully understand everything that's going on to make this string work, but your method has simplified it so I don't have to. I would recommend this to anyone who has a similar need.

My ultimate goal is to make applying changes to multiple customs forms possible for someone who does not know JavaScript. I am now working on the links you sent, and will attempt calling items from another document into the appropriate field. If I succeed I will post what that looks like. If I fail, you will hear from me again.

Regards,

tomGusp