Skip to main content
Inspiring
November 1, 2022
Answered

InDesign Scripting Help: Wrap Around Bounding Box Text Wrap

  • November 1, 2022
  • 4 replies
  • 675 views

Hi! I'm new to scripting and I'm looking for a script that I can run after a frame is selected to turn on "Wrap Around Bounding Box" Text Wrap just for that selected item. I what to add it to another script that I use and I can't figure it out. Thanks!

This topic has been closed for replies.
Correct answer jimDcleveland

Using the link that @Mike Bro supplied:
https://community.adobe.com/t5/indesign-discussions/help-with-wrapping-text-with-script-for-indesign/td-p/10203472 

I used part of the script by the great Peter Kahrel to come up with this:

if (app.selection.length && app.selection[0] instanceof Rectangle) {
app.selection[0].textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;
// To add in specific wrap dimensions, remove // from the line below
// app.selection[0].textWrapPreferences.textWrapOffset = [0, "0.0139 in", "0.125 in", "0.0139 in"];
}

 

4 replies

jimDclevelandAuthorCorrect answer
Inspiring
November 1, 2022

Using the link that @Mike Bro supplied:
https://community.adobe.com/t5/indesign-discussions/help-with-wrapping-text-with-script-for-indesign/td-p/10203472 

I used part of the script by the great Peter Kahrel to come up with this:

if (app.selection.length && app.selection[0] instanceof Rectangle) {
app.selection[0].textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;
// To add in specific wrap dimensions, remove // from the line below
// app.selection[0].textWrapPreferences.textWrapOffset = [0, "0.0139 in", "0.125 in", "0.0139 in"];
}

 

Loic.Aigon
Legend
November 1, 2022

Can't tell why my code wasn't working without any error message but whatever, as long as you get the expected result.

Willi Adelberger
Community Expert
Community Expert
November 1, 2022

Why do you want to solve everythig with scripts? it is easier to work with styles, here object styles.

Loic.Aigon
Legend
November 1, 2022

I second you on that.

Loic.Aigon
Legend
November 1, 2022

You could go with this one but I am wondering if an object style wouldn't be more appropriate and more easily managed.

//Let's make script persistent
#targetengine "onSelectingFrame"

//Our main routine
//Will add a afterSelectionChanged Handler
function main(){

    //------ VARS -------//
    var 
    //The event name
    evName = "onSelectionChangeListener",
    //The event listener reference
    ev = app.eventListeners.itemByName(evName),
    //The event handler
    onSelectionChangeHandler = function(event){
        
        var sel, obj, st;

        
        //If selection is inadequate, return
        if(event.eventType!=="afterSelectionChanged") return;
        if(app.documents.length==0) return;
        if(app.selection.length!=1) return;

        //When selection is unique, everything is ok
        sel = app.selection[0];

        //If sel is text, we could choose to either ignore it or to get the containers like here.
        //No good or bad choice, just a decision to take.
        if(sel.hasOwnProperty("baseline")){
            st = sel.parentStory;
            applyWrap(st.textContainers);
        }
        //Or it's an unique selection and not a "character" like object
        else{
            applyWrap([sel]);
        }
        
    };

    //Lazy removal of the listener for debugguing purpose
    if(ev.isValid) ev.remove();

    //adding the listener
    ev = app.eventListeners.add("afterSelectionChanged", onSelectionChangeHandler);
    ev.name = evName;
}

//Apply wrapping options to the array of supplied items
function applyWrap(aItems){
    var n = 0, nItem;
    if(!aItems || !(aItems instanceof Array) || aItems.length==0) return;
    n = aItems.length;
    while(n--){
        nItem = aItems[n];
        if(nItem.hasOwnProperty("textWrapPreferences")){
            nItem.textWrapPreferences.properties = {
                textWrapMode:TextWrapModes.BOUNDING_BOX_TEXT_WRAP
            }
        }
    }
}

//Let's run the script and add event listeners if needed.
main();
Inspiring
November 1, 2022

Thanks, @Loic.Aigon

Unfortunately, I can't seem to get the script to work correctly. Can it be simplified not to use event listeners? The correct selected frame that I need the wrap applied to is already selected as part of my other script. Any help you can give me is greatly appreciated!!

Legend
November 1, 2022