Skip to main content
rombanks
Inspiring
October 25, 2015
Answered

ES: Opening the 5th book component from the end

  • October 25, 2015
  • 2 replies
  • 956 views

Hello fellows,

I wonder what is the best way to find the 5th book component from the end of the book (so the 1st book component to count is basically the last one). 

I can loop through all components and get a list of all of them. Then, I guess, I need to somehow put them into an array and find the 5th one from the bottom?

Any ideas will be appreciated!

This topic has been closed for replies.
Correct answer frameexpert

unshift is an array method that puts an item into the front of the array instead of the back. So if you had an array of numbers:

var array = [1, 2, 3, 4, 5];

and did this:

array.unshift (6);

you would have this:

[6, 1, 2, 3, 4, 5]

Normally, you use push to put items on an array:

array.push (6);

would give you:

[1, 2, 3, 4, 5, 6]

2 replies

frameexpert
Community Expert
Community Expert
October 26, 2015

This is how I would do it:

#target framemaker

var comps = [];

var book = app.ActiveBook;

var bookComp = book.FirstComponentInBook;

while (bookComp.ObjectValid ()) {

    // put the components into the array in reverse order.

    comps.unshift (bookComp);

    bookComp = bookComp.NextBookComponentInDFSOrder;

}

alert (comps[4].Name);

www.frameexpert.com
rombanks
rombanksAuthor
Inspiring
October 26, 2015

Hi Rick,

Thank you for your response and for the suggested code! What does the unshift method do? I couldn't find it in the scripting guide.


Thank you!

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
October 26, 2015

unshift is an array method that puts an item into the front of the array instead of the back. So if you had an array of numbers:

var array = [1, 2, 3, 4, 5];

and did this:

array.unshift (6);

you would have this:

[6, 1, 2, 3, 4, 5]

Normally, you use push to put items on an array:

array.push (6);

would give you:

[1, 2, 3, 4, 5, 6]

www.frameexpert.com
Participating Frequently
October 26, 2015

I cannot see any alternative to iterating through all the book components, storing the component ids as you go..

I'd use a buffer big enough to hold only 5 component ids. Store each successive component id you find in location (counter modulo 5) - so it's effectively a circular buffer. When you reach the last component in the book, the component you want will be the next entry in the circular buffer. This method saves memory, and handles a book of any size.

Whether or not you use the above method, ensure you gracefully handle a book with fewer than 5 components. In the above case, I'd intiialize the buffer to nulls, and check for a non-null value at the end.

Legend
October 26, 2015

Mike, Rom, all this sounds good so far, but why bother with buffers at all? I would just go to the end with FP_NextComponentInBook, then step backwards 5 times with FP_PrevComponentInBook. Given the speed of this type of operation, I don't think that you would see any performance issues even with a very large book.

Russ

rombanks
rombanksAuthor
Inspiring
October 26, 2015

Hi Russ,

I wonder why there is no LastComponentInBook. It would be easier to start counting from it.