Create a gist now

Instantly share code, notes, and snippets.

Embed
create-leo.bat
@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 "launchLeo.py"
::
:: Open Source X/MIT License
:: initial version * 2012-Dec-13 * matt wilkie <maphew@gmail.com>
:: modified version * 2013-Jan-31 * jacob peck <suschord@suspended-chord.info>
:: revised 2013-May-05 * jacob peck <suschord@suspended-chord.info>
if "%1"=="" goto :Usage
call :pyCheck %1
call :main %1
if "%2"=="register" call :register %1
goto :eof
:main
:: %1 is the path to folder containing python .exe's
set pyexe=%~dp1python.exe
set pywexe=%~dp1pythonw.exe
echo.
echo. Generating...
echo.
echo. Leo.bat - run leo in Windows mode
echo. Leoc.bat - run leo and keep console window open
echo.
echo. These can be placed anywhere in PATH.
echo.
:: leoc.bat - simple
echo @"%pyexe%" "%~dp0launchLeo.py" "%%*" > leoc.bat
:: leo.bat - more complicated
echo @echo off > leo.bat
echo start "leo" /B "%pywexe%" "%~dp0launchLeo.py" "%%*" >> leo.bat
goto :eof
:register
:: perms check courtesy of http://stackoverflow.com/questions/4051883
:: batch-script-how-to-check-for-admin-rights
net session >nul 2>&1
if %errorlevel% == 0 (
echo.
echo. Setting .leo filetype and registering association with Windows
echo.
ftype LeoFile=%pywexe% "%~dp0launchLeo.py" "%%1"
assoc .leo=LeoFile
) else (
echo. Error: Can't set filetype and register association.
echo. Please run from elevated shell to do that.
echo.
)
goto :eof
:pyCheck
if not exist "%1" goto :Usage
goto :eof
:usage
echo.
echo. -=[%~nx0]=-
echo.
echo. Create batch files to launch Leo Editor that can be
echo. placed and run from anywhere on this machine.
echo.
echo. and optionally register filetype with windows
echo.
echo. Usage:
echo. %~n0 "c:\path\to\python.exe"
echo. %~n0 "c:\path\to\python.exe" register
echo.
goto :eof
@maphew

This comment has been minimized.

Show comment
Hide comment
@maphew

maphew May 5, 2013

(for those who come later, this is a fork from https://code.google.com/p/maphew/source/browse/other/create-leobat.bat, 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 https://groups.google.com/d/msg/leo-editor/li9ILUWuNAs/3TZnm_U06KMJ

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%" "%~dp0launchLeo.py" "%%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.

maphew commented May 5, 2013

(for those who come later, this is a fork from https://code.google.com/p/maphew/source/browse/other/create-leobat.bat, 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 https://groups.google.com/d/msg/leo-editor/li9ILUWuNAs/3TZnm_U06KMJ

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%" "%~dp0launchLeo.py" "%%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

This comment has been minimized.

Show comment
Hide comment
@gatesphere

gatesphere 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: http://www.instructables.com/id/Slightly-More-Advanced-Basic-Batch/step2/ECHO-OFFON-Command/
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.

Owner

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: http://www.instructables.com/id/Slightly-More-Advanced-Basic-Batch/step2/ECHO-OFFON-Command/
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