Skip to main content
Obi-wan Kenobi
Brainiac
November 3, 2016
Question

Create an array of arrays from a .txt file!

  • November 3, 2016
  • 2 replies
  • 4877 views

Hi all scripters,

The beginning is a .txt file containing data as:

text_1;text_12

text_2;text_23

text_3;text_39

I'm able to get an array as:

[text_1;text_12, text_2;text_23, text_3;text_39, …]

But I need that:

[ ["text_1","text_12"], ["text_2","text_23"], ["text_3","text_39"], …]

I've absolutely no idea about the way to get this array of arrays!!

Thanks for your ideas!

(^/)

This topic has been closed for replies.

2 replies

Wosven
Participating Frequently
November 6, 2016

Hi,

Here my “obscure” version with error-handling and a final pop-up listing which layer was renamed.

I hope I'll be able to write as fine scripts as you do Peter,

Swo

//DESCRIPTION:Rename layers from text file

/*

Text file should contains 2 columns (old name;new name), line by line

separator is ";", can be changed at line 27

If script is unable to find layer_names.txt, it asks for a file.

No check for forbidden characters in names:

you need to check the text file before running the script.

*/

(function () {

    if (app.locale.toString() == "FRENCH_LOCALE") {

        var infos = {

            findFile:"Choisir le fichier contenant la liste de noms :",

            msg:" calque(s) renommé(s) :"

        };

    } else {

        var infos = {

            findFile:"Select the text file for layer names:",

            msg:" layer(s) renamed:"

        };

    }

    // Modify text file's name and separator (sepa) if needed

    var txtList = "layer_names.txt",

        sepa= ";";

  

    var oNames = [], nNames = [], rNames = [];

    var myListFile = File(myFindFile(txtList));

    // datas

    var testNames = listNames();

    if (testNames && oNames.length == nNames.length) {

        var myLayers = app.activeDocument.layers, c = 0;

      

        for (var L = 0; L < myLayers.length; L++) {

            var testN = false;

            for (var n = 0; n < oNames.length && !testN; n++) {

                if (myLayers.name == oNames && !app.activeDocument.layers.itemByName(nNames).isValid) {

                    myLayers.name = nNames;

                    // list renamed layers

                    rNames.push(oNames + " => " + nNames);

                    // ensure old name doesn't conflict with same new name :

                    oNames = "";

                    testN = true;

                    c++;

                }

            } // for n

        } // for L

        alert(c + infos.msg + "\n" + rNames.join("\n"));

    }

    function listNames() {

        // Open the file for reading

        myListFile.open("r");

        var text = myListFile.read();

        var lines = text.split("\n");

        if (lines.length > 0) {

            for (var y =0; y < lines.length; y++) {

                var l = lines;

                if (l.length != 0) {

                    if (l[0] !== "") {

                        var names = l.split(sepa);

                        if (names.length == 2) {

                            oNames.push(names[0]);

                            nNames.push(names[1]);

                        }

                    }

                } // l.length

            } // for

            return true;

        } // lines.length

        return false;

    }

    //////// FUNCTIONS ////////////

    function myFindFile(myFilePath) {

        var myScriptFile = myGetScriptPath();

        var myScriptFile = File(myScriptFile);

        var myScriptFolder = myScriptFile.path;

        myFilePath = myScriptFolder + "/" + myFilePath;

        if(File(myFilePath).exists == false) {

            //Display a dialog.

            myFilePath = File.openDialog(infos.findFile);

        }

        // else alert("ok");

        return myFilePath;

    }

    function myGetScriptPath() {

        try {

            myFile = app.activeScript;

        }

        catch(myError){

            myFile = myError.fileName;

        }

        return myFile;

    }

}());

Community Expert
November 4, 2016

Hi Obi-wan,

one split() and one loop with another split() can get you there.

Depending on the line separator—\n or \r—that could be:

var array1 = string.split("\r");

var array2 = [];

for(var n=0;n<array1.length;n++)

{

    array2[array2.length++] = array1.split(";")

};

Regards,
Uwe

Obi-wan Kenobi
Brainiac
November 4, 2016

Hi Uwe,

It seemed interesting but I can't make it work in my script.

The deal: I have 4 Layers [Layer_1, Layer_2, Layer_3 and Layer_4] and I want to change their name but not necessarily for all!

So I have a list (.txt file):

Layer_1;Layer_12

Layer_2;Layer_23

To validate the code, I've temporarily pasted this list directly into the .jsx:

myList = ["Layer_1;Layer_12","Layer_2;Layer_23"];

var myLayers = app.activeDocument.layers;

for (var L = 0; L < myLayers.length; L++) {

        var myLayerName_0 = myLayers.name;

        var mLength_0 = myLayerName_0.length;

        for (var N = 0; N < myList.length; N++) {

                var mList_0 = myList;

                myConcordance = mList_0.slice(0,mLength_0);

                myLayerName_1 = mList_0.slice(mLength_0+1,mList_0.length);

                   

                    if ( myLayerName_0 == myConcordance ) var myLayerName_0 = myLayerName_1;

        }

}

The writing seems logical but nothing happens in the Layers panel! 

Tired! Thanks in advance! 

(^/)

Obi-wan Kenobi
Brainiac
November 6, 2016

Uwe and Swo,

I agree with both of you! 

Uwe is totally right!  "With Great Power Comes Great Responsibility!"

My Master Yoda would have said:

"Powerful the Force is! Powerful Javascript and Grep are also!

Easy is to go to the dark side and errors make!

The errors we must tame!"

(^/) 

I think the user has to first control his list quality! [as Uwe enumerated greatly!]

An alert [Ok to continue, Cancel to abort] could tell him: "Your list must be as …!"

Imho, the real problem the script could encounter is when a layer name already exists, as:

The concordances list is:

Layer_1;Layer_8

Layer_2;Layer_9

As you can see, there's a problem with Layer_8!

So, as Uwe did, I've taken his code and inserted a double if:

myList = ["Layer_1;Layer_8","Layer_2;Layer_9"]; 

myLayers = app.activeDocument.layers; 

for ( L = 0; L < myList.length; L++ ) {

    var error = false; 

    parts = myList.split (';');

    if ( myLayers.everyItem().name.join(";").match(parts[1]) ){ error = true }; 

        if( error ){

            alert ( "This layer name '" + parts[1] + "' already exist! \rThe script is going to abort! Correct it!\r(^/)  ;-)" );

            exit();

        }

    myLayers.item(parts[0]).name = parts[1];

}

Thanks for your comments! [I go on to study!]

(/^)


Uwe,

I need to study more what you wrote in post#19!

My way above is not really cool! 

(^/)