Re: Dialog for selecting a file to be read-- A solution (16K)
- To: mathgroup at smc.vnet.net
- Subject: [mg18082] Re: Dialog for selecting a file to be read-- A solution (16K)
- From: jns1 <jns1 at madmax.hrl.HAC.COM>
- Date: Tue, 15 Jun 1999 01:43:37 -0400
- Delivery-date: Tue Jun 15 05:46:11 1999
- Organization: LRH
- Sender: owner-wri-mathgroup at wolfram.com
There was a thread a month or so ago about how to select files graphically from a directory for plotting or whatever. If only a single file is needed, the Input/Get File Path... menu selection was suggested. I needed a method for selecting multiple files from a given subdirectory, so I learned about buttons and notebook manipulation, etc., over the last few days for this purpose. I must say it was not easy. The examples in the Big Book mostly pertained to applying expression manipulation to notebook cells. Anyway, I did manage to come up with something that works. I have already mucked it up to the point of incomprehensibility for my particular application, but I have also make a simplified version that demonstrates the basic idea for a simple case for those interested. It is only 16K, so here is the notebook below. I used Mathematica 4 due to the new features of the Partition function. I am sure there are other ways to do the same thing. ============================dialogSimple.nb====================================== (*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info at wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 13174, 389]*) (*NotebookOutlinePosition[ 14041, 417]*) (* CellTagsIndexPosition[ 13997, 413]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Simple File Dialog", "Subtitle"], Cell[BoxData[ \(\(\(\(Off[General::"\<spell1\>"]\) \)\(;\)\(\(Off[ General::"\<spell\>"]\) \)\(;\)\(\ \)\( (*\ Eliminates\ spelling\ error\ warnings\ *) \)\)\)], "Input", FontWeight->"Plain"], Cell[TextData[{ "This is a simple way to select one or more files from a given \ subdirectory. I use it for ", StyleBox["MultipleListPlot ", FontWeight->"Bold"], "'ing data from selected files. The files are displayed in a palette, one \ file per button. Click on a button to choose the file, which then appends it \ to a ", StyleBox["List", FontWeight->"Bold"], ". This does not allow you to move up and down the directory tree, so that \ remains to be done. You can either type in the subdirectory or subdirectories \ where your data resides explicitly, or you can use \"Get File Path...\" from \ the \"Input\" menu to choose a typical file to identify the subdirectory. I \ will do the latter here." }], "Text"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " version 4 is required due to the use of the new extensions to the ", StyleBox["Partition", FontWeight->"Bold"], " function below." }], "Text"], Cell[TextData[{ "First, a typical (but not necessarily desired) file is chosen using \ Input/Get File Path.... in the next line. This is just a convenient way to \ specify the subdirectory of interest. It is easier to demonstrate all this \ using a specific case. Really, you should make everything a function, \ probably of ", StyleBox["typicalFile ", FontWeight->"Bold"], "or", StyleBox[" dirName", FontWeight->"Bold"], ", the subdirectory itself.", " Then you can invoke this using different ", StyleBox["typicalFile", FontWeight->"Bold"], "'s", " or ", StyleBox["dirName", FontWeight->"Bold"], "'s as desired for different subdirectories." }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(typicalFile = "\<Macintosh HD:Mathematica \ Documents:RITData:SB1091:RB109115.IVM\>"\)], "Input"], Cell[BoxData[ \("Macintosh HD:Mathematica Documents:RITData:SB1091:RB109115.IVM"\)], \ "Output"] }, Open ]], Cell["This gets the directory tree of the typical file. ", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(dirName = DirectoryName[typicalFile]\)], "Input"], Cell[BoxData[ \("Macintosh HD:Mathematica Documents:RITData:SB1091:"\)], "Output"] }, Open ]], Cell[TextData[{ "All the files names in the same subdirectory as ", StyleBox["typicalFile", FontWeight->"Bold"], ", with full directory path included." }], "Text"], Cell[BoxData[ \(\(fNames = FileNames["\<*\>", dirName];\)\)], "Input"], Cell["List of the file names without the directory path:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(shortNames = StringReplace[fNames, dirName \[Rule] "\<\>"]\)], "Input"], Cell[BoxData[ \({"RB109110.IVM", "RB109111.IVM", "RB109112.IVM", "RB109113.IVM", "RB109114.IVM", "RB109115.IVM", "RB109116.IVM", "RB109117.IVM", "RB109118.IVM", "RB109119.IVM", "RB109120.IVM", "RB109121.IVM", "RB109122.IVM", "RB109123.IVM", "RB109124.IVM", "RB109125.IVM", "RB109126.IVM", "RB109127.IVM", "SB109110.IVM", "SB109111.IVM", "SB109112.IVM", "SB109113.IVM", "SB109114.IVM", "SB109115.IVM", "SB109116.IVM", "SB109117.IVM", "SB109118.IVM", "SB109119.IVM", "SB109120.IVM", "SB109121.IVM", "SB109122.IVM", "SB109123.IVM", "SB109124.IVM", "SB109125.IVM", "SB109126.IVM", "SB109127.IVM", "SB109128.IVM", "SB109129.IVM", "SB109130.IVM", "SB109131.IVM", "SB109132.IVM", "SB109133.IVM", "SB109134.IVM", "SB109135.IVM", "SB109136.IVM", "SB109137.IVM", "SB109138.IVM", "SB109139.IVM", "SB109140.IVM", "SB109141.IVM"}\)], "Output"] }, Open ]], Cell[TextData[{ StyleBox["doButton", FontWeight->"Bold"], " makes a palette button for a file name using ", StyleBox["ButtonBox", FontWeight->"Bold"], ". I didn't know anything about this stuff before writing this either. It \ is not very well described in the Big Book. The examples their are not too \ relevant for this purpose. But I managed to figure it out. \nThere will be \ one button per file in the eventual palette. ", StyleBox["f", FontWeight->"Bold"], " in the next line will represent each file entry in the array ", StyleBox["shortNames", FontWeight->"Bold"], ". ", StyleBox["ButtonEvaluator->\"Local\"", FontWeight->"Bold"], " means the Kernel evaluates this, not the mini-kernel built into the front \ end. Clicking a button will ", StyleBox["Append", FontWeight->"Bold"], " a file name to the list ", StyleBox["flist", FontWeight->"Bold"], ", so that's what the ", StyleBox["ButtonFunction", FontWeight->"Bold"], " does in this case. ", StyleBox["ButtonData", FontWeight->"Bold"], " means use the string ", StyleBox["f", FontWeight->"Bold"], " for the preceding #1 argument." }], "Text"], Cell[BoxData[ RowBox[{\(doButton[f_String]\), ":=", RowBox[{"ButtonBox", StyleBox["[", FontColor->RGBColor[0, 0, 1]], RowBox[{"f", ",", \(ButtonEvaluator \[Rule] "\<Local\>"\), ",", RowBox[{"ButtonFunction", "\[RuleDelayed]", RowBox[{ StyleBox["(", FontColor->RGBColor[1, 0, 0]], RowBox[{ RowBox[{ StyleBox["{", FontColor->RGBColor[0, 1, 0]], \(fList = \ Append[fList, #1]\), StyleBox["}", FontColor->RGBColor[0, 1, 0]]}], "&"}], StyleBox[")", FontColor->RGBColor[1, 0, 0]]}]}], ",", \(ButtonData \[Rule] f\)}], StyleBox["]", FontColor->RGBColor[0, 0, 1]]}]}]], "Input"], Cell[TextData[{ "Additional \"Done\" button to click when there are no more files. ", StyleBox["Union", FontWeight->"Bold"], " eliminates accidental duplicates and orders the files. The full directory \ path is restored to the file name by ", StyleBox["fullL", FontWeight->"Bold"], "." }], "Text"], Cell[BoxData[ \(fullL[f_] := dirName <> f\)], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"doneButton", "=", "\[IndentingNewLine]", RowBox[{"ButtonBox", "[", RowBox[{ "\"\<Done\>\"", ",", \(ButtonEvaluator \[Rule] "\<Local\>"\), ",", \(Background \[Rule] RGBColor[ .8, .4, .4]\), ",", "\[IndentingNewLine]", RowBox[{"ButtonFunction", "\[RuleDelayed]", RowBox[{ StyleBox["(", FontColor->RGBColor[1, 0, 0]], RowBox[{ RowBox[{ StyleBox["{", FontColor->RGBColor[0, 1, 0]], \(fList = fullL /@ Union[fList]\), StyleBox["}", FontColor->RGBColor[0, 1, 0]]}], "&"}], StyleBox[")", FontColor->RGBColor[1, 0, 0]]}]}]}], "]"}]}], ";"}]], "Input"], Cell[TextData[{ "This creates an ", StyleBox["Array", FontWeight->"Bold"], " which mimics the form of the eventual palette. Read the following from \ bottom up. It takes the shortNames list and partitions it into an array with \ rows of 5 in length, which makes a nice palette. Then it maps ", StyleBox["doButton", FontWeight->"Bold"], " on to the array elements to make a ", StyleBox["ButtonBox", FontWeight->"Bold"], " for each file name. Then, the ", StyleBox["doneButton", FontWeight->"Bold"], " is appended. \nThe strange form of the Partition function is needed if \ the number of files is not a multiple of 5, to avoid losing any at the end." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"bFiles", "=", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{\(Append[#, {doneButton}]\), StyleBox["&", FontColor->RGBColor[0, 0, 1]]}], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox["@", FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", \(Map[doButton, #, {2}]\)}], StyleBox["&", FontColor->RGBColor[0, 0, 1]]}], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox["@", FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", " ", \(Partition[#, 5, 5, {1, 1}, {}]\)}], StyleBox["&", FontColor->RGBColor[0, 0, 1]]}], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox["@", FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", " ", "shortNames"}]}], ";"}]], "Input"], Cell["\<\ The palette of buttons will be a cell in a separate, automatically \ created notebook. The following is the cell. I don't really understand this \ GridBox and BoxData stuff; I just copied an example in the Big Book.\ \>", \ "Text"], Cell[BoxData[ RowBox[{ RowBox[{"dirCell", "=", RowBox[{"{", "\[IndentingNewLine]", RowBox[{"Cell", StyleBox["[", FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", RowBox[{ RowBox[{"BoxData", StyleBox["[", FontColor->RGBColor[1, 0, 0]], \(GridBox[\[IndentingNewLine]bFiles, ColumnSpacings \[Rule] 0, RowSpacings \[Rule] 0]\), StyleBox["]", FontColor->RGBColor[1, 0, 0]]}], ",", "\[IndentingNewLine]", "\"\<Input\>\"", ",", \(Active \[Rule] True\)}], StyleBox["]", FontColor->RGBColor[0, 0, 1]]}], "}"}]}], ";"}]], "Input"], Cell["This is the notebook of buttons that will be created.", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"buttonNB", "=", "\[IndentingNewLine]", RowBox[{"Notebook", StyleBox["[", FontColor->RGBColor[0, 1, 0]], \(dirCell, WindowTitle \[Rule] "\<Data Files\>"\), StyleBox["]", FontColor->RGBColor[0, 1, 0]]}]}], ";"}]], "Input"], Cell[TextData[{ "This initializes ", StyleBox["fList", FontWeight->"Bold"], " and pops up the notebook of buttons. When you click ", StyleBox["Done", FontWeight->"Bold"], ", the file names you selected will be in the list ", StyleBox["fList", FontWeight->"Bold"], " to do with as you wish." }], "Text"], Cell[BoxData[ \(fList = {}; NotebookPut[buttonNB];\)], "Input"], Cell["\<\ There might be a harmless error message output because of the \ different length of the rows of buttons.\ \>", "Text"], Cell[TextData[{ "I clicked on 3 file name buttons and then ", StyleBox["Done", FontWeight->"Bold"], " to produce the following." }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(fList\)], "Input"], Cell[BoxData[ \({"Macintosh HD:Mathematica Documents:RITData:SB1091:RB109127.IVM", "Macintosh HD:Mathematica Documents:RITData:SB1091:SB109118.IVM", "Macintosh HD:Mathematica Documents:RITData:SB1091:SB109135.IVM"}\)], \ "Output"] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for Macintosh", ScreenRectangle->{{0, 1024}, {0, 748}}, WindowSize->{560, 638}, WindowMargins->{{189, Automatic}, {Automatic, 2}}, ShowCellLabel->False, MacintoshSystemPageSetup->"\<\ 00<0001804P000000]P2:?oQon82n at 960dL5:0?l0080001804P000000]P2:001 0000I00000400`<300000BL?00400 at 0000000000000006P801T1T00000000000 00000000000000000000000000000000\>" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 38, 0, 61, "Subtitle"], Cell[1780, 53, 212, 4, 41, "Input"], Cell[1995, 59, 732, 15, 114, "Text"], Cell[2730, 76, 222, 7, 32, "Text"], Cell[2955, 85, 693, 20, 98, "Text"], Cell[CellGroupData[{ Cell[3673, 109, 117, 2, 59, "Input"], Cell[3793, 113, 100, 2, 26, "Output"] }, Open ]], Cell[3908, 118, 66, 0, 30, "Text"], Cell[CellGroupData[{ Cell[3999, 122, 69, 1, 27, "Input"], Cell[4071, 125, 86, 1, 26, "Output"] }, Open ]], Cell[4172, 129, 173, 5, 32, "Text"], Cell[4348, 136, 74, 1, 27, "Input"], Cell[4425, 139, 66, 0, 30, "Text"], Cell[CellGroupData[{ Cell[4516, 143, 91, 1, 27, "Input"], Cell[4610, 146, 918, 13, 206, "Output"] }, Open ]], Cell[5543, 162, 1184, 35, 136, "Text"], Cell[6730, 199, 853, 21, 75, "Input"], Cell[7586, 222, 315, 9, 50, "Text"], Cell[7904, 233, 58, 1, 27, "Input"], Cell[7965, 236, 890, 21, 75, "Input"], Cell[8858, 259, 695, 17, 116, "Text"], Cell[9556, 278, 1167, 29, 91, "Input"], Cell[10726, 309, 241, 5, 46, "Text"], Cell[10970, 316, 774, 18, 91, "Input"], Cell[11747, 336, 69, 0, 30, "Text"], Cell[11819, 338, 328, 8, 43, "Input"], Cell[12150, 348, 328, 11, 50, "Text"], Cell[12481, 361, 67, 1, 27, "Input"], Cell[12551, 364, 128, 3, 30, "Text"], Cell[12682, 369, 150, 5, 32, "Text"], Cell[CellGroupData[{ Cell[12857, 378, 38, 1, 27, "Input"], Cell[12898, 381, 248, 4, 56, "Output"] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)