Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In Mathematica 8.0 and older, there are two ways to select cells based on cell tags.

NotebookLocate["CellTag"];
NotebookGet[EvaluationNotebook[]]

and

NotebookFind[EvaluationNotebook[], "CellTag", All, CellTags, AutoScroll -> False];
NotebookGet[EvaluationNotebook[]]

When executing either of these the user selection becomes modified. By manually extracting the cells from the notebook, the user selection is not modified. Therefore, you could run NotebookGet in some type of a loop or in another user interface without messing with user's interactions.

Now NotebookGet[EvaluationNotebook[]] holds the Notebook data. So how can I extract cells as if I had run one of the commands above using NotebookGet[EvaluationNotebook[]]?

EDIT: Here is a working solution http://pastebin.com/mvFPqvVY

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Pre-V9 solution

My answer here turns out to be applicable to this question, too. There I had written this function that goes traverses a notebook's expression tree down to the level of what I will call the "notebook's cells." These are the ones displayed with a cell bracket (so it does not find inline cells).

cells[nb_NotebookObject] := cells[nb, _];
cells[nb_NotebookObject, pat_] := Flatten @ cells[First @ NotebookGet[nb], pat];
cells[cellList_List, pat_] := cells[#, pat] & /@ cellList;
cells[Cell[CellGroupData[group_List, ___]], pat_] := cells[#, pat] & /@ group;
cells[c_Cell, pat_] := If[MatchQ[c, pat], c, {}];
cells[__] := {};

It was the OP, Liam, actually, who wrapped this function in an interface to search by tag, and kindly invited me to include it in this answer:

CellsByTag[notebook_, tag_] := Module[{cells},
  cells[nb_NotebookObject] := cells[nb, _];
  cells[nb_NotebookObject, pat_] := Flatten@cells[First@NotebookGet[nb], pat];
  cells[cellList_List, pat_] := cells[#, pat] & /@ cellList;
  cells[Cell[CellGroupData[group_List, ___]], pat_] := cells[#, pat] & /@ group;
  cells[c_Cell, pat_] := If[MatchQ[c, pat], c, {}];
  cells[__] := {};
  cells[notebook, _?(MemberQ[Flatten[{CellTags} /. Options[#]], tag] &)]]
share|improve this answer

Is this what you are looking for?

V8

Cases[
      NotebookGet[EvaluationNotebook[]],
      Cell[__, CellTags -> "tag", ___]
      , \[Infinity]]

V9

NotebookRead @ First @ Cells[EvaluationNotebook[], CellTags -> "CellTag"]
share|improve this answer
    
@Liam does Cases works for you? –  Kuba Aug 8 '13 at 7:28
    
Not all Cells will be in level 2 because of CellGroupings. My next reaction was to have Cases select with Infinity. Try running the following in an new notebook. pastebin.com/2vaixT2e –  Liam William Aug 8 '13 at 16:46
    
@Liam You are right, this came to my mind but I forgot to check. –  Kuba Aug 8 '13 at 16:49
    
I thought you did ;), but you didn't my use cases so it might have worked. Further investigation if you run the following code pastebin.com/BgDGa3eZ then you try running NotebookLocate["MyTag"];NotebookRead[EvaluationNotebook[]] you get 5 elements with Cases vs 2 with NotebookRead –  Liam William Aug 8 '13 at 16:52
    
@Liam Maybe this pattern is enough Cell[__, CellTags -> "MyTag"]? Then I have 2 matching cells, this one with Cases and the one you are looking for. It seems that CellTag is at the end but I'm not sure. –  Kuba Aug 8 '13 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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