1

How do I get the name of the current script?

For example if I run myscript.sc, I'd like to know the value of myscript I need this for killing previous instances of the script, still running.

david.perez
  • 6,090
  • 4
  • 34
  • 57
  • Executing `this.getClass.getProtectionDomain.getCodeSource.getLocation.getPath` command gives a path on `/tmp` folder in a linux system. Maybe that's closest one can get to the actual file name. [This comment](http://stackoverflow.com/questions/7655165/what-really-happens-behind-the-scala-runtime-repl-when-running-a-scala-progra#comment22945180_7655610) gave me the idea of changing the tempdir property for the scripts you are running. Any files in that directory would be a potential candidate of a running scala script. – jrook Apr 13 '17 at 10:08
  • Another solution that comes to my mind is executing the script with `scala -Xprint:parser filename.sc * 2> tempFile`. The first line of tempFile will always contain a line in the form: `[[syntax trees at end of parser]] // filename.sc: tree ...` which then can be used to extract filename! – jrook Apr 13 '17 at 10:15
  • If I write a script with this content: `println(this.getClass.getProtectionDomain.getCodeSource.getLocation().getPath)`, then I get `null` as the result. – david.perez Apr 18 '17 at 09:13
  • I don't know how to pass the `-Xprint:parser` argument to the `amm` binary. – david.perez Apr 18 '17 at 09:14

1 Answers1

0

sourcecode.File() will return the name of the current script

darrenmc
  • 1,721
  • 1
  • 19
  • 29