Skip to content

Instantly share code, notes, and snippets.

@DrGo
Forked from jcppkkk/OpenWithSublime.bat
Last active September 8, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrGo/d41c6d07441ca5d93f60 to your computer and use it in GitHub Desktop.
Save DrGo/d41c6d07441ca5d93f60 to your computer and use it in GitHub Desktop.
Add context menu to allow user open file or folder with Sublime as User or Admin. Save the raw file OpenWithSublimeTextAsAdmin.bat into sublime folder and execute it.
:: //***************************************************************************
:: // ***** 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
@rem \"%stPath%\" : Path to Sublime Text installation dir.
@rem %UserEntry%: Key name for the registry entry.
@rem %UserMenuText% : Context menu text. Set your preferred menu text (e.g.: translate to your language).
@rem %AdminEntry%: Key name for the registry entry.
@rem %AdminMenuText% : Context menu text. Set your preferred menu text for administrator privilege (e.g.: translate to your language).
SET stPath=%~dp0sublime_text.exe
SET UserEntry=Sublime Text
SET AdminEntry=Sublime Text As Admin
SET "UserMenuText=Open with Sublime(&-)"
SET "AdminMenuText=Open with Sublime As Admin(&+)"
SET file_elevate.cmd=%~dp0__elevate.cmd
SET file_elevate.vbs=%~dp0__elevate.vbs
SET file_uninstall.bat=%~dp0OpenWithSublimeText-uninstall.bat
if not exist "%file_elevate.cmd%" (
call :download "https://gist.github.com/jcppkkk/8330314/raw/99e41c37c89554268026028874d208aec1bb4073/__elevate.cmd" "%file_elevate.cmd%"
)
if not exist "%file_elevate.vbs%" (
call :download "https://gist.github.com/jcppkkk/8330314/raw/64d5f472ae679d0b85ac6579fa5917c0a96a5468/__elevate.vbs" "%file_elevate.vbs%"
)
if not exist "%file_uninstall.bat%" (
call :download "https://gist.github.com/jcppkkk/8330314/raw/6dbf9c50104f80d38b86d0cb552f33f47f3c343f/uninstall-OpenWithSublimeTextAsAdmin.bat" "%file_uninstall.bat%"
)
call :check_Permissions
echo # add it for all file types
reg add "HKEY_CLASSES_ROOT\*\shell\%UserEntry%" /t REG_SZ /v "" /d "%UserMenuText%" /f
reg add "HKEY_CLASSES_ROOT\*\shell\%UserEntry%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
reg add "HKEY_CLASSES_ROOT\*\shell\%UserEntry%\command" /t REG_SZ /v "" /d "\"%stPath%\" \"%%1\"" /f
echo # add it for all file types as admin
reg add "HKEY_CLASSES_ROOT\*\shell\%AdminEntry%" /t REG_SZ /v "" /d "%AdminMenuText%" /f
reg add "HKEY_CLASSES_ROOT\*\shell\%AdminEntry%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
reg add "HKEY_CLASSES_ROOT\*\shell\%AdminEntry%\command" /t REG_SZ /v "" /d "\"%file_elevate.cmd%\" \"%stPath%\" \"%%1\"" /f
echo # add it for folders
reg add "HKEY_CLASSES_ROOT\Folder\shell\%UserEntry%" /t REG_SZ /v "" /d "%UserMenuText%" /f
reg add "HKEY_CLASSES_ROOT\Folder\shell\%UserEntry%" /t REG_EXPAND_SZ /v "Icon" /d "\"%stPath%\",0" /f
reg add "HKEY_CLASSES_ROOT\Folder\shell\%UserEntry%\command" /t REG_SZ /v "" /d "\"%stPath%\" \"%%1\"" /f
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 "%file_elevate.cmd%" "%~fs0"
exit
)
goto :EOF
:download
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell" "$wc = New-Object System.Net.WebClient;$wc.DownloadFile('%1', '%2')"
echo %2
goto :EOF
@echo off
SET elevate.CmdPath=%~dp0__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
: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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment