3
$\begingroup$

Is there a way to effectively get drag and drop so you can drag files onto nbs in Windows?

For example similar to the way .bat scripts work.

Ideally I would like the script to automatically execute on run.

$\endgroup$
  • $\begingroup$ Notebooks are not meant to be treated as programs. You might want to look into .m files to be used as scripts. This also applies to your question about auto-evaluating a notebook on open, which is not such a good idea. Notebooks are documents, not programs, and should not auto-evaluate. It's a serious security risk. You can write scripts instead, which are programs, and are opened/run with a different expectation. $\endgroup$ – Szabolcs Mar 29 '15 at 14:47
  • 1
    $\begingroup$ I think the lack of votes here is because nobody clearly understands what the code does. Perhaps you could use LICEcap to make a recording of how this drag and drop looks/works. I could see myself voting for a drag and drop functionality but I can't vote on just the words "drag and drop" when I don't know what they mean in this context. $\endgroup$ – C. E. Apr 9 '15 at 20:42
  • $\begingroup$ @Picket good point. I will post more information. If you read the @echo off post hopefully you will notice it is pretty simple. Simple save the file as .bat and drag and drop files onto it. $\endgroup$ – William Apr 15 '15 at 2:59
2
$\begingroup$

The following works if you are simply trying to run data on the command line.

@echo off
"C:\Program Files\Wolfram Research\Mathematica\10.0\Math" -noprompt -run "ToExpression@StringJoin@Riffle[Drop[StringSplit[UsingFrontEnd@Import[$CommandLine[[5]], \"Text\"],\"\n\",All], 4], \"\n\"];Quit[];" %0 %*
exit /b

Print[$CommandLine[[6;;]]];
Print[23];
$\endgroup$
0
$\begingroup$

Assuming you have node.js installed this works well.

print.bat http://pastebin.com/raw.php?i=HSr2Dm9D

edit.bat

