Launching Corona from Sublime Text 2 [Windows]

So recently I embarked on a new adventure using Lua, Corona and Sublime Text 2 all for the first time. Out the bunch my favourite so far has to be Sublime Text 2, which is all the rage these days. I find it’s approach to simple no fuss extensibility especially awesome, even if I’m still not sold on full scale development in a text editor.

I admit I’m a bit of a work flow zealot, so much so that alt-tabbing to corona and hitting ctrl-R every time was starting to get on my nerves. Fortunately thanks to Sublime Text build systems there is a fairly easy way around this.

Tab switching script

The first (and really the crux of this method) is this super handy vb script by John Lindquist. You can find an in depth explanation over at his site, but basically it intelligently seeks the corona process and refreshes the simulator if its already running. And if not it simply launches a new instance of the corona simulator.

Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
 
'Check if Corona is already running
For Each objProcess in colProcessList
If objProcess.name = "Corona Simulator.exe" then
vFound = True
End if
Next
If vFound = True then
'If Corona is already running, then switch to it and refresh
WshShell.AppActivate "C:\Program Files\Corona Labs\Corona SDK\Corona Simulator.exe"
WScript.Sleep 100
WshShell.SendKeys "{ESC}"
WScript.Sleep 100
'This has changed to be appened to the project name. So add hyphen to avoid focusing the output panel
WshShell.AppActivate " - Corona Simulator"
WScript.Sleep 200
WshShell.SendKeys "^r"
Else
'If Corona isn't running, launch it with your main.lua
WshShell.Run """C:\Program Files\Corona Labs\Corona SDK\Corona Simulator.exe """ & WScript.Arguments(0)
End If

Points to take note of:

  • I had to add “-” to the line in the original script which looks for the corona simulator. It seems corona now prefixes the project title to it’s process name e.g “Project Title – Corona Simulator”. Without this the script will instead focus the corona output window.
  • If Corona isn’t installed in the default location, you will need to modify this script to point the correct directory

Sublime Build System

The next part of the process is hooking this up in Sublime Text. This is pretty easy thanks to sublime’s json based build system configuration. The easiest way to do this is through Tools>Build System>New Build System… This will create a new empty build file. You can simply copy the supplied code over then save it with an appropriate name.

{
	"cmd": ["C:/dev/corona/CoronaLauncher.vbs", "main.lua"],
	"working_dir": "${project_path:${folder}}",
	"shell":true,
	"selector":"source.lua"
}

Notes:

  • The first line simply specifies the location of the vbscript and passes it main.lua as an argument. This should be where ever you saved the script above.
  • Unlike some of the examples posted online, I had to specify the working dir in order to get the main.lua argument above to evaluate properly
  • Setting shell to true tells sublime to launch through the native shell. This is pretty essential because it is not possible to invoke vbscript from the python exec task that sublime text uses.
  • the final line is a handy way of saying you want this build behaviour to be the default for all lua files when the build system is set to “Automatic”

That’s pretty much all there is to it. From now on you can simply launch Corona from Sublime Text by using Ctrl-B! Magic!

Addendum:

Try as I might, the one thing that still seems unpossible is capturing errors from Corona’s output panel. Although sublime provides you a handy reg-ex methods to parse error strings (and thus conveniently jump to the position in code where the errors were thrown), there is no way to actually send corona output to anywhere useful on windows. This is kinda a bummer as well for anyone hoping for a CI (continuous integration) set-up.

  • Michael

    Thanks for this writeup! I’m wondering what solution you are using for autocompletion in ST2 for Windows?

    • http://www.madewithmonsterlove.com Peter Cardwell-Gardner

      Hi Michael,

      It’s been a while (and a pc change) since I’ve worked in corona land, so I’m afraid I can’t remember exactly what I used. What I do remember though is that I never found anything particular satisfactory, beyond sublimes auto complete engine. There were a few options out there that helped bring in a few class names here and there, but nothing I consider proper auto-completion.