Copy link to clipboard
Copied
Hi --I am working on a script to read data from a text file and use within a dialog. But I have hit a wall.
I have used a script from a previous post that sets a variable from the text document when the script user chooses it from a dropdown.
var myDialog = app.dialogs.add({name:"Map",canCancel:true});
with(myDialog){
with(dialogColumns.add()){
with(borderPanels.add()){
staticTexts.add({staticLabel:"Choose location:"});
with(dialogColumns.add()){
var file = File("~/Desktop/myPlacesfile.txt");
file.open("r");
var str = file.read();
file.close();
var myPlaceList = str.split(/[\r\n]+/);
var myPlaceMenu = dropdowns.add({stringList:myPlaceList, selectedIndex:0});
}
}}}
var myResult = myDialog.show();
if(myResult == true){
if (myPlaceMenu.selectedIndex == 0){
var myPlace = "- Not Defined -";
}else{
var myPlace = myPlaceList[myPlaceMenu.selectedIndex];
alert(myPlace);
}
myDialog.destroy();
}
This is what I need to do now:
The text file is in this format:
value1 [TAB] value2
value1 [TAB] value2
value1 [TAB] value2
I need to have the dialog pulldown only show value 1, and after the user selects it, the script returns only value 2. (The alert is just there for testing -- I am doing something else with the variable).
Is there a way to display the first part of a tab delimited line in the pulldown and return the second half as a variable?
Any help would be greatly appreciated.
thanks
1 Correct answer
One of the possibilities is this. Create an array with values left of the tab for the dialog. Then create an object which you use as a look-up table. Roughly as follows:
// same as what you have
file.open("r");
var str = file.read();
file.close();
var array = str.split(/[\r\n]+/);
// "pairs" is the look-up table
var pairs = {};
// as before, "myPlaceList" will be used for the dropdown
var myPlaceList = [];
var v;
for (var i = 0; i < array.length; i++)
{
v = array.split ("\t");
pairs [v[0]] = v[1];
myPl
Copy link to clipboard
Copied
One of the possibilities is this. Create an array with values left of the tab for the dialog. Then create an object which you use as a look-up table. Roughly as follows:
// same as what you have
file.open("r");
var str = file.read();
file.close();
var array = str.split(/[\r\n]+/);
// "pairs" is the look-up table
var pairs = {};
// as before, "myPlaceList" will be used for the dropdown
var myPlaceList = [];
var v;
for (var i = 0; i < array.length; i++)
{
v = array.split ("\t");
pairs [v[0]] = v[1];
myPlaceList.push (v[0]);
}
// add the dropdown as before:
var myPlaceMenu = dropdowns.add({stringList:myPlaceList, selectedIndex:0});
// the look-up table "pairs" is used as follows: pairs ["value1"] returns "value2"
// so in your script that would be:
var myPlace = pairs [myPlaceList[myPlaceMenu.selectedIndex]];
Peter
Copy link to clipboard
Copied
Very nice!
I didn't know you can use a RegExp as an argument for split()!
Very good to know!
Harbs
Copy link to clipboard
Copied
Thanks Peter -- that worked great.
Terrific solution.
Jim
Copy link to clipboard
Copied
I was hoping I could prevail on you for one more issue:
I am now adapting this script into scriptUI.
Here's a snippet:
//This correctly displays my drop down list.
myDialog.myPlaceMenu = myPanel.add('dropdownlist', [230,58,630,78], myPlaceList);
myDialog.myPlaceMenu.items[0].selected = true;
//This is where I run into a problem. The 'if' seems to be doing what is is supposed to but I cannot figure out the correct wording in the "else" part for the myPlace variable when the user chooses something other than the first item.
if (myDialog.myPlaceMenu.items[0].selected == true){
var myPlace = "- Not Defined -";
}else{
var myPlace = pairs [myPlaceList[myDialog.myPlaceMenu.selected]];
alert (myPlace);
}
Any help would be greatly appreciated.
thanks
Copy link to clipboard
Copied
Hi -- Nevermind -- I figured it out (found an earlier post where you answered the same question).
var myPlace = pairs [myPlaceList[myDialog.myPlaceMenu.selection.index]];
thanks again,
Jim

