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

Recreating JavaScript with Copying and Pasting

prander
Registered: Nov 6 2008
Posts: 44
Answered

I promise, I've researched this issue and I haven't had much luck finding a usable answer.

I was actually happy with how useful chrisconverse's lesson "Changing another field with Combo Box Selection" was at /print/636; it was exactly what I needed! I was mindful of changing out the names of my fields to his and even carefully read through his directions, but somehow, I'm afraid I'm just not getting it. In my script below, rather than pre-populating 3 fields, I'm only changing 1 (note: the script is half written because there was a lot of typing left to do and I was afraid of just what happened: the script wasn't accepted!). I get a repeated warning about missing a : at Line 4.

Another issue I seem to have is understanding the spacing in the scripts that I see on the various forums and webpages or some of the things I've attempted to write. Is that a space in between the slashes? Are there actually carriage returns or tabs (in the version on chrisconverse's tutorial)? This might also be what trips me up. I've seen Thom P's tips on c/p scripting, but I need a better understanding of that.

BTW, scripting is not a key job function for me, but I've found it useful to learn and would love to introduce more such documents and forms to my 'company'

Thanks in advance,
Page

// Place all pre-population data into a single data structure

var JobData = { Toll Collector:{ Func2: "MUST be able to lift 35 pounds. Standing 6 or more hours per day. Pushing and pulling, twisting, reaching and bending. May involve stair climbing." },
Lane and Plaza Supervisor:{ Func2: "MUST be able to lift 65 pounds. Standing 6 or more hours per day. Pushing and pulling, twisting, reaching and bending. May involve climbing stairs." } };

function SetFieldValues(cJobTitle)
{
// Populate fields with values from the Job Data Object
this.getField("JobFunc2").value = JobData[cJobData].Func2;
}

My Product Information:
Acrobat Pro Extended 9.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
First the '\' in a character string is known as an escape character and denotes that a special character is following.

" - use as a quote within a quoted string
\ - insert the escape character
n - new line
r - line return
t - tab

When creating a data object, the element names can not have spaces within the name unless the name is a quoted character string.
// job description -3 line description var JobData = { "Toll Collector": {Func2: "MUST be able to lift 35 pounds. Standing 6 or more hours per day. \n\nPushing and pulling, twisting, reaching and bending. \n\nMay involve stair climbing." },"Lane and Plaza Supervisor": {Func2: "MUST be able to lift 65 pounds. Standing 6 or more hours per day.  \n\nPushing and pulling, twisting, reaching and bending. \n\nMay involve climbing stairs." }};

George Kaiser

prander
Registered: Nov 6 2008
Posts: 44
G'
I appreciate your response, but I'm not sure I explained myself enough.

The code you wrote added lines to the supposed pre-populated response. I don't need that. That string of "must be able to lift...standing 5 or more..." can be in one long sentence.

My spacing issues come from me not understanding where programmers add/get the spacing in the actual script. When I paste someone else's code (or try to recreate it by actually typing it), it presents itself with 20+ spaces between one part of the script and the next. Are programmers actually spacing over 20 times between a { and another { or some other character or letter?? What appears to me as a string of spaces in a pasted script may very well have originated as tabbing.

The below code was from the referenced tutorial found in the A' Users community. It does exactly what I want: the end user picks a name (in below example department name; in my case a job title) and 3 subsequent fields pre-populate based on that choice (below, contact, e-mail and number; in my case, one field with a brief description of job duties, no multi-lines needed).

Thanks again,
Page

This is copied exactly from the scripting window from Advanced>Document Processing>Document JavaScripts (fyi, the actual script has extra carriage returns at the end after the very last }; is there a purpose to all that spacing??)// Place all prepopulation data into a single data structure
var DeptData = { Accounting:{ contact: "Steala Damuni",
email: "accounting [at] mycomp [dot] com",
deptnum: "cmp1234" },
Engineering:{ contact: "Frank N. Stien",
email: "engineering [at] mycomp [dot] com",
deptnum: "eng1234" },
Marketing :{ contact: "Shelly Oughtbuks",
email: "marketing [at] mycomp [dot] com",
deptnum: "mkt1234" },
ITSupport:{ contact: "Goah Wei",
email: "it [at] mycomp [dot] com",
deptnum: "its1234" }};
function SetFieldValues(cDeptName)
{
this.getField("DeptContact").value = DeptData[cDeptName].contact;
this.getField("DeptEmail").value = DeptData[cDeptName].email;
this.getField("DeptNumber").value = DeptData[cDeptName].deptnum;
}
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Spacing or white space in the code may make the code more readable. For example one can write JavaScript without the terminating ';' as long as the next statement starts on a new line. But if you place multiple JavaScript statements on a single line, you will get an error unless you use the statement terminating ';'.

The "//" denote a single line comment and the "/*" starts a multi line comment and that multi line comment ends with "*/". And comments can add a better description of what the code is doing or other needed data than just a listing of the code.

You copied the code, but you did not keep the same exact name construction for the elements of the data structure. You will note that the element names of the data element have no spaces. If the name could be separated by a space. For example, ' ITSupport' could have been written as 'IT Support', but the code creating the data object would not work because the element name, IT Support, can not have a space in the element name. You will get the following error on the JavaScript console:

Quote:
JavaScript error at line 12: missing : after property id
11:. Fix it?
If you add the quotes around the string 'IT Support' the error goes away:
// Place all prepopulation data into a single data structurevar DeptData = { Accounting:{ contact: "Steala Damuni",email: "<span class="spamspan"><span class="u">accounting</span> [at] <span class="d">mycomp [dot] com</span></span>",deptnum: "cmp1234" },Engineering:{ contact: "Frank N. Stien",email: "<span class="spamspan"><span class="u">engineering</span> [at] <span class="d">mycomp [dot] com</span></span>",deptnum: "eng1234" },Marketing :{ contact: "Shelly Oughtbuks",email: "<span class="spamspan"><span class="u">marketing</span> [at] <span class="d">mycomp [dot] com</span></span>",deptnum: "mkt1234" },// added quotes to allow a space in the element name  "IT Support":{ contact: "Goah  Wei",email: "<span class="spamspan"><span class="u">it</span> [at] <span class="d">mycomp [dot] com</span></span>",deptnum: "its1234" }};function SetFieldValues(cDeptName){this.getField("DeptContact").value = DeptData[cDeptName].contact;this.getField("DeptEmail").value = DeptData[cDeptName].email;this.getField("DeptNumber").value = DeptData[cDeptName].deptnum;}

And if you select "IT Support" it will match the element name "IT Support".

George Kaiser

prander
Registered: Nov 6 2008
Posts: 44
I think this is finally making a lot more sense!! The quotes around the example of IT Support is something that I definitely need and might have been what was giving me my error code. I'm going to accept the answer, but I may have a few...more...questions. ;-)
prander
Registered: Nov 6 2008
Posts: 44
Here's one for you: I noticed at the end of your error message the question "Fix it?" Is there a function to fix it automatically? I'm hit another brick wall in that it's telling me "missing ; before statement 2: at line 3"

I'm not exactly sure what part of this script qualifies as 'statement 2':

“Lane or Plaza Supervisor”: { function: “MUST be able to lift 60 pounds, standing 6 or more hours per day.”},
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
There is no automatic 'fix' function. The question requires a "Yes" or "No" answer that do you want to try to fix the problem or not.

The posted quote is the text of the pop-up box if one adds a space to the string 'ITSupport'. If you answer 'Yes' then the JavaScript editor opens with the problem code. If you answer "No" the erroneous script is left as is.

George Kaiser