Quantcast
Channel: Adobe Community : Popular Discussions - Illustrator Scripting
Viewing all articles
Browse latest Browse all 12845

A basic question/problem with array element as undefined

$
0
0

Hello everybody,

 

thank you for looking at my problem. I'm very new to scripting and javaScript and I've encountered a strange problem. I'm always trying to solve all my problem myself, with documentation (it help to learn) or in the last instance with help of google. But in this case I am stuck. I'm sure its something very simple and elementary.

 

Here I have a code which simply loads a text file (txt), loads the content of the file in to a "var content". This text file contents a font family name, each name on a separate line, like:

Albertus

Antenna

Antique

Arial

Arimo

Avant

Barber1

Barber2

Barber3

Barber4

Birch

Blackoak ...etc

 

Now, I loop trough the content variable, extract each letter and add it to the "fontList[i]" array. If the character is a line break the fontList[i] array adds another element (i = i + 1); That's how I separate every single name into its own array element;

 

The problem which I am having is, when I loop trough the fontList array and $.writeln(fontList[i]) the result in the console is:

 

undefinedAlbertus

undefinedAntenna

undefinedAntique

undefinedArial ...etc.

 

I seriously don't get it, where the undefined is coming from? As far as I have tested each digit being added into the array element, I can't see anything out of ordinary.

----------------------------

----------------------------

Here is my code:

 

#target illustrator

var doc = app.documents.add();

 

//open file

var myFile = new File ("c:/ScriptFiles/installedFonts-Families.txt");

var openFile = myFile.open("r");

 

//check if open

if(openFile == true){

    $.writeln("The file has loaded")}

else {$.writeln("The file did not load, check the name or the path");}

 

 

//load the file content into a variable

var content = myFile.read();

myFile.close();

 

var ch;

var x = 0;

var fontList = [];

 

 

for (var i = 0; i < content.length; i++) {

    ch = content.charAt (i);

        if((ch) !== (String.fromCharCode(10))) {

            fontList[x] += ch;

            }

        else {

            x ++;

            }

    }

 

 

for ( i = 0; i < fontList.length; i++) {

   $.writeln(fontList[i]);

  }

 

 

doc.close (SaveOptions.DONOTSAVECHANGES);

 

-----------------

-----------------

Thank you for any help or explanation. If you have any advice on how to improve my practices or any hint, please feel free to say. Thank you


Viewing all articles
Browse latest Browse all 12845


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>