How do I add a right-click option to search the web using a selected file's name?
I'm looking to add an option to the right-click menu in Windows Explorer that will launch Chrome and search a specific site for that file's name. To be a little more specific, I want to be able to do this on a video file and then look for available subtitles for it. I'm aware that there are a number of programs that can do some variation of this, but after trying out a bunch of them, I haven't quite found what I want. Not only that, but I'd rather use some lightweight, native code than having to install another program.
The closest thing to a guide that I've found is in this post, but it doesn't quite work. Of course, I've tried cleaning it up for my needs, but it still doesn't work, so I think there's a flaw in there. Here's what I have so far...
Running Command Prompt as Administrator, I executed these two lines to add them to the Registry:
REG ADD "HKLM\software\Classes\*\shell\Subtitle Search" /d "Subtitle Search"
REG ADD "HKLM\software\Classes\*\shell\Subtitle Search\Command" /d "C:\My Projects\My Code\SubtitleSearch.bat ""%1"""
The, I create a batch file (SubtitleSearch.bat) with the following:
set xGOOGLESEARCH=%~nx1
cmd /c "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http://subscene.com/subtitles/title?q=%xGOOGLESEARCH%"
That seems pretty straightforward, but it doesn't work. Instead, Windows thinks I'm trying to open the file and asks me what program to use. If I choose Chrome, incidentally, it actually starts playing the video in a tab.
I'm convinced the problem is with the Registry code, but I can't figure out how to tell the system that I don't want to open the file I'm right-clicking on; I just want to use its filename. I'm familiar with the Registry, but not skilled enough to see what the issue is.
Thanks in advance for your help!
@echo off
setlocal
rem Process 1st argument passed.
if "%~1" == "/?" goto :help
if "%~1" == "/query" goto :query
if "%~1" == "/register" goto :register
if "%~1" == "/unregister" goto :unregister
if "%~1" == "" exit /b 1
goto :search
exit /b 0
:help
echo Open chrome search page with filename as the item to search.
echo:
echo Syntax: "%~nx0" keyword
echo:
echo Arguments:
echo /?
echo This help message.
echo /query
echo View a query of registered entries.
echo /register
echo Register context menu entries.
echo /unregister
echo Unregister context menu entries.
exit /b 0
:query
reg query "HKLM\software\Classes\*\shell\Subtitle Search" /s
exit /b 0
:register
echo Register subtitle search
reg add "HKLM\software\Classes\*\shell\Subtitle Search" /d "Subtitle Search" /f
reg add "HKLM\software\Classes\*\shell\Subtitle Search\Command" /d "\"%~f0\" \"%%1\"" /f
if errorlevel 1 net session >nul 2>&1 || echo Require admin to register.
exit /b 0
:search
rem Filename as item to search. If want extension also, use nx modifiers instead of just n.
set "search=%~n1"
rem URL used as the search engine.
:: set "url=https://www.google.com/search?q="
set "url=https://www.startpage.com/do/search/?q="
:: set "url=http://subscene.com/subtitles/title?q="
rem Use start so that the window does not remain open. CMD not being called so surrounding double quotes not stripped.
start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%url%%search%"
rem View CMD /?. Surrounding double quotes and more than 1 pair may have the outer pair stripped. Add another escaped pair to be stripped.
:: cmd /s /c ^""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%url%%search%"^"
rem Uncomment command to check commandline of cmd.exe.
:: wmic process where caption="cmd.exe" get commandline & pause
exit /b 0
:unregister
echo Unregister subtitle search
reg delete "HKLM\software\Classes\*\shell\Subtitle Search" /f
if errorlevel 1 net session >nul 2>&1 || echo Require admin to unregister.
exit /b 0
Problems in your code:
- Escape inner double quotes with backslashes when using
reg
command i.e.""""
to"\"\""
. - Escape
%
sign with another%
so thatreg
can add literal%1
i.e.%1
to%%1
. - The "Windows thinks I'm trying to open the file" is probably caused by the double quoting issue as I have not yet reproduced.
-
CMD may strip double quotes if the command has surrounding double quotes and has more then 2 double quotes. View
cmd /?
for parsing rules:If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:
-
If all of the following conditions are met, then quote characters on the command line are preserved:
- no /S switch
- exactly two quote characters
- no special characters between the two quote characters, where special is one of: &<>()@^|
- there are one or more whitespace characters between the two quote characters
- the string between the two quote characters is the name of an executable file.
Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.
-
I have made the script all in one so you can register by:
search /register
Output expected:
Register subtitle search The operation completed successfully. The operation completed successfully.
Ensure a Command Prompt is used as Administrator as registering to HKLM
branch may require it. Where the script is located is the path added to registry. So do not register just anywhere and move it later to its correct path.
Query the registry entries by:
search /query
Output expected:
HKEY_LOCAL_MACHINE\software\Classes\*\shell\Subtitle Search (Default) REG_SZ Subtitle Search HKEY_LOCAL_MACHINE\software\Classes\*\shell\Subtitle Search\Command (Default) REG_SZ "C:\My Projects\My Code\search.cmd" "%1"
View help message by:
search /?
If uncomment wmic
command in the :search
label, Output in open console from doing a search is:
CommandLine cmd /c ""C:\My Projects\My Code\search.cmd" "C:\My Projects\My Code\Alice In Wonderland.mp4"" Press any key to continue . . .
This command by default should be disabled as a comment or removed as only added for checking the command line.
Chrome opens at this URL if startpage.com is used for search engine:
https://www.startpage.com/do/search/?q=Alice%20In%20Wonderland
If neither of those 2 arguments mentioned are not passed, and the 1st argument is something, then a search is done with Chrome. No arguments will just end the script as is useless to process nothing.
I cannot access the subscene.com
site so I have added some other URLs for testing with.
The cmd
command is disabled and use of start
has been added so the console window closes and Chrome opens. I left cmd
in the script so you can see that it can work.
To unregister:
search /unregister
Output expected:
Unregister subtitle search The operation completed successfully.
Ensure a Command Prompt is used as Administrator as unregistering from HKLM
branch may require it. Location of the script does not matter as it deletes the whole registry key.
The batch-file path contains spaces, so you have to surround it with quotes:
REG ADD "HKLM\software\Classes\*\shell\Subtitle Search\Command" /d "\"C:\My Projects\My Code\SubtitleSearch.bat\" \"%1\""
I think you do not need the ""%1"" stuff at the end of your commandline put in the Registry. The executable selected to open the file gets the target file implicitly as %1. No need to explicitly state that in your commandline. In fact, I think %1 is not even defined in that context, only inside of your batch file. The last part is probably interpreted as a literal parameter String, which does not make sense for the batch interpreter. Did you try a echo %1
somewhere at the top of your batch file to check its value?
Edit: Dang! 4 years, 9 months to wait on further answers... I feel sorry for you :(
Why don't you make a .reg file that creates a right click menu that does what you are trying to do? I am unclear of your objective so this is all I can do.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\StartChrome]
@="Start google chrome and search for ____ website"
[HKEY_CLASSES_ROOT\*\shell\StartChrome\command]
@="cmd /min /c \"start chrome.exe google.com"
This is a .reg file that creates a right-click menu prompt that says "Start google chrome and search for ____ website
" and then starts chrome and goes to Google.com. In the last @=""
part, in the quotations, put what you want the right click to do when you activate it. In the first @=""
part, you choose what the thing shouled say in the right click menu. If you want to get rid of the right-click menu option, go to the registry and go to the part of the registry you specified in the second and fourth lines and delete them. Then you can restart explorer and the options will be gone.
I tested this with brave instead of chrome replacing chrome.exe with brave.exe and it works as intended by opening google.com. In your command part of the reg file, %1 is the thing you right-click to bring up the prompt. Here is my best attempt at trying to do what you may want:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\StartChrome]
@="Start google chrome and search for ____ website"
[HKEY_CLASSES_ROOT\*\shell\StartChrome\command]
@="cmd /min /c \"start chrome.exe 'http://subscene.com/subtitles/title?q=%~nx1'"