@echo off
"C:\Program Files\nodejs\node.exe" -p "require('fs').writeFileSync('./args.txt',JSON.stringify(process.argv));require('child_process').exec('C:\\Users\\a\\Desktop\\nb.nb',function (error, stdout, stderr) {if(error != null) {console.log('exec error: ' + error);}});" %0 %*
exit
(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 10.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       158,          7]
NotebookDataLength[      4879,        124]
NotebookOptionsPosition[      3950,         96]
NotebookOutlinePosition[      4981,        126]
CellTagsIndexPosition[      4938,        123]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
 RowBox[{"Module", "[", 
  RowBox[{
   RowBox[{"{", 
    RowBox[{"str", ",", "nb"}], "}"}], ",", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"If", "[", 
     RowBox[{
      RowBox[{
       RowBox[{"Length", "@", "$CommandLines"}], "<", "3"}], ",", 
          RowBox[{"Throw", "[", "1", "]"}]}], "]"}], ";", "\[IndentingNewLine]", 
        RowBox[{"str", "=", 
         RowBox[{"OpenWrite", "[", 
          RowBox[{"$TemporaryDirectory", "<>", "\"\<\\\\\>\"", "<>", 
           RowBox[{"Last", "@", 
            RowBox[{"StringSplit", "[", 
             RowBox[{
              RowBox[{"$CommandLines", "[", 
               RowBox[{"[", "3", "]"}], "]"}], ",", "\"\<\\\\\>\""}], "]"}]}], 
           "<>", "\"\<.nb\>\""}], "]"}]}], ";", "\[IndentingNewLine]", 
        RowBox[{"WriteString", "[", 
         RowBox[{"str", ",", "\[IndentingNewLine]", 
          RowBox[{"StringReplace", "[", 
           RowBox[{
            RowBox[{"Import", "[", 
             RowBox[{
              RowBox[{"$CommandLines", "[", 
               RowBox[{"[", "3", "]"}], "]"}], ",", "\"\<Text\>\""}], "]"}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{
            "\"\<@echo off\n\\\"C:\\\\Program Files\\\\nodejs\\\\node.exe\\\" -p \
    \\\"require('fs').writeFileSync('./args.txt',JSON.stringify(process.argv));\
    require('child_process').exec('C:\\\\\\\\Users\\\\\\\\a\\\\\\\\Desktop\\\\\\\\\
    nb.nb',function (error, stdout, stderr) {if(error != null) {console.log('exec \
    error: ' + error);}});\\\" %0 %*\nexit\n\>\"", "\[Rule]", "\"\<\>\""}]}], 
           "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
        RowBox[{"NotebookClose", "[", 
         RowBox[{"EvaluationNotebook", "[", "]"}], "]"}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"nb", " ", "=", 
         RowBox[{"NotebookOpen", "[", 
          RowBox[{"Close", "[", "str", "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
        RowBox[{"SetOptions", "[", 
         RowBox[{"nb", ",", 
          RowBox[{"NotebookEventActions", "\[Rule]", 
           RowBox[{"{", 
            RowBox[{
             RowBox[{
              RowBox[{"{", 
               RowBox[{"\"\<MenuCommand\>\"", ",", "\"\<Save\>\""}], "}"}], 
              "\[RuleDelayed]", 
              RowBox[{"(", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"NotebookSave", "[", 
                 RowBox[{"InputNotebook", "[", "]"}], "]"}], ";", 
                "\[IndentingNewLine]", 
                RowBox[{"Export", "[", 
                 RowBox[{
                  RowBox[{"$CommandLines", "[", 
                   RowBox[{"[", "3", "]"}], "]"}], ",", "\[IndentingNewLine]", 
                  RowBox[{
                  "\"\<@echo off\n\\\"C:\\\\Program Files\\\\nodejs\\\\node.exe\\\
    \" -p \\\"require('fs').writeFileSync('./args.txt',JSON.stringify(process.\
    argv));require('child_process').exec('C:\\\\\\\\Users\\\\\\\\a\\\\\\\\Desktop\
    \\\\\\\\nb.nb',function (error, stdout, stderr) {if(error != null) \
    {console.log('exec error: ' + error);}});\\\" %0 %*\nexit\n\>\"", " ", "<>", 
                   "\[IndentingNewLine]", 
                   RowBox[{"Import", "[", 
                    RowBox[{
                     RowBox[{"NotebookFileName", "[", 
                      RowBox[{"InputNotebook", "[", "]"}], "]"}], ",", 
                     "\"\<Text\>\""}], "]"}]}], ",", "\"\<Text\>\""}], "]"}], 
                ";"}], "\[IndentingNewLine]", ")"}]}], ",", 
             RowBox[{"PassEventsDown", "\[Rule]", "False"}]}], "}"}]}]}], "]"}], 
        ";"}]}], "\[IndentingNewLine]", "]"}]], "Input"]
    },
    InitializationCellEvaluation->True,
    InitializationCellWarning->False,
    NotebookEventActions->{{"MenuCommand", "Save"} :> (NotebookSave[
         InputNotebook[]]; Export[
         Part[$CellContext`$CommandLines, 3], 
     StringJoin[
     "@echo off\n\"C:\\Program Files\\nodejs\\node.exe\" -p \
\"require('fs').writeFileSync('./args.txt',JSON.stringify(process.argv));\
require('child_process').exec('C:\\\\Users\\\\a\\\\Desktop\\\\nb.nb',function \
(error, stdout, stderr) {if(error != null) {console.log('exec error: ' + \
error);}});\" %0 %*\nexit\n", 
      Import[
       NotebookFileName[
        InputNotebook[]], "Text"]], "Text"]; Null), PassEventsDown -> False},
WindowSize->{667, 670},
WindowMargins->{{Automatic, 0}, {Automatic, 0}},
InitializationCell->True,
FrontEndVersion->"10.0 for Microsoft Windows (64-bit) (July 1, 2014)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[558, 20, 3388, 74, 712, "Input"]
}
]
*)

(* End of internal cache information *)

Desktop/nb.nb

(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 10.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       158,          7]
NotebookDataLength[      2733,         79]
NotebookOptionsPosition[      2400,         63]
NotebookOutlinePosition[      2835,         81]
CellTagsIndexPosition[      2792,         78]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[{
 RowBox[{
  RowBox[{"$CommandLines", " ", "=", 
       RowBox[{"Import", "[", 
        RowBox[{
        "\"\<C:\\\\Users\\\\a\\\\Desktop\\\\args.txt\>\"", ",", "\"\<JSON\>\""}], 
        "]"}]}], ";"}], "\[IndentingNewLine]", 
     RowBox[{"Module", "[", 
      RowBox[{
       RowBox[{"{", "str", "}"}], ",", 
       RowBox[{
        RowBox[{"str", " ", "=", " ", 
         RowBox[{"OpenWrite", "[", 
          RowBox[{"$TemporaryDirectory", "<>", "\"\<\\\\\>\"", "<>", 
           RowBox[{"Last", "@", 
            RowBox[{"StringSplit", "[", 
             RowBox[{
              RowBox[{"$CommandLines", "[", 
               RowBox[{"[", "2", "]"}], "]"}], ",", "\"\<\\\\\>\""}], "]"}]}], 
           "<>", "\"\<.nb\>\""}], " ", "]"}]}], ";", "\[IndentingNewLine]", 
        RowBox[{"WriteString", "[", 
         RowBox[{"str", ",", "\[IndentingNewLine]", "\[IndentingNewLine]", 
          RowBox[{"StringReplace", "[", 
           RowBox[{
            RowBox[{"Import", "[", 
             RowBox[{
              RowBox[{"$CommandLines", "[", 
           RowBox[{"[", "2", "]"}], "]"}], ",", "\"\<Text\>\""}], "]"}], ",", 
        "\[IndentingNewLine]", 
        RowBox[{
        "\"\<@echo off\n\\\"C:\\\\Program Files\\\\nodejs\\\\node.exe\\\" -p \
\\\"require('fs').writeFileSync('./args.txt',JSON.stringify(process.argv));\
require('child_process').exec('C:\\\\\\\\Users\\\\\\\\a\\\\\\\\Desktop\\\\\\\\\
nb.nb',function (error, stdout, stderr) {if(error != null) {console.log('exec \
error: ' + error);}});\\\" %0 %*\nexit\n\>\"", "\[Rule]", "\"\<\>\""}]}], 
       "]"}]}], "\[IndentingNewLine]", "\[IndentingNewLine]", "]"}], ";", 
    "\[IndentingNewLine]", 
    RowBox[{"SystemOpen", "[", 
     RowBox[{"Close", "[", "str", "]"}], "]"}], ";", "\[IndentingNewLine]", 
    RowBox[{"NotebookClose", "[", 
     RowBox[{"EvaluationNotebook", "[", "]"}], "]"}], ";"}]}], 
  "\[IndentingNewLine]", "]"}]}], "Input"]
},
InitializationCellEvaluation->True,
InitializationCellWarning->False,
WindowSize->{667, 670},
WindowMargins->{{0, Automatic}, {Automatic, 0}},
InitializationCell->True,
FrontEndVersion->"10.0 for Microsoft Windows (64-bit) (July 1, 2014)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[558, 20, 1838, 41, 432, "Input"]
}
]
*)

(* End of internal cache information *)
$\endgroup$
0
$\begingroup$

test.m don't remember what this is used for.

in:=(ToExpression@InputString[]);
If[Length@$CommandLine<5,
  Print["no args"],
  Print@ToExpression@$CommandLine[[5]]
]

Evaluate Notebook.

nb = Notebook[{Cell[BoxData[ToBoxes[ToExpression@ToString[Hold[
           nb2 = CreateDocument[{
              TextCell["The head", "Section"],
              TextCell["first cell"],
              Cell[BoxData[RowBox[{"Print", "[", "23", "]"}]], "Input"]
              }];
           NotebookEvaluate[nb2, InsertResults -> True];
           NotebookClose[EvaluationNotebook[]];
           ], InputForm]][[1, 3]]], "Input", 
     InitializationCell -> True]}, 
   InitializationCellEvaluation -> False, 
   InitializationCellWarning -> False];
UsingFrontEnd@Export["C:\\Users\\a\\Desktop\\nb.nb", nb];
Run["C:\\Users\\a\\Desktop\\nb.nb"];
$\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.