Skip to content

Instantly share code, notes, and snippets.

@neremin
Last active April 6, 2018 15:34
Show Gist options
  • Save neremin/8cbb234543de3b66743500cbfd3633d8 to your computer and use it in GitHub Desktop.
Save neremin/8cbb234543de3b66743500cbfd3633d8 to your computer and use it in GitHub Desktop.
Batch script for Sublime Text 3 shell menus & launch alias ("st.exe") registering. Keep it next to sublime_text.exe and run.
@ECHO off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
@IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
@GOTO ADMINTASKS
:ELEVATE
ECHO Elevated privileges are temporarily required to run script
cd /d %~dp0
mshta "javascript: var shell = new ActiveXObject('Shell.Application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1); close();"
EXIT /B
:ADMINTASKS
: Set code page to UTF8 if your script encoding is (and contains non-ASCII chars)
chcp 65001
ECHO Adding Sublime Text 3 "Open With" context menu
: Solution for escaping '&' chars in args from here: http://stackoverflow.com/a/3875881
setlocal enableDelayedExpansion
set "root_dir=%~dp0"
: Does string have a trailing slash? if so remove it
if !root_dir:~-1!==\ set root_dir=!root_dir:~0,-1!
set sublime_path=!root_dir!\sublime_text.exe
set sublime_path=\"!sublime_path!\"
set sublime_0="!sublime_path!,0"
set sublime_1="!sublime_path! \"%%1\""
set sublime_v="!sublime_path! \"%%V\""
REM The "Icon" is optional
: Open files menu item
reg add "HKCR\*\shell\Edit in ST3" /ve /f /t REG_SZ /d "Edit in ST3"
reg add "HKCR\*\shell\Edit in ST3" /v Icon /f /t REG_SZ /d !sublime_0!
reg add "HKCR\*\shell\Edit in ST3\command" /ve /f /t REG_SZ /d !sublime_1!
: Right click ON a folder
reg add "HKCR\Directory\shell\Edit as ST3 project" /ve /f /t REG_SZ /d "Edit as ST3 project"
reg add "HKCR\Directory\shell\Edit as ST3 project" /v Icon /f /t REG_SZ /d !sublime_0!
reg add "HKCR\Directory\shell\Edit as ST3 project\command" /ve /f /t REG_SZ /d !sublime_1!
: Right click INSIDE a folder
reg add "HKCR\Directory\Background\shell\Edit as ST3 project" /ve /f /t REG_SZ /d "Edit as ST3 project"
reg add "HKCR\Directory\Background\shell\Edit as ST3 project" /v Icon /f /t REG_SZ /d !sublime_0!
reg add "HKCR\Directory\Background\shell\Edit as ST3 project\command" /ve /f /t REG_SZ /d !sublime_v!
: Reg "st.exe" alias
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\st.exe" /ve /f /t REG_SZ /d "!sublime_path!"
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\st.exe" /v "Path" /f /t REG_SZ /d "\"!root_dir!\""
PAUSE
@ECHO on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment