0
[CS3 JS] Predefining the selectedIndex in a DropDown menu
New Here
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/td-p/1080929
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Hello wise-ones,
My situation is such that I have a dialogbox that allows the user to select a country of the world via a dropdown menu:
> with(dialogColumns.add()){
var file = File("~/countries.txt");
file.open("r");
var str = file.read();
file.close();
var myLandList = str.split(/[\r\n]+/);
var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});
}
The choice is stored:
> if (myLandMenu.selectedIndex == 0){
var myLand = "- Not Defined -";
}else{
var myLand = myLandList[myLandMenu.selectedIndex];
}
Now - my problem..........
At some stage - it could be that the user whishes to make changes.
My stumbling stone is that I'd like the dropdown list to display the
b previously selected country
as the
b selectedIndex
!
I'm sure it must be simple -
simply search for
i myLand
in the
i myLandList
and then set the
i selectedIndex
as the
i position
of myLand within the myLandList Array....
Sounds easy when I tell that myself - but it's got me cornered and stuck!
Can anyone please help me - point me in the right direction?
Thank you in advance,
Lee
My situation is such that I have a dialogbox that allows the user to select a country of the world via a dropdown menu:
> with(dialogColumns.add()){
var file = File("~/countries.txt");
file.open("r");
var str = file.read();
file.close();
var myLandList = str.split(/[\r\n]+/);
var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});
}
The choice is stored:
> if (myLandMenu.selectedIndex == 0){
var myLand = "- Not Defined -";
}else{
var myLand = myLandList[myLandMenu.selectedIndex];
}
Now - my problem..........
At some stage - it could be that the user whishes to make changes.
My stumbling stone is that I'd like the dropdown list to display the
b previously selected country
as the
b selectedIndex
!
I'm sure it must be simple -
simply search for
i myLand
in the
i myLandList
and then set the
i selectedIndex
as the
i position
of myLand within the myLandList Array....
Sounds easy when I tell that myself - but it's got me cornered and stuck!
Can anyone please help me - point me in the right direction?
Thank you in advance,
Lee
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080930#M275740
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Do you mean the previous time the script was run?
Peter
Peter
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080931#M275741
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Insert a label somewhere. For example, you might want to have the dialog sensitive to the active document, so you could have a document label that you seek out and set:
If (app.documents[0].extractLabel("Selected Country") == "") {
// document doesn't contain a previous selected country
} else {
// it does so use it
}
Then, on receiving an OK from the user after the dialog, you can set the new value:
app.documents[0].insertLabel("Selected Country", myLandList[myLandMenu.selectedIndex]);
If you use the actual name of the country rather than its index in the list, it would allow for adding new countries at some point in the future, but if you do that, you'll need iterate through the list to get the index on the way in.
If you just want a single value across all documents, do the same thing but insert the label in the application object.
Dave
If (app.documents[0].extractLabel("Selected Country") == "") {
// document doesn't contain a previous selected country
} else {
// it does so use it
}
Then, on receiving an OK from the user after the dialog, you can set the new value:
app.documents[0].insertLabel("Selected Country", myLandList[myLandMenu.selectedIndex]);
If you use the actual name of the country rather than its index in the list, it would allow for adding new countries at some point in the future, but if you do that, you'll need iterate through the list to get the index on the way in.
If you just want a single value across all documents, do the same thing but insert the label in the application object.
Dave
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Lee_Fielding_
AUTHOR
New Here
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080932#M275742
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Thanks,
OK....
Peter:
> Yes - The land was chosen from a script that was run at an earlier time/day/month....
Dave:
> Sorry to say - but I'm a little confused from your reply! I'm not such an expert at this JavaScripting stuff!
Actually I have a list with all countries of the world. The list must not be edited.
I'm inexperienced with 'Labeling' if you can believe me?
The bigger picture:
The user has set up a document with aid of a 'creation' script - where they chose the Land and entered certain other values.
Then - it could be that the user wants to update the document using a 'modification' script, and changes values.
Now I would like to display the Land dopdown list again - BUT - have the previously chosen land as the initially selected option and not simply
b selectedIndex:0
Did this help - or maybe you knew it anyways, and I'm the confused one! :)
Sorry if I anoy you as I don't know much about Label thingy?
Regards,
Lee
OK....
Peter:
> Yes - The land was chosen from a script that was run at an earlier time/day/month....
Dave:
> Sorry to say - but I'm a little confused from your reply! I'm not such an expert at this JavaScripting stuff!
Actually I have a list with all countries of the world. The list must not be edited.
I'm inexperienced with 'Labeling' if you can believe me?
The bigger picture:
The user has set up a document with aid of a 'creation' script - where they chose the Land and entered certain other values.
Then - it could be that the user wants to update the document using a 'modification' script, and changes values.
Now I would like to display the Land dopdown list again - BUT - have the previously chosen land as the initially selected option and not simply
b selectedIndex:0
Did this help - or maybe you knew it anyways, and I'm the confused one! :)
Sorry if I anoy you as I don't know much about Label thingy?
Regards,
Lee
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080933#M275743
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Many objects in InDesign have a label. document is one. You could do e.g.
>app.activeDocument.label = 'Honduras';
to set the document's label to Honduras. You could then later retrieve this using this statement:
>myLand = app.activeDocument.label;
You then need to determine Honduras's index in the list and you can preselect Honduras.
Peter
>app.activeDocument.label = 'Honduras';
to set the document's label to Honduras. You could then later retrieve this using this statement:
>myLand = app.activeDocument.label;
You then need to determine Honduras's index in the list and you can preselect Honduras.
Peter
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080934#M275744
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Well, Peter and I are both pointing you in the same direction. I'd use a keyed label rather than the single label property because it makes sure that you don't accidentally use the same label for more than one thing.
I practically wrote the code for you, so I'm not sure what you're missing.
Dave
I practically wrote the code for you, so I'm not sure what you're missing.
Dave
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Lee_Fielding_
AUTHOR
New Here
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080935#M275745
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
Thanks Peter, Thanks Dave,
OK - I actually have a characterStyle that I read in to see what the langauge was
i myCurrentLand
- so there's no problem finding the name of the land:
> app.findTextPreferences.appliedCharacterStyle = myLandCharStyleName;
f = myDoc.findText();
if (f[0].contents == "- Not Defined" | f[0].contents == ""){
var myCurrentLand = "- Not Defined -";
}else{
var myCurrentLand = f[0].contents;
}
My problem is that - what Peter said....
i You then need to determine Honduras's index in the list and you can preselect Honduras.
I don't know how to find
i myCurrentLand
's index in my
i myLandList
string!
:(
I've just made a 'work around' where myCurrentLand is inserted into the top of myLandList:
> with(dialogColumns.add()){
var file = File("~/countries.txt");
file.open("r");
var str = file.read();
file.close();
str = myCurrentLand+"\r---//---\r"+str;
var myLandList = str.split(/[\r\n]+/);
var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});
}
OK - I actually have a characterStyle that I read in to see what the langauge was
i myCurrentLand
- so there's no problem finding the name of the land:
> app.findTextPreferences.appliedCharacterStyle = myLandCharStyleName;
f = myDoc.findText();
if (f[0].contents == "- Not Defined" | f[0].contents == ""){
var myCurrentLand = "- Not Defined -";
}else{
var myCurrentLand = f[0].contents;
}
My problem is that - what Peter said....
i You then need to determine Honduras's index in the list and you can preselect Honduras.
I don't know how to find
i myCurrentLand
's index in my
i myLandList
string!
:(
I've just made a 'work around' where myCurrentLand is inserted into the top of myLandList:
> with(dialogColumns.add()){
var file = File("~/countries.txt");
file.open("r");
var str = file.read();
file.close();
str = myCurrentLand+"\r---//---\r"+str;
var myLandList = str.split(/[\r\n]+/);
var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});
}
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080936#M275746
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
function getIndex (myObj, myList) {
for (var j = myList.length - 1; j >= 0; j--) {
if (myObj -- myList) return j
}
return -1
}
I just wrote that here, so there might be a minor syntax error.
Dave
for (var j = myList.length - 1; j >= 0; j--) {
if (myObj -- myList
}
return -1
}
I just wrote that here, so there might be a minor syntax error.
Dave
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Lee_Fielding_
AUTHOR
New Here
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080937#M275747
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
🙂 🙂 :)
Thank you Dave - Thank you very much.
My final script was:
> for (var j = myLandList.length - 1; j >= 0; j--) {
if (myCurrentLand == myLandList) {
myLandIndex = j;
}
}
var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:myLandIndex});
Works wonderfully!
It just goes to show that my boss should allow me to do the basic JavaScripting course....
Thanks once again.
Lee
Thank you Dave - Thank you very much.
My final script was:
> for (var j = myLandList.length - 1; j >= 0; j--) {
if (myCurrentLand == myLandList
myLandIndex = j;
}
}
var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:myLandIndex});
Works wonderfully!
It just goes to show that my boss should allow me to do the basic JavaScripting course....
Thanks once again.
Lee
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080938#M275748
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
As long as you're confident that myCurrentLand is guaranteed to be in the list you're fine. I think I'd do this:
for (var myLandIndex = myLandList.length - 1; myLandIndex >= 0; myLandIndex--) {
if (myCurrentLand == myLandList[myLandIndex]) break;
}
}
etc.
This way, you're absolutely guaranteed that myLandIndex actually has a value when you come to use it and you don't go around the loop any more times than is absolutely necessary.
Dave
for (var myLandIndex = myLandList.length - 1; myLandIndex >= 0; myLandIndex--) {
if (myCurrentLand == myLandList[myLandIndex]) break;
}
}
etc.
This way, you're absolutely guaranteed that myLandIndex actually has a value when you come to use it and you don't go around the loop any more times than is absolutely necessary.
Dave
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Lee_Fielding_
AUTHOR
New Here
,
LATEST
/t5/indesign-discussions/cs3-js-predefining-the-selectedindex-in-a-dropdown-menu/m-p/1080939#M275749
Jul 18, 2008
Jul 18, 2008
Copy link to clipboard
Copied
:)
Keeping it simpler - the better!
Something else I've learnt!
Thanks you.
Keeping it simpler - the better!
Something else I've learnt!
Thanks you.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

