JS How to select the "first" text frame on a page

This is a discussion on JS How to select the "first" text frame on a page within the Adobe Indesign forums in Adobe Tools category; Hello, The following will select a text frame on the first page of a document. However textFrames.item(0) refers to the most recently created text frame, which is not very useful. Is there a property in the object model that allows for selecting the first text frame--based upon its position (highest and most left)--or must a clever script be written? app.activeDocument.pages.item(0).textFrames.item(0 ).select(); Thanks, Tom...

Go Back   Application Development Forum > Adobe Tools > Adobe Indesign

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-24-2008, 03:13 PM
Tom_Tomasko@adobeforums.com
Guest
 
Default JS How to select the "first" text frame on a page

Hello,

The following will select a text frame on the first page of a document. However textFrames.item(0) refers to the most recently created text frame, which is not very useful. Is there a property in the object model that allows for selecting the first text frame--based upon its position (highest and most left)--or must a clever script be written?

app.activeDocument.pages.item(0).textFrames.item(0 ).select();

Thanks,
Tom
Reply With Quote
  #2  
Old 08-24-2008, 03:34 PM
Peter Kahrel
Guest
 
Default Re: JS How to select the "first" text frame on a page

You need to compare the top of every text frame on that page and take the smallest value. Get the geometricBounds of each frame, which returns four-element arrays, then find the smallest value of the first element. E.g.

myTextframe.geometricBounds[0]




returns the top of myTextframe. What you need to decide is whether 'first' is topmost AND leftmost. A frame could be the first one on a page vertically and second or third, horizontally.

Peter
Reply With Quote
  #3  
Old 08-24-2008, 03:57 PM
Dave_Saunders@adobeforums.com
Guest
 
Default Re: JS How to select the "first" text frame on a page

It doesn't have to be that clever, but yes, a script must determine that. This function will do the job. Just pass the page reference to it. Note that it considers only those text frames that are loose on the page.

function getFirst(page) {
// returns top-left text frame on page
var myFrames = page.textFrames.everyItem().getElements();
myFrames.sort(topLeft);
return myFrames[0];
function topLeft (a,b) {
aBounds = a.geometricBounds;
bBounds = b.geometricBounds;
if (aBounds[0] > bBounds[0]) return 1;
if (bBounds[0] > aBounds[0]) return -1;
if (aBounds[1] > bBounds[1]) return 1;
if (bBounds[1] > aBounds[1]) return -1;
return 0;
}
}

Dave
Reply With Quote
  #4  
Old 08-24-2008, 03:59 PM
Dave_Saunders@adobeforums.com
Guest
 
Default Re: JS How to select the "first" text frame on a page

On thinking about it, the function would be better named getTopLeft. Then the comment is probably not needed.

Dave
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:42 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.