Skip to main content
Participating Frequently
September 3, 2021
Question

Specific Selection to Merge Text

  • September 3, 2021
  • 2 replies
  • 2246 views

Hello all, beginner-ish javascripter here. I've been looking into creating a script for my job to automate the merging of text elements on my floorplans, and have thus far been successful. The main() function in my script takes a single text group and merges the elements inside the group together, to form one block of text, then groups it again.

The issue I am having is with the next step, which is running a loop to perform the main function on each appropriate text item in the plan at once. I believe the issue I'm running into doesn't have to do with the main function, as it works just fine if the user goes in and selects each text piece they want to merge. But that's just a theory, and after banging my head against this for the past 6 hours, I'm running out of ideas.

I'll omit the main() function for now, as it's pretty long. This is my latest attempt:

var aDoc = app.activeDocument;
var mergeItems = [];
aDoc.activeLayer = aDoc.layers ["360text"];

cull();
function cull(){
    aDoc.activeLayer.hasSelectedArtwork = true;
    var textItems = aDoc.selection;
    for(var i=0i < textItems.lengthi++){
        var select = textItems[i];
        var textsLen = select.pageItems.length;
        if (textsLen >= 2){
            mergeItems.push(select);
        }
    }
}



for(var i=0i <mergeItems.lengthi++){
    aDoc.selection = null;
    mergeItems[i].selected = true;
    main();
    cull();
}




This topic has been closed for replies.

2 replies

Silly-V
Legend
September 4, 2021

I am not sure about your code's objectives too, but the one script example that comes to mind is the John Wundes tie-text script. Here is a Graphics stackexchange answer that explains everything: https://graphicdesign.stackexchange.com/questions/57730/merge-point-text-objects-into-one-text-area-in-illustrator

 

If you'd like to merge broken text while preserving as much of the formatting, placement, paragraphs and other typography of the existing text as you can, rather than pasting into a newly created text area as plain text, you can try John Wundes' amazing Join Text Frames script.

Josh OAuthor
Participating Frequently
September 7, 2021

Appreciate the help, but after testing it, Wundes' script essentially does the same thing as my main() function. My goal is to automate this process, so that rather than the user going through and selecting each text element and running the script, they can hit one button and the script will go through and select each text element, merge it, and move on to the next.

femkeblanco
Legend
September 4, 2021

1.  Your goals, both general and specific to the shared code, are unclear to me. If the below doesn't fix your problem, can you elaborate on this a little more?

 

2.  The purpose of cull() seems to be to push items with 2+ children items (i.e. groupItems and/or compoundPathItems) into the array mergeItems. As it is, "select.pageItems" will throw an error if "select" doesn't have any "pageItems". You can delete this line and change the test to

 

if (select.typename == "GroupItem") {

 

3.  You then iterate the array mergeItems, calling cull() for each element. Is this intentional?

Josh OAuthor
Participating Frequently
September 7, 2021

Appreciate the help, unfortunately the script still throws up errors. Let me explain the goal of this script in greater detail. Firstly, my main function. What this essentially does is take the user's selection whether it's grouped or ungrouped, and merges the text together. The base code is found here:  http://www.ajarproductions.com/adobe_extensions/ai/MergeText_AI_CS1.js

I built on and modified it by adding functionality for rotated text, sorting options, and centering the paragraph style. As far as I know, this part of the code works flawlessly, assuming the user makes a valid selection.

Secondly, the automation portion. All I really need this function to do is parse through each text item in the document, and merge it if it needs to be merged. So, for example, see the attached image. I need the script to be able to find each of the circled items, and individually merge them, while skipping over the other text items. Assuming the main function works as intended, all I need to do here is select each element that needs to be merged and run the main function on it.

In the original code above, I call cull() for each element because the main() function does some ungrouping and regrouping, so I wasn't sure if this would mess up the mergeItems array or not, so I figured redefining the array for each element would solve for this. Sorry if the original posting was vague, hopefully this helps a bit.

femkeblanco
Legend
September 7, 2021

I can't run Ajar's script as it is. There is at least one undefined non-native function (newOminoDialog). I can run Wundes' script, so I think I understand what you mean by merging textFrames. So the next question is:  How are you deciding which two (or more) textFrames to merge at a time?