Skip to content

Instantly share code, notes, and snippets.

@TheZoc
Last active January 26, 2023 18:08
Show Gist options
  • Save TheZoc/e3295df094801fa937596964499ac048 to your computer and use it in GitHub Desktop.
Save TheZoc/e3295df094801fa937596964499ac048 to your computer and use it in GitHub Desktop.
Sublime Text right click context menu and PATH variable setup (Windows)
@ECHO off
REM ===========================================================================
REM Sublime Text - Path and Right Click context menu
REM Felipe Guedes - https://github.com/TheZoc
REM https://gist.github.com/TheZoc/e3295df094801fa937596964499ac048
REM ---------------------------------------------------------------------------
REM I've swapped my environment so many times recently, that I decided to write
REM this overengineered solution to add Sublime Text 3 to the right click menu
REM on Windows, and to add it to the current user PATH environment variable
REM allowing `subl.exe` to be easily usable from command line.
REM
REM I also added an uninstall section to keep things clean.
REM I hope this makes things easier in the future for whenever someone needs to
REM setup a new work environment for themselves.
REM ===========================================================================
REM =====================
REM Configuration Options
REM =====================
SET AppName=Sublime Text
SET ContextMenuText=Open with Sublime Text
SET AppWorkDir=C:\Program Files\Sublime Text\
SET AppPath=%AppWorkDir%sublime_text.exe
REM ===============================================
REM Advanced Options
REM Only change them if you know what you're doing!
REM ===============================================
SET RegAppKey=%AppName: =%
SET AppIcon=%AppPath%,0
REM ===========================
REM Check for admin permissions
REM ===========================
REM https://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file/10052222#10052222
"%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" >nul 2>&1
REM ========================================
REM If error flag set, we do not have admin.
REM ========================================
if '%ERRORLEVEL%' NEQ '0' (
ECHO Requesting administrative privileges...
GOTO UAC_PROMPT
) else (
GOTO START_SCRIPT
)
REM =======================================
REM Open the script in a new command prompt
REM with Administrator privileges.
REM =======================================
:UAC_PROMPT
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
SET params= %*
ECHO UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
ECHO The script will run in a separate Window with the required privileges.
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:START_SCRIPT
REM ===============================
REM Setup Colors - Windows 10 only!
REM ===============================
for /f "tokens=4-5 delims=. " %%i in ('ver') do SET VERSION=%%i.%%j
if "%VERSION%" == "10.0" (
SET COLOR_RESET=
SET COLOR_GREEN=
SET COLOR_YELLOW=
SET COLOR_RED=
) ELSE (
SET COLOR_RESET=
SET COLOR_GREEN=
SET COLOR_YELLOW=
SET COLOR_RED=
)
REM ============================
REM Check if the paths are valid
REM ============================
IF EXIST "%AppWorkDir%" (
SET WORK_DIR_VALID=%COLOR_GREEN%[VALID]%COLOR_RESET%
) ELSE (
SET WORK_DIR_VALID=%COLOR_RED%[INVALID]%COLOR_RESET%
)
IF EXIST "%AppPath%" (
SET APP_PATH_VALID=%COLOR_GREEN%[VALID]%COLOR_RESET%
) ELSE (
SET APP_PATH_VALID=%COLOR_RED%[INVALID]%COLOR_RESET%
)
REM ===========================
REM Print current configuration
REM ===========================
ECHO.
ECHO This script will install the right click context menu for %AppName%.
ECHO This is the current configuration for this script:
ECHO.
ECHO %COLOR_YELLOW%AppName:%COLOR_RESET% %AppName%
ECHO %COLOR_YELLOW%RegAppKey:%COLOR_RESET% %RegAppKey%
ECHO %COLOR_YELLOW%Context menu text:%COLOR_RESET% %ContextMenuText%
ECHO %COLOR_YELLOW%Application working directory:%COLOR_RESET% %AppWorkDir% %WORK_DIR_VALID%
ECHO %COLOR_YELLOW%Application Path:%COLOR_RESET% %AppPath% %APP_PATH_VALID%
ECHO %COLOR_YELLOW%Application Icon:%COLOR_RESET% %AppIcon%
ECHO.
IF NOT EXIST "%AppWorkDir%" (
ECHO Application work directory is invalid. Edit the script, fix it, and run the script again.
PAUSE
GOTO:EOF
)
IF NOT EXIST "%AppPath%" (
ECHO Application path is invalid. Edit the script, fix it, and run the script again.
PAUSE
GOTO:EOF
)
REM =====================================================================
REM Check if it's already installed, and give the option to uninstall it.
REM =====================================================================
REG QUERY "HKEY_CLASSES_ROOT\*\shell\%RegAppKey%" >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
GOTO :PROMPT_INSTALL
) ELSE (
GOTO :PROMPT_UNINSTALL
)
:PROMPT_INSTALL
ECHO|SET /p="Do you want to %COLOR_GREEN%ADD%COLOR_RESET% it to the context menu and path"
CHOICE /M ""
IF %ERRORLEVEL% EQU 1 GOTO :INSTALL
GOTO:EOF
:PROMPT_UNINSTALL
ECHO This application is already registered.
ECHO|SET /p="Do you want to %COLOR_RED%REMOVE%COLOR_RESET% the context menu and path"
CHOICE /M ""
IF %ERRORLEVEL% EQU 1 GOTO :UNINSTALL
GOTO:EOF
REM =======================
REM Better safe than sorry!
REM =======================
ECHO Unknown Operation.
PAUSE
GOTO:EOF
:INSTALL
REM =====================================
REM Add the required keys to the registry
REM =====================================
REM Add it for all files
REG ADD "HKEY_CLASSES_ROOT\*\shell\%RegAppKey%" /ve /f /d "%ContextMenuText%" >nul
REG ADD "HKEY_CLASSES_ROOT\*\shell\%RegAppKey%" /v "Icon" /f /d "%AppIcon%" >nul
REG ADD "HKEY_CLASSES_ROOT\*\shell\%RegAppKey%\command" /ve /f /d "%AppPath% \"%%1\"" >nul
REM Add it for folders
REG ADD "HKEY_CLASSES_ROOT\Folder\shell\%RegAppKey%" /ve /f /d "%ContextMenuText%" >nul
REG ADD "HKEY_CLASSES_ROOT\Folder\shell\%RegAppKey%" /v "Icon" /f /d "%AppIcon%" >nul
REG ADD "HKEY_CLASSES_ROOT\Folder\shell\%RegAppKey%\command" /ve /f /d "%AppPath% \"%%v\"" >nul
REM Add it for background right click on a folder
REG ADD "HKEY_CLASSES_ROOT\Directory\Background\shell\%RegAppKey%" /ve /f /d "%ContextMenuText%" >nul
REG ADD "HKEY_CLASSES_ROOT\Directory\Background\shell\%RegAppKey%" /v "Icon" /f /d "%AppIcon%" >nul
REG ADD "HKEY_CLASSES_ROOT\Directory\Background\shell\%RegAppKey%\command" /ve /f /d "%AppPath% \"%%v\"" >nul
REM Add the appication path to the current user path environment variable
FOR /F "usebackq tokens=2,*" %%A IN (`REG QUERY HKCU\Environment /v PATH`) DO SET USER_PATH=%%B
IF NOT "%USER_PATH:~-1%"==";" (
SET USER_PATH=%USER_PATH%;
)
SETX PATH "%USER_PATH%%AppWorkDir%;" >nul
ECHO.
ECHO Context menu %COLOR_GREEN%installed%COLOR_RESET%!
PAUSE
GOTO:EOF
:UNINSTALL
REM =============================
REM Revert installation procedure
REM =============================
REG DELETE "HKEY_CLASSES_ROOT\*\shell\%RegAppKey%" /f >nul
REG DELETE "HKEY_CLASSES_ROOT\Folder\shell\%RegAppKey%" /f >nul
REG DELETE "HKEY_CLASSES_ROOT\Directory\Background\shell\%RegAppKey%" /f >nul
REM ===============
REM Remove app path
REM ===============
REM Advanced Usage of ~: https://ss64.com/nt/syntax-substring.html
FOR /F "usebackq tokens=2,*" %%A IN (`REG QUERY HKCU\Environment /v PATH`) DO SET USER_PATH=%%B
CALL SET NEW_PATH=%%USER_PATH:%AppWorkDir%;=%%
SETX PATH "%NEW_PATH%" >nul
ECHO Context menu %COLOR_RED%uninstalled%COLOR_RESET%!
PAUSE
GOTO:EOF
@TheZoc
Copy link
Author

TheZoc commented Mar 11, 2022

@akotulu Updated the script, thanks for the feedback :)

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