Skip to main content
Participating Frequently
July 13, 2014
Question

Check the first object within text frame if another text frame or normal text with JaveScript

  • July 13, 2014
  • 1 reply
  • 343 views

Hi all,

I need to write script that will check the first object within my selected text frame and tell me if that object is textFrame Object or text Object.

For Example the next image shows that the first object in the main text frame is another text frame contains Table, in that case the script should return TRUE

in the next case the main text frame contains normal text in beginning of the frame, in that case the script should return FALSE

So how can i check for this cases,

Any help i will be grateful,

Thanks

This topic has been closed for replies.

1 reply

Jump_Over
Legend
July 13, 2014

Hi,

Are you considering a table placed in your textFrame with no container (your textFrame is a table.parent)?

Are you considering a spaces or empty lines in the beginnig of your textFrame?

You could detect objects at the beginning of a frame by checking

mTextFrame.texts[0].characters[0].pageItems.length    //    0 means false

However considering my two question a function could look like:

function objectSearcher (depth) {

  var mFrame = app.selection[0];

  if (!mFrame ||

      mFrame.constructor.name != "TextFrame" )

      return false;

// if depth no specified - whole textFrame is a target

  if (isNaN (depth) ) depth = -1;

  var

      mText = mFrame.texts[0].characters.itemByRange(0,depth).texts[0],

      mItems = mText.pageItems,

      mTables = mText.tables,     //     in case of table is placed with no additional container

      itemsCount = mItems.length,

      tablesCount = mTables.length;

  if (itemsCount || tablesCount) return true;

  else return false;

}

mResult = objectSearcher ();

depth = 0 to check 1st character only

depth = null to check entire textFrame

Jarek

Kai Rübsamen
Participating Frequently
July 13, 2014

Argh, Jarek was quicker than me ;-)

I’m not sure, if this is a good solution, but for me this one works too:

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

if ( !(app.selection.length == 1 && app.selection[0].constructor.name == "TextFrame" ) ) {

    alert( "Check your selection!" ); exit();

}

else {

    var curSel = app.selection[0];

}

var firstChar = curSel.characters[0];

if ( firstChar.textFrames.length == 1 || firstChar.tables.length == 1 ) {

        alert( "true" );

}

else {

    alert( "false" );

}

Participating Frequently
July 14, 2014

Hi Kai,

Thank you for your help, but the main problem here is the text frames containers of the tables are not inline objects, so `characters[0]` will not detect the container text frame it will detect the first character in the text "T" letter.


Thanks,