Skip to main content
Known Participant
June 28, 2010
Answered

Reading tab delimited data from a text file (JS CS3)

  • June 28, 2010
  • 1 reply
  • 5820 views

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

This topic has been closed for replies.
Correct answer Peter Kahrel

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

1 reply

Peter KahrelCorrect answer
Adobe Expert
June 29, 2010

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

Harbs.
Brainiac
June 29, 2010

Very nice!

I didn't know you can use a RegExp as an argument for split()!

Very good to know!

Harbs