Posted: 2008-10-31
Copying and Pasting Code
I just want to say a few words about code reuse. Copying and pasting JavaScript code from the forums, articles, and sample docs available on this site as well as others is probably the most popular usage model. Let's face it, most people are not professional programmers, and after all, that's what those scripts are there for. And for anyone who is not a programmer, copied code brings a very high value to the table.
However, as anyone who has done this knows, it's rare that you can copy a script verbatim. Most scripts are written for the specific document they are attached to, or if they are posted in an article or forum message they might be written more as pseudo-code, which provides the structure of a program but doesn't fill in the details. In these cases you don't have to be a programmer to make the appropriate changes for your usage. You just have to pay attention to the details. Remember, whoever wrote the code spent time and effort designing the code and probably years learning what they needed to be able to write the code in the first place. It didn't come free and easy to them and if you want to benefit from it you should spend a little time and effort as well.
So here are some rules, or things to think about when you copy someone else's code for your own use.
- You're situation is unique. It may be very common or similar to someone else's situation, but if it wasn't unique in some way it wouldn't require a custom code solution.
- As such, it's particularly important for you to be aware of what makes it unique. Most often it's the names of things, like form fields. But it may also be structure, a copied script might affect one field, but you need it to affect two fields.
- Being aware of what makes your form unique tells you how a copied script needs to be changed.
- The essence of a script is it's structure. It is a pattern of action. But in order to connect this pattern of action to a specific thing, like a document, all of the scripts elements have to be named. It's the names that make the connection between the pattern and the actual application.
- When it comes to reusing code, names are everything.
- Always be aware of the names of the elements on your form and the elements in the script.
- If you duplicate a section of code in a script, all the names have to be changed so the script doesn't overwrite itself.
- If you copy code for manipulating form fields the field names in the script have to exactly match the names of the fields on your document.
- If a script uses a group of fields together, such as a calculation for a row or column in a table. Then there is probably a pattern to the field names. In this case you don't just change the names in the script, you will probably have to rename your form fields to match the naming pattern in the script. You don't necessarily use the names in the script, you use the the naming pattern used in the script.
- Did I mention that names are everything?
- Every function and variable in a script requires a unique name. Not to say that there aren't situations where the same name can be used for different things in different places. But even then it's a bad idea. If you ever have to look at a piece of code again it's a good idea to use short, descriptive, and unique names for everything so there is no confusion.
- Be Aware of the names of things in other scripts on your document. If you copy a script that uses a name that already exist in your document, even if they are in different document level scripts, they may interfere with one another.
- With out a detailed analysis of your particular document and needs, and this is especially true for LiveCycle forms, no one can just give you an exact solution.
- Designing scripts from the ground up takes time, effort, and a detailed knowledge of the particulars. This is really your job, so if you are copying scripts, be kind to the developer, you're getting their invaluable help for free.
- If you are asking for advice on how to modify a script, be specific and clear. Provide all necessary information.
- And since it's your job, it's your responsibility to make the appropriate changes to the copied script. Remember, it's the names that count.
- Ok, values are important as well. Often you'll have to change the values used in a script. But this is less important to making the script work.
- Yeah, you need the correct values to make the script work correctly, but to make the script work at all the names have to be right.
- Names first, then values.
- And for heaven's sake, don't forget to relax:)
Comments
add new commentCopy and Paste
This is some practical and important information. You are right, most people are not programmers and these rules will help us all a great deal when trying to copy and paste. Maybe they will start looking right. Finding the time to gamble at an online casino can be difficult but finding the best casino online casino italiani payouts and casino bonuses is not difficult at all, they are right here.
How to print an attachment (dataObjects) using JavaScript
Good morning Thom,
I have seen a lot of your posts on Acrobat JavaScript and was wondering how to ask you a question, which I currently have.
I also apologize for posting this question here. And now the question.
I have a form developed using LiveCycle Designer ES 8.2 with a button to add attachment (dataObjects) and another button to open the attachment.
What I need next is to create a button to open/print the attachment (dataObjects) in at the same time. Is this feasible using JavaScript? Or may be you have a better solution to recommend.
loralon
Another submitForm() question: I want the XML
Greetings Thom,
I have read a great number of your posts and would like to convey my gratitude for your sharing of expertise. I have learned a good number of things from your posts, but have managed to hit the wall on a custom solution whereby I'm trying to create and submit a custom XML file to send to an backend SAP/CRM system.
In my javascript I have managed to create the XML document,
and parse it via the XMLData.parse() method. My problem comes in when I try do the submitForm() call using my custom XML object. The code looks like this:
var cXMLDoc = myScript.GenerateXML(cXMLDoc);
var xdData = XMLData.parse(cXMLDoc, false);
var cToAddr = "mailto:mysap@work.org";
var cSubLine = "Call Report Submit";
var cBody = "See attachment.";
var cEmailURL = "mailto:" + cToAddr + "?subject=" + cSubLine + "&body=" + cBody;
event.target.submitForm({cURL:strEncodURL, cSubmitAs:"XML", oXML:xdData, cCharSet:"utf-8"});
This all works, just as you have mentioned in various other posts, right up to the point where outlook mail pops up and
displays "Outlook blocked access to the following potentially unsafe attachments: Acr8326.tmp"
I believe outlook has a problem with the attachment file extension. I have not been able to find any parameters for controlling the attachment name. This problem doesn't occur when sending other file types or if I remove the oXML parameter it creates the file attachment .xml as expected.
Any insight would be greatly appreciated.
PS: I too could not find an email to contact you directly so I took a shot in the dark that you might come across this.
Help!!
I would like to email you but I can't find any email address. Somehow my question is related to you post "Copying and Pasting Code".
My wife (in her company) is using acrobat 5.0.
I found some script regarding merging pdfs from below url
http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=139
it works well except one issue.
When I need to create bookmark with Chinese text (e.g. 測試TESTING), the bookmark can only been seen in the acrobat 5.0. When I tried to open the file in other version's reader e.g. 8.0. All the bookmark become strange characters. I searched the web but still can't a concrete answer for this.
Would u pls help provde any advice to me? (or this issue is known bug for the "BookMarkRoot.createChild"?
You advice should be very helpful.
Look forward to hear from you.
Yu
yipchunyu@gmail.com
p.s. forgot to mention, she is still using 5.0.0 without update with the latest patch (her company restricted this). Does the latest patches solve this issue?
Copy & Paste
Thom,
As an absolute newbie Acrobat programmer the information this site and you in particular provides in invaluable to the learner.
I am trying to use the code you published on this site for "Automatically Adding a Button to a PDF document".
For the life of me I cannot get it to work.
I keep getting the reply Mynextbutton not defined.
Do you know if the code on the page is correct or is it my learner stupidity causing it?
Anyway thanks for all your submissions!
Mark Green
markg@gisoz.com
Defining Variables
Hello Mark,
Whenever Acrobat JavaScript returns the error "Not Defined" it's because the variable in question either doesn't exist or has an "undefined" value. To figure this out you'll need to look at where the "Mynextbutton" gets its value, make sure the variable is defined and getting a value. Another thing to check over is the spelling of this variable. JavaScript is case sensitive so the variable name has to be exactly the same everywhere. Its very common to mispell names.
If you have further issues please post to the forum and let me know you have a question there.
Cheers,
Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]
The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.html]http://www.adobe.com/devnet/acro