• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Sorting 2 Arrays

Engaged ,
Mar 01, 2018 Mar 01, 2018

Copy link to clipboard

Copied

I am pulling 2 arrays and having them be reformatted and spit out next to the artboard. I can't seem to get the first column of names to sort. Then once they are sorted I need the second column to sort with the first column.

Here is my script....

#target illustrator-22

var doc = app.activeDocument;

var allLayers = doc.layers;

var layerRename = "Grid Location";

var triggerAlert = 0;

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

    if (allLayers.name == "42 Pg Grid Location" || allLayers.name == "36 Pg Grid Location" ||

        allLayers.name == "30 Pg Grid Location" || allLayers.name == "24 Pg Grid Location" ||

        allLayers.name == "20 Pg Grid Location" || allLayers.name == "16 Pg Grid Location" ||

        allLayers.name == "12 Pg Grid Location" || allLayers.name == "8 Pg Grid Location" ||

        allLayers.name == "4 Pg Grid Location") {

        allLayers.name = layerRename;

        triggerAlert++;

    } else if (allLayers.name == layerRename) {

        triggerAlert++;

    }

}

if (triggerAlert == 1) {

    function findGrid() {

        var d = activeDocument,

            ps = d.layers['Grid Location'].pathItems,

            l = ps.length,

            o = 'ABCDEFGHIJKLMNOPQRST...',

            W, H,

            rows = columns = 1,

            origin = d.rulerOrigin,

            /*newOrigin = new Array(2),*/

            newOrigin = new Array(0, 0),

            t, b, center, gx, gy,

            i = 0;

        for (; i < l; i++) {

            if (ps.width == 0) {

                columns++;

                !newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3];

                !H && H = ps.height;

            }

            if (ps.height == 0) {

                rows++;

                !newOrigin[0] && newOrigin[0] = origin[0] + ps.geometricBounds[2];

                !W && W = ps.width;

            }

        }

        d.rulerOrigin = newOrigin;

        W = W / columns;

        H = H / rows;

        t = app.selection;

        b = t.geometricBounds;

        center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];

        gx = Math.abs(center[0]) / W;

        gy = Math.abs(center[1]) / H;

        d.rulerOrigin = origin;

        xCoor = -50;

        yCoor = 0;

        createGridText = d.textFrames.add();

        createGridText.position = [xCoor, yCoor = (inc -= 20)];

        createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);

    }

    inc = 0;

    incTwo = 0;

    d = activeDocument;

    for (var z = 0; z < selection.length; z++) {

        xC = -350;

        yC = 0;

        createComponentText = d.textFrames.add();

        createComponentText.position = [xC, yC = (incTwo -= 20)];

        createComponentText.contents = selection.contents;

        findGrid();

    }

    var doc = app.activeDocument

    var allText = doc.textFrames;

    formatCompTable();

    removeLamps();

    removeUnderscores();

    /*  CODE TO FORMAT SWITCH, RELAY, SOLENOID, SENSOR, GROUND, ETC. */

    function formatCompTable() {

        var aCon;

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

            //alert(allText.layer);

            //if (allText.layer == "[Layer Hypertext]"){

            aCon = allText.contents;

            if (aCon.match(/_SWITCH|SWITCH_|SWITCH/) != null) {

                aCon = aCon.replace(/_SWITCH|SWITCH_|SWITCH/, "");

                aCon = "SWITCH - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_SW(?!\w)|SW_/) != null) {

                aCon = aCon.replace(/_SW|SW_/, "");

                aCon = "SWITCH - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_RLY|RLY_/) != null) {

                aCon = aCon.replace(/_RLY|RLY_/, "");

                aCon = "RELAY - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/RELAY/) != null) {

                aCon = aCon.replace(/RELAY/, "");

                aCon = "RELAY - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_FUSE|FUSE_/) != null) {

                aCon = aCon.replace(/_FUSE|FUSE_/, "");

                aCon = "FUSE - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_SOL(?!\w)|SOL_|SOL(?!\w)/) != null) {

                aCon = aCon.replace(/_SOL|SOL_|SOL/, "");

                aCon = "SOLENOID - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_SNSR|SNSR_/) != null) {

                aCon = aCon.replace(/_SNSR|SNSR_/, "");

                aCon = "SENSOR - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_SENSOR|SENSOR_/) != null) {

                aCon = aCon.replace(/_SENSOR|SENSOR_/, "");

                aCon = "SENSOR - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/GND($^)/) != null) {

                aCon = aCon.replace(/GND/, "");

                aCon = "GROUND - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_GROUND|GROUND_/) != null) {

                aCon = aCon.replace(/_GROUND|GROUND_/, "");

                aCon = "GROUND - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/GND/) != null) {

                aCon = aCon.replace(/GND/, "");

                aCon = "GROUND - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_ALARM|ALARM_/) != null) {

                aCon = aCon.replace(/_ALARM|ALARM_/, "");

                aCon = "ALARM - " + aCon;

                allText.contents = aCon;

            }

            /*else if (aCon.match(/_ECM|ECM_/) != null) {

                       aCon = aCon.replace(/_ECM|ECM_/, "");

                       aCon = "CONTROL - " + aCon;

                       allText.contents = aCon;

                   }*/

            else if (aCon.match(/_CONTROL|CONTROL_/) != null) {

                aCon = aCon.replace(/_CONTROL|CONTROL_/, "");

                aCon = "CONTROL - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/CTRL/) != null) {

                aCon = aCon.replace(/CTRL/, "");

                aCon = "CONTROL - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/RESISTOR/) != null) {

                aCon = aCon.replace(/RESISTOR/, "");

                aCon = "RESISTOR - " + aCon;

                allText.contents = aCon;

            } else if (aCon.match(/_CAN|CAN_/) != null) {

                aCon = aCon.replace(/_CAN|CAN_/, "");

                aCon = "RESISTOR - CAN " + aCon;

                allText.contents = aCon;

            } //else if (aCon.match(/RES(?=\s)|RES_/) != null) {

            else if (aCon.match(/_RES|RES_/) != null) {

                aCon = aCon.replace(/_RES|RES_/, "");

                aCon = "RESISTOR - " + aCon;

                allText.contents = aCon;

            } //else if (aCon.match(/MOTOR|MOT(?=\s)|MOT_/) != null) {

            else if (aCon.match(/_MOTOR|MOTOR_/) != null) {

                aCon = aCon.replace(/_MOTOR|MOTOR_/, "");

                aCon = "MOTOR - " + aCon;

                allText.contents = aCon;

            }

        }

    }

} else {

    alert("Couldn't find the proper Grid Location layer format. \nMake sure the layer with your grid locations is labeled...\nGrid Location");

}

