Re: Extracting the contents of tagged cells as plain text
- To: mathgroup at smc.vnet.net
- Subject: [mg86403] Re: Extracting the contents of tagged cells as plain text
- From: David Reiss <dbreiss at gmail.com>
- Date: Mon, 10 Mar 2008 02:06:03 -0500 (EST)
- References: <fqtqjd$d4j$1@smc.vnet.net>
The answer to this partly depends on what kind of cells the code is contained in. Your solution assumes that the cells in question are , for example, Text cells or Program cells. And in this case the Cell's cell expression has a string as its first argument as long as there is no formatting in the cell. I wouldn't consider your solution a hack-- it is a sensible starting point for doing what you want to do. Mathematica has all sorts of notebook manipulation functions that are specifically in place for this sort of thing. And "A WorkLife FrameWork " is built out of thousands of instances of this sort of thing.... Here is another example of how to do it nb= the notebook object of your notebook Module[{nbr}, NotebookFind[nb, "Java", All, CellTags]; nbr = NotebookRead[nb]; nbr /. Cell[x_, ___] :> x ] Hope that this helps... -David A WorkLife FrameWork E x t e n d i n g MATHEMATICA's Reach... http://scientificarts.com/worklife/ On Mar 8, 6:42=A0am, "J. McKenzie Alexander" <ja... at lse.ac.uk> wrote: > Hello, > > Suppose that I have a number of cells in a notebook containing program =A0= > code written in a language other than Mathematica. (Like Java or C.) =A0 > I'd like to be able to extract the contents of these cells as plain =A0 > text for processing in Mathematica (as strings), and then ultimately =A0 > export to a .java file for later compiling. > > Suppose that each cell has the tag "java". I know that I can select =A0 > the cells in the current notebook with that tag by calling > > =A0 =A0 =A0 =A0 NotebookLocate["java"] > > but, once I've done this, how do I get the contents of each cell? =A0The = =A0 > only thing I've found to work, so far, is: > > =A0 =A0 =A0 =A0 NotebookLocate["java"]; > =A0 =A0 =A0 =A0 (First[List @@ #] &) /@ NotebookRead[InputNotebook[]] > > but this looks like a bit of a hack, and I keep thinking that there =A0 > has to be a more elegant way. Is there? > > Sorry for such a basic question, but I'm just starting to learn how to =A0= > manipulate the contents of notebooks programmatically, and am =A0 > stumbling in the dark a bit. > > Many thanks, > > Jason > > -- > Dr J. McKenzie Alexander > Department of Philosophy, Logic and Scientific Method > London School of Economics and Political Science > Houghton Street, London WC2A 2AE > > Please access the attached hyperlink for an important electronic communica= tions disclaimer:http://www.lse.ac.uk/collections/secretariat/legal/disclaim= er.htm