1
$\begingroup$

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"]
 ]
$\endgroup$
  • $\begingroup$ there is some code here mathematica.stackexchange.com/questions/5846/… 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. $\endgroup$ – Mike Honeychurch Jul 18 '13 at 3:56
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Aug 17 '13 at 16:54
1
$\begingroup$

I'd start with something like this:

file = Button["File", file = SystemDialogInput["FileOpen"]];
CellPrint@ExpressionCell[test[Dynamic@file, Placeholder[text]], "Input"]
$\endgroup$

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.