/*  CODE TO FORMAT LAMPS */

function removeLamps() {

    var aCon;

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

        aCon = allText.contents;

        if (aCon.match(/_LAMP|LAMP_/) != null) {

            aCon = aCon.replace(/_LAMP|LAMP_/, "");

            aCon = "XXXX LAMPS - " + aCon;

            allText.contents = aCon;

        } else if (aCon.match(/_LTS|LTS_/) != null) {

            aCon = aCon.replace(/_LTS|LTS_/, "");

            aCon = "XXXX LAMPS - " + aCon;

            allText.contents = aCon;

        }

    }

}

/*  CODE TO REMOVE UNDERSCORES */

function removeUnderscores() {

    var searchString = /_/g;

    var replaceString = " ";

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

        var thisTextFrame = allText;

        var newString = thisTextFrame.contents.replace(searchString, replaceString);

        if (newString != thisTextFrame.contents) {

            thisTextFrame.contents = newString;

        }

    }

}

var theContents;

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

    theContents = allText.contents;

    theContents = myTrim(theContents);

    allText.contents = theContents;

}

removeDoubleSpaces();

function myTrim(x) {

    return x.replace(/^\s+|\s+$/gm, '');

}

function removeDoubleSpaces() {

    var searchString = /  /g;

    var replaceString = " ";

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

        var thisTextFrame = allText;

        var newString = thisTextFrame.contents.replace(searchString, replaceString);

        if (newString != thisTextFrame.contents) {

            thisTextFrame.contents = newString;

        }

    }

}

function removeDoubleDashes() {

    var searchString = /--/g;

    var replaceString = "-";

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

        var thisTextFrame = allText;

        var newString = thisTextFrame.contents.replace(searchString, replaceString);

        if (newString != thisTextFrame.contents) {

            thisTextFrame.contents = newString;

        }

    }

}

removeDoubleDashes();

alert("Your component table has been successfully created!");

Here is a test file....

Test File.ai - Google Drive

A couple bonus items I would love to do.... no selection needed. Anything that is on the Hypertext layer is what would get used for column 1. Would also love to spit out a .csv file to import into Excel.

This may be too much to ask at one time. If so.... I apologize. Any help would be appreciated though!

TOPICS
Scripting

Views

918

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Advocate ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

Bonjour,

This script is a real gas plant ?

que faire avec ces blocs de texte ?

usine a gaz.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

