Skip to main content
Obi-wan Kenobi
Legend
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
Legend
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! 

(^/)

Wosven
Participating Frequently
November 6, 2016

Uwe,

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

My way above is not really cool! 

(^/)


Obi-wan,

I really like :

"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!"

And I would add :

"And great scripts must have!"

An alert ([Ok to continue, Cancel to abort] could tell him: "Your list must be as …!") would teach us users to be carefull with the text file content.

Too many can be anoying too...

Perhaps you can play with a "while" (be carefull, those are tricky), to rename the layer adding a +" 2", +" 3" or +" copy 1", +" copy 2"... in the name.

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

    // renaming, etc.

    ...

}

This part would change for:

if (myLayers.name == oNames) {

    // "a" will increase until there's no more layer with the same name

    var a = "2";

    // testName will change value until it's an unique layer name

    var testName = nNames;

    while (app.activeDocument.layers.itemByName(testName).isValid) {

        testName = nNames + " " + a;

        a++;

    }

    myLayers.name = testName;

    // list renamed layers

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

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

    oNames = "";

    testN = true;

    c++;

}

This feels more like a puzzle now

Have fun,

Swo