1 (изменено: Rumata, 2020-12-16 12:51:19)

Тема: Run Here - настройка собственных команд контекстного меню каталогов

Это маленький и не очень сложный, но достаточно гибкий скрипт для управления собственными командами в контекстном меню каталогов.

Разработан скрипт достаточно давно. С его помощью я создал несколько пунктов меню для запуска в заданном каталоге, например, "Run Cygwin Here", "Run Cmd Here". Недавно я включил поддержку создания иконок меню.

Поддерживает следующие ключи:


run-here [/A] /I "menu" [/K iconfile] [/NO-CHECK] command [arguments]
run-here [/A] /U "menu"
run-here [/A] /S "menu"

menu - Пункт меню, который появится в контекстном меню
command - команда, которая будет выполнена
arguments - Необязательные аргументы для команды. По умолчанию, если не указано, передается %V, то есть текущий каталог

/A - Применить для всех пользователей. По умолчанию - только для текущего
/I - Установить пункт меню
/U - Удалить пункт меню
/S - Показать для данного пункта меню записи из реестра
/K - Указать иконку пункта меню (файл или ресурс иконки)
/NO-CHECK - Не проверять путь до команды при инсталляции

Вот некоторые рабочие примеры.

Добавить PowerShell:


run-here /i "Run PowerShell Here" /k powershell.exe powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'

Аналогично можно установить, например, FAR (здесь надо указать настоящий путь до файла, так как он расположен на вашей системе):


run-here /i "Run FAR3 Here" C:\Правильный\путь\до\Far.exe "%V"

Критика и предложения приветствуются и обсуждаются.

+ Исходный код

Здесь последняя версия скрипта на момент на писания. Самую свежую версию можно также найти по адресу: https://github.com/ildar-shaimordanov/c … n-here.bat.


@echo off

setlocal

if /i "%~1" == "" goto :run_help

:: See also
:: https://www.howtogeek.com/165268/how-to-add-open-powershell-here-to-the-context-menu-in-windows/
:: https://www.tenforums.com/tutorials/60175-open-powershell-window-here-context-menu-add-windows-10-a.html
:: https://gist.github.com/davecan/f3045266aa9e3441211ab55f9db70c2b

:: Current user
:: HKEY_CURRENT_USER\Software\Classes\Drive\shell\<menu>\command
:: HKEY_CURRENT_USER\Software\Classes\Directory\shell\<menu>\command
:: HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\<menu>\command
set "run_rootkey=HKEY_CURRENT_USER"

:: All users
:: HKEY_LOCAL_MACHINE\Software\Classes\Drive\shell\<menu>\command
:: HKEY_LOCAL_MACHINE\Software\Classes\Directory\shell\<menu>\command
:: HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\shell\<menu>\command
if /i "%~1" == "/A" (
	set "run_rootkey=HKEY_LOCAL_MACHINE"
	shift
)

set "run_classkey=%run_rootkey%\Software\Classes"

:: /I, /U or /S
set "run_action=%~1"
shift

:: "menu"
set "run_menu=%~1"
shift

if not defined run_menu (
	>&2 echo:Menu not declared
	goto :run_help
)

if not "%run_menu%" == "%run_menu:\=%" (
	>&2 echo:Incorrect menu: %run_menu%
	goto :EOF
)

set "run_subkey_list=Drive Directory Directory\Background"

if /i "%run_action%" == "/I" goto :run_install
if /i "%run_action%" == "/U" goto :run_uninstall
if /i "%run_action%" == "/S" goto :run_show

>&2 echo:Unknown action: %run_action%
goto :run_help


:run_install
if /i "%run_menu%" == "cmd" goto :run_forbidden

:: [/K iconfile]
set "run_iconfile="
if /i "%~1" == "/K" (
	set "run_iconfile=%~2"
	shift
	shift
)

:: [/NO-CHECK]
set "run_nocheck="
if /i "%~1" == "/NO-CHECK" (
	set "run_nocheck=1"
	shift
)

:: command
set "run_command=%~1"
shift

if not defined run_command (
	>&2 echo:Command not specified
	goto :run_help
)

if defined run_nocheck (
	set "run_progpath=%run_command%"
	goto :run_install_args
)

set "run_progpath="
for /f "tokens=*" %%c in ( "%run_command%" ) do (
	if not "%%~$PATH:c" == "" set "run_progpath=%%~$PATH:c"
	if exist "%%~fc" set "run_progpath=%%~fc"
)

if not defined run_progpath (
	>&2 echo:Command not found: %run_command%
	goto :run_help
)

:run_install_args

:: [arguments] or "%V"
set "run_arguments="

:run_install_args_begin
if "%~1" == "" goto :run_install_args_end
	set "run_arguments=%run_arguments% %1"
	shift
goto :run_install_args_begin
:run_install_args_end

:: http://superuser.com/a/473602
:: http://www.robvanderwoude.com/ntstart.php
:: http://msdn.microsoft.com/en-us/library/windows/desktop/cc144101%28v=vs.85%29.aspx
if not defined run_arguments set "run_arguments= "%%V""

:: Escape quotes before enclosing within quotes
set "run_arguments=%run_arguments:"=\"%"

for %%s in ( %run_subkey_list% ) do (
	reg add "%run_classkey%\%%s\shell\%run_menu%\command" /ve /d "\"%run_progpath%\"%run_arguments%" /f
)

if defined run_iconfile for %%s in ( %run_subkey_list% ) do (
	reg add "%run_classkey%\%%s\shell\%run_menu%" /v Icon /t REG_SZ /d "%run_iconfile%" /f
)
goto :EOF


:run_uninstall
if /i "%run_menu%" == "cmd" goto :run_forbidden

for %%s in ( %run_subkey_list% ) do (
	reg delete "%run_classkey%\%%s\shell\%run_menu%" /f
)
goto :EOF


:run_show
for %%s in ( %run_subkey_list% ) do (
	reg query "%run_classkey%\%%s\shell\%run_menu%" /s
)
goto :EOF


:run_forbidden
>&2 echo:the operation over this element is forbidden.
goto :EOF


:run_help
echo:Open the command identified by the menu over the folder. 
echo:
echo:%~n0 [/A] /I "menu" [/K iconfile] [/NO-CHECK] command [arguments]
echo:%~n0 [/A] /U "menu"
echo:%~n0 [/A] /S "menu"
echo:
echo:menu       The menu item of the Windows Explorer context menu.
echo:command    The command to be executed on selecting the menu.
echo:arguments  Arguments to be passed to the command (otherwise %%V).
echo:/A         Apply for all users. By default, for the current user only.
echo:/I         Install the menu item.
echo:/U         Uninstall the existing item. 
echo:/S         Show the Registry entries if they exist.
echo:/K icon    Set the menu icon.
echo:/NO-CHECK  Do not check the path to the command when installing.
goto :EOF


( 2 * b ) || ! ( 2 * b )

2

Re: Run Here - настройка собственных команд контекстного меню каталогов

Пара незначительных исправлений:

-- запретил в тексте меню символ "\"
-- небольшие косметические изменения

( 2 * b ) || ! ( 2 * b )