Skip to main content
YannickDeSmetBE
Participating Frequently
October 13, 2022
Question

Split textframe based on

  • October 13, 2022
  • 1 reply
  • 132 views

I'm working on a script that should seperates a textframe based on it's paragraphstyle.

 

Example:

A textframe has a formatted newspaper article. 

This textframe should be split and shaped into an intro, a title, a leading, etc...

I'm able to seperate the textframe based on size, but often this is not sufficient.

Sometimes there is no intro or a leading, so it would be handy if the script could find out what the paragraph style is, so I could redefine how the textframe should be shaped.

 

// Get the selected textFrame
var doc = app.activeDocument;
var sel = app.selection[0];
if(sel.hasOwnProperty('baseline')) {
	var tf = sel.parentTextFrames[0];
} else if (sel instanceof TextFrame) {
	var tf = sel;
}

// Change the dimensions of the textFrame for Intro
sel.geometricBounds = [
    y1,
    x1,
    y1+heightIntro,
    x1+widthIntro
];


// Create new textframe for Title and link it to the Intro textframe
var tf2 = doc.pages[0].textFrames.add();
tf2.previousTextFrame = tf;
tf2.properties = {
    geometricBounds: [
        y1+boxIntro,
        x1,
        y1+boxIntro+boxTitle,
        x1+tfWidth
    ]
};

......

 

Is such a thing possible? Knowing I'll have to manipulate each textframe accordingly.

Thanks ahead!

This topic has been closed for replies.

1 reply

brian_p_dts
Community Expert
Community Expert
October 14, 2022

Newspapers provide a rich playground for automation. That's how I got my start. Don't be afraid to use separate functions so you don't have to repeat yourself. Below is an idea of a potential approach, where you split off the split function, and reassign the text frame you're connecting to in that function. This is not perfect; you need to figure out how to handle the first text frame, and I also don't know the separation space you desire. But maybe this starts you on the right path to think about the solution. 


var main = function() {
    var sel = app.selection[0];
    if(sel.hasOwnProperty('baseline')) {
        var tf = sel.parentTextFrames[0];
    } else if (sel instanceof TextFrame) {
        var tf = sel;
    }
    
    var rangs = tf.textStyleRanges;
    var n = rangs.length;
    var i = 0;
    var rx = /intro|title|leading|body/gi;
    for (i; i < n; i++) {
        if (rx.test(rangs[i].appliedParagraphStyle.name)) {
            tf = splitTf(tf);
        }
    }

}

var splitTf = function(tf) {
    var tfb = tf.geometricBounds;
    var x1 = tfb[1];
    var y1 = tfb[0];
    //var boxIntro = ?????;
    //var boxTitle = ?????;
    var tfWidth = tfb[3] - tfb[1];
    var tf2 = tf.parentPage.textFrames.add(); //use parent page to add to the page you want
    tf2.previousTextFrame = tf;
    tf2.properties = {
        geometricBounds: [
        y1+boxIntro,
        x1,
        y1+boxIntro+boxTitle,
        x1+tfWidth
    ]};
    return tf2;
}