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

Sort text in an array based on Y coordinate

Engaged ,
Apr 11, 2018 Apr 11, 2018

Copy link to clipboard

Copied

I am trying to find the x and y coordinates to some text frames. Once I get the x, y... I would like to sort them by the y coordinate. Which ever line is highest up should be the first in an array it gets passed to.

So using this as an example.....

lines.JPG

Then some written out code (it doesn't function...just trying to get my point across)...

var doc = app.activeDocument;

var allText = doc.textFrames;

var textPos = [x, y];

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

textPos.push(allText.position);

}

textPos.sort(Y);

The output of textPos would be

textPos[0] = "This line of text";

textPos[1] = "then another line";

textPos[2] = "here is some more";

Then I will join them together to make one string...

var finalSentence = textPos.join();

finalSentence will be:
"This line of text then another line here is some more"

Separated by a space between each array item.

Any help would be much appreciated!

TOPICS
Scripting

Views

556

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

correct answers 1 Correct answer

People's Champ , Apr 12, 2018 Apr 12, 2018

var doc = app.activeDocument; 

var allText = doc.textFrames;

var n  = allText.length;

var textArray = [];

while (n--) textArray = allText;

var sortFn = function(a,b){

return a.top < b.top;

}

textArray.sort( sortFn );

n = textArray.length;

var strs = [];

while (n--) strs = textArray.contents;

alert (strs.join(" ") );

Votes

Translate

Translate
Adobe
People's Champ ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

var doc = app.activeDocument; 

var allText = doc.textFrames;

var n  = allText.length;

var textArray = [];

while (n--) textArray = allText;

var sortFn = function(a,b){

return a.top < b.top;

}

textArray.sort( sortFn );

n = textArray.length;

var strs = [];

while (n--) strs = textArray.contents;

alert (strs.join(" ") );

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 ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

Thank you for your help and response!

Can you explain to me how to "access" just the y coordinates when using textFrame.position?

If I do an alert it shows me the x and y coordinates.... but how do I access them specifically?

Sorry I am just not getting it and I feel like this is something I need to learn/understand.

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 ,
Apr 16, 2018 Apr 16, 2018

Copy link to clipboard

Copied

Ok.... so I try not to throw out all of my code and sometimes it makes it harder for me to incorporate someone's suggestions. I am a designer... not a programmer. But... I am trying

So @Loic.Aigon

Any chance you could SHOW me how to incorporate it into my blocks of code???

Any help would be greatly appreciated!

#target illustrator-22

var doc = app.activeDocument;

var allText = doc.textFrames;

var boldText = app.textFonts.getByName("Arial-Black");

var titleLines = [];

// GET THE TITLE AND TYPE OF DOCUMENT

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

    var textFieldRange = allText.textRange;

    var textProps = textFieldRange.characterAttributes;

    if (textProps.textFont == boldText && allText.contents == "Electrical System") {

        var mediaType = allText.contents;

        // Electrical Publication information

        var dpso = "00000321";

        var smcs = "7566";

    } else if (textProps.textFont == boldText && (allText.contents == "Hydraulic System" ||

            allText.contents == "Hydraulic System - Attachment" || allText.contents == "Powertrain System" ||

            allText.contents == "Air System" || allText.contents == "Cooling System")) {

        var mediaType = allText.contents;

        // Hydraulic Publication information

        var dpso = "00000989";

        var smcs = "5050";

    } else if (textProps.textFont == boldText && (allText.contents != "Electrical System" || allText.contents != "Hydraulic System" ||

            allText.contents != "Hydraulic System - Attachment" || allText.contents != "Powertrain System" ||

            allText.contents != "Air System" || allText.contents != "Cooling System")) {

        // This is where I need to sort by the Y coordinate. This could be anywhere from 1 line up to 4 lines

        // Again I need it whichever allText.position is highest on Y to be the first line of the title

        titleLines.sort(SORT IT BY Y)

        titleLines.join(Put together in order with a space between)

    } else {

        mediaType == null;

    }

}

Thank you for sticking with me! Lines 29 - 32 are my notes and psudo code

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
People's Champ ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

position is an array so you  need to adress the specific property per its index like position[0]

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
People's Champ ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

Well I think it's all in your hands now. Just build your array of text frames, then use the sort array function I provided.

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

LATEST

Thank you! I did use your function and it works great! Not sure why I couldn't figure it out... looks like walking away from it for a day and coming back was what I needed to see it. LOL

Thank you again for the help!

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