Skip to main content
Inspiring
July 22, 2020
Answered

Converting text to outline and then select it with javascript

  • July 22, 2020
  • 2 replies
  • 1253 views

So I was working on doing something in InDesign, and finally finished. The script works perfect, until its run on an Mac...

 

I am converting text to an outline, selecting everything on the page, and croping the page to fit the existing items on the page.

 

I believe the issue is that when run on a mac, the text converted to outline is no longer being seen as an item?


So, with that being said!!! Is there a way to convert a textFrame to an Outline and immediately have that conversion selected?

I will post my previous code in a reply in case that helps clarify.

Thanks in advance!

This topic has been closed for replies.
Correct answer adamr5798954dfd

So I've got it...

 

For some odd reason it was not selecting all on a mac. And on one layer the item I needed was at [0], on the other it was [1].

 

So since I know the the outlined text is always the lowest item, I did this.

            //Clear selection
            app.activeDocument.select(NothingEnum.NOTHING);
            //Select text for bounderies
            if(myDocument.layers[0].name == "1 LINE"){
                currentPage.pageItems.item(0).select();
            }
            else if(myDocument.layers[0].name == "2 LINE"){
                currentPage.pageItems.item(1).select();
            }
            else{
                alert("Document improperly formatted.");
                exit();
            }

 

2 replies

adamr5798954dfdAuthorCorrect answer
Inspiring
July 23, 2020

So I've got it...

 

For some odd reason it was not selecting all on a mac. And on one layer the item I needed was at [0], on the other it was [1].

 

So since I know the the outlined text is always the lowest item, I did this.

            //Clear selection
            app.activeDocument.select(NothingEnum.NOTHING);
            //Select text for bounderies
            if(myDocument.layers[0].name == "1 LINE"){
                currentPage.pageItems.item(0).select();
            }
            else if(myDocument.layers[0].name == "2 LINE"){
                currentPage.pageItems.item(1).select();
            }
            else{
                alert("Document improperly formatted.");
                exit();
            }

 

Inspiring
July 22, 2020
    var myDocument = app.activeDocument;

    //Set current page
    var currentPage = myDocument.pages[0];
        
    //Create outlines of text
    currentPage.textFrames.everyItem().createOutlines();

    //Select items on current page
    for (var j=0; j<currentPage.pageItems.length; j++)
    {
         currentPage.pageItems.everyItem().select();
    }
    //Get outer bounds of selection for resize
    var gb = app.activeDocument.selection[0].geometricBounds;
    //Resize
    currentPage.resize(CoordinateSpaces.INNER_COORDINATES,
       AnchorPoint.TOP_LEFT_ANCHOR,
       ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,
       [2000, gb[2]] );
brian_p_dts
Community Expert
Community Expert
July 22, 2020

Not sure it would necessarily be a problem with Mac. Something different about the document perhaps (a different measurement setting)? If so, using [2000, gb[2]] may be causing an out of bounds exception. 

 

Best way to check is to wrap the whole codeblock in a try/catch statement, and alert the error on the catch: 

try {    
    var myDocument = app.activeDocument;

    //Set current page
    var currentPage = myDocument.pages[0];
        
    //Create outlines of text
    currentPage.textFrames.everyItem().createOutlines();

    //Select items on current page
    for (var j=0; j<currentPage.pageItems.length; j++)
    {
         currentPage.pageItems.everyItem().select();
    }
    //Get outer bounds of selection for resize
    var gb = app.activeDocument.selection[0].geometricBounds;
    //Resize
    currentPage.resize(CoordinateSpaces.INNER_COORDINATES,
       AnchorPoint.TOP_LEFT_ANCHOR,
       ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,
       [2000, gb[2]] );
} catch(e) {
   alert(e + " on line " + e.line);
}

 

Other potential error in the code I see is:

 

for (var j=0; j<currentPage.pageItems.length; j++)
{
   currentPage.pageItems.everyItem().select();
}

 

You don't need the for loop; everyItem().select() should select every item in one fell swoop. 

Inspiring
July 22, 2020

Thanks for the crazy quick reply!

So it's the same exact document from one machine to the next.  It's weird!!

 

Ok, I made the changes and will test tomorrow when I've got a mac in front of me.  I'll update, but in the end the only real change being made is the looped select. Honestly, I had that doing something different previously and never removed the loop.  Either way, not sure that could effect it directly but then again it shouldn't be OS specific.