Skip to content

Instantly share code, notes, and snippets.

@ryandvmartin
Last active May 12, 2021 15:59
Show Gist options
  • Save ryandvmartin/b44ed3b9cdf9743e5d2877e2aaeb3c86 to your computer and use it in GitHub Desktop.
Save ryandvmartin/b44ed3b9cdf9743e5d2877e2aaeb3c86 to your computer and use it in GitHub Desktop.
A utility to add right-click Jupyter and Anaconda Prompt keys.
::
:: Quick function to add jupyter and anaconda keys to the currently activate environment
:: Author: Ryan Martin
:: Date: April 30, 2019
:: VERSION 1.000
:: - initial release
:: VERSION 2.000
:: - added ability to have multiple environments defined in right click keys
::
@echo off
setlocal enabledelayedexpansion
SET "ENVNAME=%~1"
SET "SHOULDREMOVE=%~2"
set "_tempvar="
if /I "%ENVNAME%" EQU "-h" set _tempvar=1
if /I "%ENVNAME%" EQU "--h" set _tempvar=1
if /I "%ENVNAME%" EQU "-help" set _tempvar=1
if /I "%ENVNAME%" EQU "--help" set _tempvar=1
if /I "%ENVNAME%" EQU "help" set _tempvar=1
if defined _tempvar (
echo.
echo. To install:
echo $ conda-env-here # sets a key to base
echo. $ conda-env-here ^<envname^> # sets a key to envname
echo.
echo. To remove:
echo. $ conda deactivate
echo. $ conda-env-here remove # removes right click to base
echo. $ conda-env-here ^<envname^> remove # removes right click to env
echo.
goto :end
)
:: figure out if the keys should be removed
if "%ENVNAME%" EQU "remove" (
if "%SHOULDREMOVE%" equ "" (
SET ENVNAME=
) else (
SET "ENVNAME=%SHOULDREMOVE%"
)
goto :remove_keys
goto :end
)
if "%SHOULDREMOVE%" equ "remove" (
call conda activate %ENVNAME% || goto :end
goto :remove_keys
goto :end
) else (
:: if nothing is passed, figure out the current env
if "%ENVNAME%" equ "" (
call where.exe activate.bat>tmpfl
set /p ACTIVATEBAT=<tmpfl
del tmpfl
set "RIGHTCLICKNAME="
goto :add_keys
) else (
:: make sure this is a valid conda environment
call conda activate %ENVNAME% || goto :end
call conda activate
set "RIGHTCLICKNAME=%ENVNAME%-"
set "ACTIVATEBAT=conda activate %ENVNAME%"
goto :add_keys
)
)
::
:: Functions
::
:add_keys
echo cmd.exe /K \"%ACTIVATEBAT%\"> tmpfl
set /p ANACONDAHERE=<tmpfl
echo "%ANACONDAHERE% & jupyter-notebook"> tmpfl
set /p JUPYTERHERE=<tmpfl
del tmpfl
:: keep the keys sorted in the right click menu with 'AA'
Reg.exe add "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaPrompt" /ve /t REG_SZ /d "&%RIGHTCLICKNAME%Anaconda" /f
Reg.exe add "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaPrompt\command" /ve /t REG_SZ /d "%ANACONDAHERE%" /f
Reg.exe add "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaJupyterHere" /ve /t REG_SZ /d "&%RIGHTCLICKNAME%Jupyter" /f
Reg.exe add "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaJupyterHere\command" /ve /t REG_SZ /d %JUPYTERHERE% /f
goto :end
:remove_keys
reg.exe query "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaPrompt" > nul 2>&1
if %errorlevel% equ 0 (
reg delete "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaPrompt" /f
reg delete "HKCU\SOFTWARE\Classes\Directory\Background\shell\AA%ENVNAME%AnacondaJupyterHere" /f
) else (
echo Nothing to do
)
goto :end
:end
exit /b 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment