Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
@echo off
:: A batch file which generates other batch files to run the Leo Editor,
:: adapted for the local machine. Optionally, it will also set the Windows
:: filetype and association so .leo files can be opened from Explorer.
:: It needs to live in the same folder as ""
:: Open Source X/MIT License
:: initial version * 2012-Dec-13 * matt wilkie <>
:: modified version * 2013-Jan-31 * jacob peck <>
:: revised 2013-May-05 * jacob peck <>
if "%1"=="" goto :Usage
call :pyCheck %1
call :main %1
if "%2"=="register" call :register %1
goto :eof
:: %1 is the path to folder containing python .exe's
set pyexe=%~dp1python.exe
set pywexe=%~dp1pythonw.exe
echo. Generating...
echo. Leo.bat - run leo in Windows mode
echo. Leoc.bat - run leo and keep console window open
echo. These can be placed anywhere in PATH.
:: leoc.bat - simple
echo @"%pyexe%" "" "%%*" > leoc.bat
:: leo.bat - more complicated
echo @echo off > leo.bat
echo start "leo" /B "%pywexe%" "" "%%*" >> leo.bat
goto :eof
:: perms check courtesy of
:: batch-script-how-to-check-for-admin-rights
net session >nul 2>&1
if %errorlevel% == 0 (
echo. Setting .leo filetype and registering association with Windows
ftype LeoFile=%pywexe% "" "%%1"
assoc .leo=LeoFile
) else (
echo. Error: Can't set filetype and register association.
echo. Please run from elevated shell to do that.
goto :eof
if not exist "%1" goto :Usage
goto :eof
echo. -=[%~nx0]=-
echo. Create batch files to launch Leo Editor that can be
echo. placed and run from anywhere on this machine.
echo. and optionally register filetype with windows
echo. Usage:
echo. %~n0 "c:\path\to\python.exe"
echo. %~n0 "c:\path\to\python.exe" register
goto :eof

maphew commented May 5, 2013

(for those who come later, this is a fork from, and the comments are made with regards to comparing the two.)

L32: %%1 in place of %%*: The former will only pass the 1st parameter while the 2nd will pass everything. So when using %%1 the second and further files or parameters will be ignored. A fuller exploration of this at

L34-35: using @ at the beginning of a line is the same as "@echo off for this one line", so these two lines can be collapsed to one:

echo @start "leo" /B "%pywexe%" "" "%%1" > leo.bat

L21, and eleswhere: While it's true that adding separator slash, like so %~dp1\python.exe, results in a double-slash when expanded, C:\Python27\\python.exe, I've yet to encounter a situation where the double-slash is a problem in practice. Windows doesn't seem to care. I prefer keeping the slash as it makes the code in the editor more readable, the variable is more distinct from the hard coded part. In the end though it's user preference, I just wanted to explain that it is deliberate and not an oversight. Use what's best for you.


gatesphere commented May 6, 2013

L32: Good question why I did that... no clue, really. It works, though, for my purposes. %%* is probably correct, and I will revise this.

L34-35: Google says otherwise:
That being said, I did test this both with and without the @echo off line... and it only worked with the @echo off line. At least on my systems (2 win xp, 1 win 7 64).

L21: It was complaining on my WinXP boxes, but that could be a security setting. Both of my WinXP boxes are the property of my employer, and they have some pretty crazy security protocols in place. Not sure if that has anything to do with it, but my WinXP boxes didn't like the extra slash at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment