Skip to content

Instantly share code, notes, and snippets.

@jcppkkk
Forked from FoxColdMetal/OpenWithSublimeText.bat
Last active June 25, 2023 22:07
Show Gist options
  • Save jcppkkk/8330314 to your computer and use it in GitHub Desktop.
Save jcppkkk/8330314 to your computer and use it in GitHub Desktop.
[[[ Move to https://github.com/jcppkkk/OpenWithSublime !!! ]]] Add context menu to allow user open file or folder with Sublime as User (or as Admin).
Add context menu to allow user open file or folder with Sublime as User or Admin.
Installation
=====
1. Right Click on OpenWithSublime.bat's raw button, save it to folder where sublime_text.exe is
2. Execute OpenWithSublime.bat to install context menu.
Alternative
=====
To open txt file in sublime with double-click, Try https://www.binaryfortress.com/NotepadReplacer/
@echo off
:: Path to Sublime Text installation dir.
SET stPath=%~dp0sublime_text.exe
SET stPathOnly=%~dp0
:: Key name for the registry entries.
SET UserEntry=Sublime Text
SET AdminEntry=Sublime Text As Admin
:: Context menu texts.
SET "UserMenuText=Open with Sublime(&-)"
SET "AdminMenuText=Open with Sublime As Admin(&+)"
SET GIST_WORKSPACE=https://gist.github.com/jcppkkk/8330314/raw
SET F_ELEVATE_CMD=OpenWithSublime_elevate.cmd
SET F_ELEVATE_VBS=OpenWithSublime_elevate.vbs
SET F_UNINSTALL=OpenWithSublime_uninstall.bat
call :download %GIST_WORKSPACE%/%F_ELEVATE_CMD% %F_ELEVATE_CMD%
call :download %GIST_WORKSPACE%/%F_ELEVATE_VBS% %F_ELEVATE_VBS%
call :download %GIST_WORKSPACE%/%F_UNINSTALL% %F_UNINSTALL%
call :check_Permissions
echo ===================================
echo Add context menu entry for all file types
SET REG_BASE=HKEY_CLASSES_ROOT\*\shell\%UserEntry%
reg add "%REG_BASE%" /t REG_SZ /v "" /d "%UserMenuText%" /f
reg add "%REG_BASE%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
reg add "%REG_BASE%\command" /t REG_SZ /v "" /d "\"%stPath%\" \"%%1\"" /f
echo Add context menu entry for all file types, open as admin
SET REG_BASE=HKEY_CLASSES_ROOT\*\shell\%AdminEntry%
reg add "%REG_BASE%" /t REG_SZ /v "" /d "%AdminMenuText%" /f
reg add "%REG_BASE%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
reg add "%REG_BASE%\command" /t REG_SZ /v "" /d "\"%stPathOnly%%F_ELEVATE_CMD%\" \"%stPath%\" \"%%1\"" /f
echo ===================================
echo Add context menu entry for folders
SET REG_BASE=HKEY_CLASSES_ROOT\Folder\shell\%UserEntry%
reg add "%REG_BASE%" /t REG_SZ /v "" /d "%UserMenuText%" /f
reg add "%REG_BASE%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
reg add "%REG_BASE%\command" /t REG_SZ /v "" /d "\"%stPath%\" \"%%1\"" /f
echo Add context menu entry for directories background
SET REG_BASE=HKEY_CLASSES_ROOT\Directory\Background\shell\%UserEntry%
@reg add "%REG_BASE%" /t REG_SZ /v "" /d "%UserMenuText%" /f
@reg add "%REG_BASE%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
@reg add "%REG_BASE%\command" /t REG_SZ /v "" /d "\"%stPath%\" \"%%V\"" /f
echo ===================================
echo All done! press any key to leave.
echo ===================================
pause
goto :EOF
:check_Permissions
echo # Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Administrative permissions confirmed.
goto :EOF
) else (
echo Failure: Current permissions inadequate. Try to get elevation...
SET openwithsublime_elevation=1
call "%F_ELEVATE_CMD%" "%~fs0"
exit
)
:download
if not exist "%CD%\%2" (
C:\Windows\System32\WindowsPowerShell\v1.0\powershell "$wc = New-Object System.Net.WebClient;$wc.DownloadFile(\"%1\", \"%2\")"
echo Download %2
)
goto :EOF
:: //***************************************************************************
:: // ***** Script Header *****
:: // =======================================================
:: // Elevation PowerToys for Windows Vista v1.1 (04/29/2008)
:: // =======================================================
:: //
:: // File: Elevate.cmd
:: //
:: // Additional files required: Elevate.vbs
:: //
:: // Purpose: To provide a command line method of launching applications that
:: // prompt for elevation (Run as Administrator) on Windows Vista.
:: //
:: // Usage: elevate.cmd application <application arguments>
:: //
:: // Version: 1.0.0
:: // Date : 01/02/2007
:: //
:: // History:
:: // 1.0.0 01/02/2007 Created initial version.
:: //
:: // ***** End Header *****
:: //***************************************************************************
@setlocal
@echo off
:: Pass raw command line agruments and first argument to Elevate.vbs
:: through environment variables.
set ELEVATE_CMDLINE=%*
set ELEVATE_APP=%1
if not DEFINED openwithsublime_elevation taskkill /f /im sublime_text.exe
start wscript //nologo "%~dpn0.vbs" %*
' //***************************************************************************
' // ***** Script Header *****
' // =======================================================
' // Elevation PowerToys for Windows Vista v1.1 (04/29/2008)
' // =======================================================
' //
' // File: Elevate.vbs
' //
' // Additional files required: Elevate.cmd
' //
' // Purpose: To provide a command line method of launching applications that
' // prompt for elevation (Run as Administrator) on Windows Vista.
' //
' // Usage: (Not used directly. Launched from Elevate.cmd.)
' //
' // Version: 1.0.1
' // Date : 01/03/2007
' //
' // History:
' // 1.0.0 01/02/2007 Created initial version.
' // 1.0.1 01/03/2007 Added detailed usage output.
' //
' // ***** End Header *****
' //***************************************************************************
Set objShell = CreateObject("Shell.Application")
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objWshProcessEnv = objWshShell.Environment("PROCESS")
' Get raw command line agruments and first argument from Elevate.cmd passed
' in through environment variables.
strCommandLine = objWshProcessEnv("ELEVATE_CMDLINE")
strApplication = objWshProcessEnv("ELEVATE_APP")
strArguments = Right(strCommandLine, (Len(strCommandLine) - Len(strApplication)))
If (WScript.Arguments.Count >= 1) Then
strFlag = WScript.Arguments(0)
If (strFlag = "") OR (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "-?") OR (strFlag="h") _
OR (strFlag = "?") Then
DisplayUsage
WScript.Quit
Else
objShell.ShellExecute strApplication, strArguments, "", "runas"
End If
Else
DisplayUsage
WScript.Quit
End If
Sub DisplayUsage
WScript.Echo "Elevate - Elevation Command Line Tool for Windows Vista" & vbCrLf & _
"" & vbCrLf & _
"Purpose:" & vbCrLf & _
"--------" & vbCrLf & _
"To launch applications that prompt for elevation (i.e. Run as Administrator)" & vbCrLf & _
"from the command line, a script, or the Run box." & vbCrLf & _
"" & vbCrLf & _
"Usage: " & vbCrLf & _
"" & vbCrLf & _
" elevate application <arguments>" & vbCrLf & _
"" & vbCrLf & _
"" & vbCrLf & _
"Sample usage:" & vbCrLf & _
"" & vbCrLf & _
" elevate notepad ""C:\Windows\win.ini""" & vbCrLf & _
"" & vbCrLf & _
" elevate cmd /k cd ""C:\Program Files""" & vbCrLf & _
"" & vbCrLf & _
" elevate powershell -NoExit -Command Set-Location 'C:\Windows'" & vbCrLf & _
"" & vbCrLf & _
"" & vbCrLf & _
"Usage with scripts: When using the elevate command with scripts such as" & vbCrLf & _
"Windows Script Host or Windows PowerShell scripts, you should specify" & vbCrLf & _
"the script host executable (i.e., wscript, cscript, powershell) as the " & vbCrLf & _
"application." & vbCrLf & _
"" & vbCrLf & _
"Sample usage with scripts:" & vbCrLf & _
"" & vbCrLf & _
" elevate wscript ""C:\windows\system32\slmgr.vbs"" -dli" & vbCrLf & _
"" & vbCrLf & _
" elevate powershell -NoExit -Command & 'C:\Temp\Test.ps1'" & vbCrLf & _
"" & vbCrLf & _
"" & vbCrLf & _
"The elevate command consists of the following files:" & vbCrLf & _
"" & vbCrLf & _
" elevate.cmd" & vbCrLf & _
" elevate.vbs" & vbCrLf
End Sub
@echo off
SET elevate.CmdPath=%~dp0OpenWithSublime_elevate.cmd
call :check_Permissions
REM uninstall old version
SET entryName=Sublime
SET entryNameAsAdmin=Sublime As Admin
REG DELETE "HKEY_CLASSES_ROOT\*\shell\%entryName%" /f
REG DELETE "HKEY_CLASSES_ROOT\*\shell\%entryNameAsAdmin%" /f
REG DELETE "HKEY_CLASSES_ROOT\Folder\shell\%entryName%" /f
REM uninstall new version
SET entryName=Sublime Text
SET entryNameAsAdmin=Sublime Text As Admin
REG DELETE "HKEY_CLASSES_ROOT\*\shell\%entryName%" /f
REG DELETE "HKEY_CLASSES_ROOT\*\shell\%entryNameAsAdmin%" /f
REG DELETE "HKEY_CLASSES_ROOT\Folder\shell\%entryName%" /f
REG DELETE "HKEY_CLASSES_ROOT\Directory\Background\shell\%entryName%" /f
:check_Permissions
echo # Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Administrative permissions confirmed.
goto :EOF
) else (
echo Failure: Current permissions inadequate. Try to get elevation...
SET openwithsublime_elevation=1
call "%elevate.CmdPath%" "%~fs0"
exit
)
goto :EOF
Copy link

ghost commented Jan 24, 2014

Works like a charm! Thanks

@natbrazil
Copy link

Hi,
Would this work with a portable .exe with Win 8.1 64 bit?
I have an old mini wav player I'm trying to send specific .wav files there instead of the normal one.
Any help is greatly appreciated

@jcppkkk
Copy link
Author

jcppkkk commented Mar 4, 2014

@natbrazil I currently use this script on Win 8.1 64bit and everything works well. It's very likely your player can use same script with proper modification.

@samal-rasmussen
Copy link

Thanks for this! Works as intended.

Took a minute to figure out to extract all three files and run them from the sublime text folder. The instructions are there at the top, just not very noticeable at the moment.

@Way
Copy link

Way commented May 1, 2014

Great job! Simple and it works as expected. Thank you for that.

@frankfuu
Copy link

frankfuu commented Jun 7, 2014

Amazing

@Spiralis
Copy link

Very cool 👍

@Basster
Copy link

Basster commented Aug 13, 2014

Awesome! Thank you!

@dstroyrofworlds
Copy link

Werk uistekend. Wonderlik.

@ethanliew
Copy link

Cool ! Thanks

@manzanofab
Copy link

Awesome awesome awesome!!!

@jamesmullen
Copy link

How do I remove what this did?

@danmaispace
Copy link

Awesome!

@amadeomano
Copy link

Wow, amazing.. Thanks a lot 😄

@den-run-ai
Copy link

great!

@LGRI
Copy link

LGRI commented Feb 11, 2015

Hi, I had upgraded my sublime text 2 to 3 and how would i able to remove the sublime 2 context menu(install by your code)?

@xtu
Copy link

xtu commented Mar 24, 2015

It doesn't work well on my laptop. Files are opened but there is no content.

@jcppkkk
Copy link
Author

jcppkkk commented Apr 16, 2015

🎈 Update! 🎈

  • Auto prompt for elevation on install / uninstall
  • Add shortcut [-/+] to sublime entries
  • Add uninstaller (will be auto download by new version)

For person who installed old version:

  1. Delete old __elevate.cmd
  2. Download OpenWithSublimeText.bat into sublime folder and execute it again.

@Mr-A
Copy link

Mr-A commented Apr 17, 2015

@jcppkkk once you elevated and edit the file, seems you cannot edit the contextual manual to edit other files with the same ST window, unless you close it. is this normal? But thanks for the great work tho.

@jcppkkk
Copy link
Author

jcppkkk commented Jul 17, 2015

@Mr-A Yes it's normal. It's due to windows's security implementation. Process with elevated permission cannot interact with non elevated processes. e.g. normal editing new file with sublime will try to use elevated sublime if there is one, and that will lead to a silent failure.

@hainh
Copy link

hainh commented Aug 26, 2015

Confirm as work insanely in Windows 10 :)

@DrGo
Copy link

DrGo commented Sep 8, 2015

Worked without a hitch in Windows 10 (and ST3). 👍
Thanks for sharing! If you are ever in Winnipeg drop by for a beer :)

@blaskotron
Copy link

What needs to change in the bat to work with sublime portable? Thanks.

Edit, my mistake in not copying the bat to the install directory.

@SiriusBits
Copy link

So simple; download the zip, extract to my Sublime folder (ST3 x64 on Win 8.1 => Program Files\Sublime Text 3), run the batch file and done. Worked like a charm! Thank you!

@davidcorbin
Copy link

Very useful! Thank you 😄

@zanechua
Copy link

zanechua commented Oct 6, 2017

Elevate as admin no longer working for Windows 10 Fall Creators Update

EDIT:

I guess being more explicit is never a problem.

Simple Fix: https://gist.github.com/zanechua/534e86f169803e5cfaa8442a23580b4a

@jcppkkk
Copy link
Author

jcppkkk commented Oct 29, 2017

Thanks @zanechua's fix. I applied it 😄

@Heraes-git
Copy link

Thanks, it worked fine ! Great use of powershell and vbscript commands in a batch file !
(the dynamic downloading of your project is pretty amazing !)

I'm new in Github, so I don't understand how to post a fork, but I see a few things to change in your code :

OpenWithSublime.bat

Line 6 and 7, you forgot to use doublequotes for values containing spaces :

SET "UserEntry=Sublime Text"
SET "AdminEntry=Sublime Text As Admin"

Line 59, the use of

SET openwithsublime_elevation=1 

in relationship with OpenWithSublime_elevate.cmd seems to be useless, because inevitable.

@pawelpanek81
Copy link

Thanks for including HKEY_CLASSES_ROOT\Directory\Background\shell\ as well

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