I'm trying to learn low-level notebook programming and I have a question.

Is there faster way to select the longest cell in a notebook than this:

NB = CreateDocument[
      ExpressionCell[#, "Input"] & /@ {"cell 1", Column@{"cell", "2"}, "cell 3"}]
pos = Position[#, Max @@ #][[ 1, 1]] &@
              ReplaceAll[CellSize, (# // AbsoluteOptions) & /@ Cells[NB]][[ All, 2]]

SelectionMove[Cells[NB][[ pos]], All, Cell]
  • 2
    $\begingroup$ Longest means the String Content? or if cell options are the same, ByteCount maybe useful after NotebookRead CellObject? $\endgroup$ – HyperGroups May 28 '13 at 9:07
  • 1
    $\begingroup$ @HyperGroups Well, it does not matter now if it is going about height of cell or strign length. As You see I've used AbsoluteOptions and ReplaceAll, what I want is to make the code simpler. $\endgroup$ – Kuba May 28 '13 at 9:11

It seems that what you want has been implemented by AbsoluteOptions. You can ask the value of a specific option using AbsoluteOptions. Your code then looks like this

pos = Position[#, Max @ #][[1, 
     1]] &@(AbsoluteOptions[#, CellSize][[1, 2, 2]] & /@ Cells[NB])

-> 2

I have also taken the liberty of selecting the second elements of the CellSize option values while doing the map, rather than selecting them afterwards. I've also written Max@# instead of Max@@# as the latter must be slower.

  • $\begingroup$ @Kuba, ah, I didn't realize it was you again :). I'm glad to see you are using Cells in this nice way :). You're welcome. $\endgroup$ – Jacob Akkerboom May 28 '13 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.