3

There are two duplicate's of this question that seem to only work on linux/osx currently.

So how can I viably run mathematica scripts without spawning a new session each time from command prompt?

I would like to support stdout.

William
  • 7,595
  • 2
  • 22
  • 70

1 Answers1

6

This works for running scripts. Print is not supported although the last return value defaults to the stdout.

Needs["NETLink`"];
InstallNET[];

ShowNETConsole["stdout"];
LoadNETType["System.Console"];
Console`Out@WriteLine["Hello from .NET"];

WatchFile[file_String, fun_] := 
  Module[{time, time2, w, e}, 
   w = NETNew["System.IO.FileSystemWatcher"];
   LoadNETType["System.IO.NotifyFilters"];
   LoadNETType["System.IO.File"];
   w@Path = FileNameJoin@Drop[FileNameSplit[file], -1];
   w@NotifyFilter = NotifyFilters`LastWrite;
   w@Filter = Last@FileNameSplit[file];
   e = AddEventHandler[w@Changed, myHandler];
   time = 0;
   myHandler[source_, 
     e_] := (time2 = File`GetLastWriteTime[file]@ToBinary[];
     If[time == time2, Null;, fun[source, e];];
     time = time2;);
   w@EnableRaisingEvents = True;
   Return[{w, e}];];
WatchFile["C:\\Users\\a\\Desktop\\in.txt", Function[{w, e},
  Export[FileNameJoin@
     Insert[Drop[FileNameSplit@e@FullPath, -1], "out.txt", -1], 
    ToExpression@Import[e@FullPath]];
  ]]

and the c# script.

using System;

public class Watcher
{
    public static int exit=1;
    static DateTime lastRead = DateTime.MinValue;
    public static void Main(string[] args)
    {
        System.IO.File.WriteAllText(@"C:\Users\a\Desktop\in.txt", String.Join(" ", args));
        System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
        watcher.Path = "C:\\Users\\a\\Desktop";
        watcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
        watcher.Filter = "out.txt";
        watcher.Changed += new System.IO.FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
        while (exit != 0) ;
    }
    private static void OnChanged(object source, System.IO.FileSystemEventArgs e)
    {
        DateTime lastWriteTime = System.IO.File.GetLastWriteTime(e.FullPath);
        if (lastWriteTime != lastRead)
        {
            Console.Write(
              System.IO.File.ReadAllText(e.FullPath)
            );
            exit = 0;
            lastRead = lastWriteTime;
        }
    }
}

compile script.

C:\Windows\Microsoft.Net\Framework\v4.0.30319\csc.exe test.cs
William
  • 7,595
  • 2
  • 22
  • 70