2

Let's say I have a function of the form.

Test[File_,text_]

What would be the most automated way and flexible way to generate an input cell of the form,

enter image description here

such that when you press the File Button and select "C:\\Users\\path\\testing.txt" it autofill with the textual arguments.

enter image description here

I'm quit interested to see what the Mathematica community thinks is the best technique for specifying which arguments should be File Buttons and if it might be possible to incorporate the File buttons with Template Completion. My current code is below, which isn't generalized to all function definitions, but I knew it would be best to attempt to create a partially working solution. I used the following question to help piece a together my partially working example.


MakeBoxes[replacementMarker[a_, tag_], StandardForm] ^:= 
  TagBox[MakeBoxes[a], tag];

 replaceMark[rule_, nb_: EvaluationNotebook[], which_: All, 
   cell_: EvaluationCell] := (
   SelectionMove[nb, which, cell]; 
   NotebookWrite[EvaluationNotebook[], 
    NotebookRead[EvaluationNotebook[]] /. rule]
   ) ;

CellPrint[
 Cell[
  BoxData[RowBox[{"Test", "[", 
       RowBox[{
          ToBoxes@replacementMarker[
         Button["Browse",
          replaceMark[
           file = SystemDialogInput["FileOpen"];
           TagBox[_, "replaceThis"] :> "\"" <>
             StringReplace[file, "\\" -> "\\\\"]
             <> "\""
           ]
          ], "replaceThis"]
       , ",", 
          TagBox[
            FrameBox["test"],
            "Placeholder"]}], "]"}]]
  , "Output"]
 ]
William
  • 7,595
  • 2
  • 22
  • 70
  • there is some code here http://mathematica.stackexchange.com/questions/5846/resources-for-beautiful-mathematica-stylesheets/14865#14865 in which you press a button and the button is replaced by text. You may be able to hack that around to do what you want for this task. – Mike Honeychurch Jul 18 '13 at 03:56
  • I copied your code into the question. It's not excessively long, and I think it is better to have it here than linked exclusively. – Mr.Wizard Aug 17 '13 at 16:54

1 Answers1

1

I'd start with something like this:

file = Button["File", file = SystemDialogInput["FileOpen"]];
CellPrint@ExpressionCell[test[Dynamic@file, Placeholder[text]], "Input"]
Michael Hale
  • 2,313
  • 18
  • 20