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

Custom Dialog Boxes and Case Switch problems

Nifty
Registered: Jan 16 2011
Posts: 17
Answered

I have had one issue with my custom index search dialogue that I didn't resolve but circumnavigated.
I wanted to use a Case Switch operation to set one variable based on the return value of a dropdown list.
I have been unable to get the case switch to work but have used an if else sub to reach the same end (though less tidily I think).
 
The code that isn't working is:
 
var MatchOption = "";
switch (MatchOpt) {
case "Match Exact word or phrase":
MatchOption = "MatchPhrase";
break;
case "Match Any of the words":
MatchOption = "MatchAnyWord";
break;
case "Match All of the words":
MatchOption = "MatchAllWords";
break;
default: MatchOption = "Help"
}
 
MatchOpt is the return variable from the dropdown box and its value has been set to the current selection using the commands 'var oRslt = dialog.store();' and 'MatchOpt = (this.GetListSel(oRslt["pop1"],path))?path.reverse():"";' higher up in the code.
 
I added app.alert boxes into the code at the end of each case statement and then added the default: "Help" and another app.alert, to identify what the two variables were recorded as at each point through the process.
The return variable from the dropdown, MatchOpt, was correct (depending on the dropdown setting) at every stage but MatchOption remained null ("") regardless of what it should have read (one of the 3 case options) until it picked up the default value.
 
Would my problem be that the return value from the dropdown is a string containing multiple words, eg "Match Exact word or phrase" and the Case operation will only work on a single text word, eg. case "MatchExact": ~etc., rather than case "Match Exact word or phrase": ~etc. ?
 
Regards,
 
Nifty Styles
 
Norfolk, England
 
Using Acrobat 8.2.5 Professional

My Product Information:
Acrobat Pro 8.1.7, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Yes, that would be a problem as the switch command only works with true matches (the same as using the == operator).
If you want to account for partial matches you need to use something else, like the match() or indexOf methods.
See here for more info: http://w3schools.com/jsref/jsref_obj_string.asp

Also, in your default clause you're not assigning the value to MatchOption.

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

Nifty
Registered: Jan 16 2011
Posts: 17
Thank you for the reply.

Not assigning the value to MatchOption in the default clause was a transcription error when I wrote the code into the thread.
It was assigned in the real code and was working correctly when tested by inserting an app.alert after the assigning line.
(I have now edited the first post to correct this error).

With respect to the switch command only working with true matches, I'm not sure we are on the same track.
The code above is using exact (true) matches. The wording within the quotation marks after 'case' is the exact wording of the 3 options that MatchOpt can be.
What I'm trying to do is convert the full text returned from the dropdown box selection into the correct wording for the 'wordMatching' property of the search method .... so later on in the coding I have a line that says:
search.wordMatching = MatchOption.


If I switch from using a 'switch case' to using an 'if else' then the process works, so I now have in my coding:

//Assess the word matching option
var MatchOption = "";
if( MatchOpt == "Match Exact word or phrase" )
{
MatchOption = "MatchPhrase";
}
else if( MatchOpt == "Match Any of the words" )
{
MatchOption = "MatchAnyWord";
}
else
{
MatchOption = "MatchAllWords";
}
//

This is all working and the whole dialogue is doing what I wish it to .. I'd just like to understand why the switch case method (which looks tidier) wouldn't work for me.
Is it because it will not accept the nature of the MatchOpt value (i.e multiple words with spaces between) or is it because I have got the coding wrong .... or does it not work at all within a dialogue?

I'm new to javascript and so I'm learning on the go as it were and a lot of my learning has come from online material, most of which is concerned with using these form tools (dropdowns, checkboxes etc) in forms and not in dialogue boxes and I have found there are differences in the code between the two .. so I may be barking up the wrong tree or just have completely nonsense coding !!
Nifty
Registered: Jan 16 2011
Posts: 17
Accepted Answer
I've copied below a reply that Thom Parker posted on another thread of mine:

thomp wrote:
Even if a string contains multiple words, its still a single string of characters. There is no problem with it being used in a case statement. I suspect the issue might be with a type conversion. The list functions used by AcroDialogs return an array of strings.
Do an explicit conversion to string before using the return value.

For example:
switch(myDropVar.toString())
{
case "Select all users":
}



Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
The JavaScript Console Window (Video tutorial)

Following Thom's advice has cured the problem.

Thank you,

Nifty.