The example file I posted is a stripped down version of the file the script runs on. There is A LOT of text in the actual file. The blocks of text that the script creates are put into Excel and then sorted by the first column name.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

can you just merge the two arrays before sorting them?

var array1 = [3,5,2,4,1];

var array2 = [8,6,10,9,7];

for(var x=0,len=array2.length;x<len;x++)

{

     array1.push(array2);

}

function sortMyArray(array1)

{

     //do the sorting

     //return sorted array

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

Hi wiiliam,

(I don't understand the question of the TO).

But why you loop through the array for merging?

why not concat()

array1 = array1.concat(array2);

or better

var array3 = array1.concat(array2);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

only because i didn't know it off the top of my head. Your way is clearly better.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

Thank you both for your suggestions. I am not a programmer.... (as I am sure you can clearly see from my code). So I am kind of stumbling my way through this the best I can. I will see if I can implement the concat() and sort() to make this happen.

Again I appreciate your input!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

Hi subieguy2​,

I see parts of code (written by myself in the past) mixed with other code.

Some parts are cumbersome and very slow.

Eg this parts with dozens of "ORs" is really heavy

  1. for (var i = 0; i < allLayers.length; i++) { 
  2.     if (allLayers.name == "42 Pg Grid Location" || allLayers.name == "36 Pg Grid Location" || 
  3.         allLayers.name == "30 Pg Grid Location" || allLayers.name == "24 Pg Grid Location" || 
  4.         allLayers.name == "20 Pg Grid Location" || allLayers.name == "16 Pg Grid Location" || 
  5.         allLayers.name == "12 Pg Grid Location" || allLayers.name == "8 Pg Grid Location" || 
  6.         allLayers.name == "4 Pg Grid Location") { 
  7.         allLayers.name = layerRename; 
  8.         triggerAlert++; 
  9.     } else if (allLayers.name == layerRename) { 
  10.         triggerAlert++; 
  11.     } 

Maybe a better and quicker way

var regNme = /\d+\sPg\sGrid\sLocation/;

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

    if (regNme.test(allLayers.name) == true) {

        allLayers.name = layerRename;

        triggerAlert++;

    } else if (allLayers.name == layerRename) {

        triggerAlert++;

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

To deal with a limited and pre-determined list of names, I usually put them into a static array and also make sure the indexOf prototype for arrays is included in the beginning of my code. Then it's a simpler loop using indexOf, where only the pre-ordained keys are used.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

pixxxel schubser​ yes you are correct. a lot of people on this form have helped me get the code functional. i am trying to learn.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

subieguy2  schrieb

… a lot of people on this form have helped me get the code functional. i am trying to learn.

Trying to write your own code is very good. I know it is a steep learning curve.

But "mixed" codes often make problems that are hard to detect and mostly slow. Did you understand what Silly-V​ and I tried to say?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

pixxxel schubser I see you used a regExp to search quickly through the desired search phrases. Testing to see if it is true. Then changing the name. Which makes sense.

Silly-V​ is saying he would have used something like this...

var doc = app.activeDocument;

var allLayers = doc.layers;

var layerRename = "Grid Location";

var triggerAlert = 0;

var layerNamesArray = ["42 Pg Grid Location", "36 Pg Grid Location", "30 Pg Grid Location", "24 Pg Grid Location", "20 Pg Grid Location", "16 Pg Grid Location", "12 Pg Grid Location", "8 Pg Grid Location", "4 Pg Grid Location"];

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

    for (var z = 0; z < layerNamesArray.length; z++) {

        if (layerNamesArray == allLayers.name) {

            allLayers.name = layerRename;

            triggerAlert++;

        } else if (allLayers.name == layerRename) {

            triggerAlert++;

        }

    }

}

Again I can't say how much I appreciate the guidance. Luckily programming isn't my full time job and I am still gaining efficiency with my clunky code. lol!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

Yes.

There are so many possibilities. With less or more efficiency, accuracy, speed …

… depending on the number and the naming of the layers and your document structure.

Another (quick) way sometimes could be to provoke an exception (not useful in your case😞

try {

    doc.layers.getByName("Grid Location");

    // layer found

    }

catch (e) {/* layer not found */}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 03, 2018 Mar 03, 2018

Copy link to clipboard

Copied

Bonjour subieguy2,

Je pense avoir trouvé la réponse à ta question :

- Dans ton script la fonction findGrid() est appelée pour  chaque texte, les propriétés de la grille calculs sont calculées x fois

    (calculs superflus).

- Pour ma part, j'évite de déplacer les règles (d.rulerOrigin = newOrigin;)

- les fonctions doivent être placées en général en dehors du programme principal.

- Il faut bien distinguer les variables globales et locales.

Je propose donc un script (partiel) qui corrige ces problèmes.

Je joins le script et le fichier xlx produit par un simple copié/collé dans Excel suivi d'un trie par noms.​

https://share.orange.fr/#qtyljtDtTW70f188003

de elleere, (de France)

// JavaScript Document pour Illustrator
// elleere Sat, 3 March 2018 14:07:42 GMT
//------------
var d = app.activeDocument;
var Origin = d.rulerOrigin;
var origX = -Origin[0]; // left && Top ActiveArtboard
var origY = -Origin[1]+d.height;
var decx = 300; // position de la liste à gauche ActiveArtboard
var cdr = true; // true affiche le cadre
var liste = ""; // liste  à afficher
var propG;    // objet propriétés de la grille

  if (selection) {
    propG = findGrid(selection);
      for (var z = 0; z < selection.length; z++) {
        if (selection.typename = "textFrame") {
          liste +=selection.contents+String.fromCharCode(9);
          liste += rang(selection,propG.p,propG.w,propG.h,propG.c)+"\r";
        }
      }
  }
  createGridText = d.textFrames.pointText([origX-decx,origY]);
  createGridText.contents = liste.substr(0,liste.length-1);
//---------------
function findGrid(t) {
    var ps = d.layers[d.layers.length-1].pathItems;
    var l = ps.length;
    var W, H, w, h, b, center, rows, columns;
    var posGrille = [],
        rows = columns = 1;

        for (var i = 0; i < l; i++) {
            if (ps.width == 0) {  // vertical
                columns++;
                !posGrille[1] && posGrille[1] = ps.geometricBounds[3];
                !H && H = ps.height;
            }
            if (ps.height == 0) {  // horizontal
                rows++;
                !posGrille[0] && posGrille[0] = ps.geometricBounds[0];
                !W && W = ps.width;
            }
        }
        w = W / columns; // width column;
        h = H / rows; //    height row;
        if (cdr) {cadre (d,posGrille[1]+H,posGrille[0],W,H);}
        return  {p:posGrille,w:w,h:h,c:columns,r:rows};
    }
//---------------
function cadre (relativeObjet,y,x,w,h) {
    var rect = relativeObjet.pathItems.rectangle(y,x,w,h);
        rect.filled = false;
        rect.stroked = true;
        rect.strokeWidth = 1;
        rect.strokeDashes = [3, 1.5];
}
//---------------
function rang(t,pos,w,h,col) {
    var b, center, rangW, rangH;
        b = t.geometricBounds;
        center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];
        rangW = col+1-Math.ceil(Math.abs(center[0]-pos[0])/w);
        rangH = Math.floor(Math.abs(center[1]-pos[1])/h);
        return  String.fromCharCode(65+rangH )+ "-" + rangW;
}
//---------------

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 06, 2018 Mar 06, 2018

Copy link to clipboard

Copied

I will try to work that into my code...thank you so much! So I am trying to make this more efficient again just to learn.

If I write the following

var doc = app.activeDocument;

var allText = doc.textFrames;

var searchFor = /SW(?!ING)|SWITCH|SOL|SOLENOID|GND|GROUND|RLY|RELAY/gi;

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

    var textFrameContents = allText.contents;

if (textFrameContents.match(searchFor) != null) {

                textFrameContents.replace(searchFor, "");

                textFrameContents = "SWITCH - " + textFrameContents;

                allText.contents = textFrameContents;

    }

}    

How can I make this regExp use the .replace() for multiple search terms?

So in other words if it finds SOL I want it to remove the SOL from whatever position it is in the textFrame. Then put "SOLENOID - " at the beginning of that textFrame.

If it finds GND or GROUND I want it to remove the GND or GROUND from whatever position it is in the textFrame. Then put "GROUND - " at the beginning of that textFrame.

Instead of having the super long list of if else searches. Just wondering if it is possible?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 06, 2018 Mar 06, 2018

Copy link to clipboard

Copied

LATEST

pixxxel schubser​ I have been using this website to have it help me learn more about regExp

https://regexr.com/

I love that I can test and it explains my regExp....or in some cases... your regExp 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 02, 2018 Mar 02, 2018

Copy link to clipboard

Copied

Pourquoi ne pas créer une liste avec tabulations :
liste = "";

liste += selection.contents+String.fromCharCode(9);
liste += findGrid()+"\r";

findGrid() {
-
-
return o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);
r}

à la fin du script afficher la liste

usine a gaz.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines