sobota 26. června 2010

Running Scala scripts as executables in Windows

Just recently I discovered a very useful pair of windows commands - ASSOC and FTYPE. Basically they are used to set file associations in windows via command line. While reading help for the FTYPE command I came across one particularly useful use-case: you can use it to associate script files with interpreters and then directly run your scripts like normal executables.
All you have to do (apart from downloading and installing Scala distribution from http://www.scala-lang.org/) is associate ".scala" file extension with some file type (e.g. I used "Scala.File") typing command
assoc .scala=Scala.File
And after that you have to specify command string for your new file type:
ftype Scala.File=^%SCALA_HOME^%\bin\scala.bat "%1" %*
I assume you have set your environment variable SCALA_HOME pointing to your Scala installation folder. I used escaped form of variable dereferencing, because this way I can change my SCALA_HOME to any new location and this file association is still going to work (provided there is scala.bat file in bin subdirectory).

Now you can create some simple test script called hello.scala
println("hello world script")
args.foreach(println(_))
 And after executing it with "hello.scala a b c d" it should print:
hello world script
a
b
c
d

Žádné komentáře: