Searcher

Searcher

About:
A simple Script AddIn which may be handy from time to time. It allows you to search multiple search engines with either the selected filename, the clipboard or typed text. Double click on a search engine to toggle it on or off.

The script adds a new command to DOpus (Searcher) which you can add to any menu, toolbar or hotkey.

The Search Engines are configurable in the Script AddIn configuration dialog. The format for new engines is: [Google_Images]https://www.google.com/search?tbm=isch&q=%S. A number of search engines are pre-configured.

The first item between brackets [ ] is the Search Engines name, after that put the url for the search engine, inserting %S where the search terms go.

If a file is selected in the lister when you run this script command it's name_stem will be prefilled in the search box, otherwise the clipboard contents will be pasted in. You can override this by using the CLIP argument which will force Searcher to ignore selected files and just paste from the clipboard. The other available argument is FULLNAME which will force Searcher to use the full name of the first selected item rather than just the name_stem.

The Filename and Clipboard buttons will allow you to toggle between the first selected item in the lister and the clipboard. If nothing is selected in the Lister then the Filename button will be unavailable.

The settings are saved when you close the dialog so Searcher will remember which engines you last used.

History:

  • 1.0 (26/4/20)
    • Initial Release.
  • 1.0.1 (27/4/20)
    • Return key now works from the edit control.
  • 1.1 (27/4/20)
    • Search engines are now configurable.
  • 1.2.1 (28/4/20)
    • Search engine names can now have spaces in them.
    • Added a "Clear" button to clear the search string.
    • Added more default search engines.
    • Added new command argument: FULLNAME which will force Searcher to use the full name of the first selected item instead of just the name_stem.

Installation:

Usage:

  1. Download: Searcher.dcf (240 Bytes)
  2. Select "Settings/Customize Toolbar..." from your Lister and then drag the .dcf file to any toolbar you like.
Script:

The script is included for reference only. To use searcher just download the script and button above.

// Searcher
// (c) 2020 Steve Banham

scriptName = "Searcher";
scriptVersion = "1.2.1";
scriptDate = "28/4/2020";
scriptCopyright = "(c) 2020 Steve Banham";
scriptMinVersion = "12.20";
scriptDesc = "Find selected filename, or clipboard content in various search engines.";

function OnInit(initData) {
    initData.name = scriptName;
    initData.version = scriptVersion;
    initData.copyright = scriptCopyright;
    initData.desc = scriptDesc;
    initData.default_enable = true;
    initData.min_version = scriptMinVersion;

    var vecSearchEngines = DOpus.NewVector();
    vecSearchEngines.push_back = "[Google]https://www.google.com/search?q=%S";
    vecSearchEngines.push_back = "[Google Images]https://www.google.com/search?tbm=isch&q=%S";
    vecSearchEngines.push_back = "[Bing]https://www.bing.com/search?q=%S";
    vecSearchEngines.push_back = "[Bing Images]https://www.bing.com/images/search?q=%S";
    vecSearchEngines.push_back = "[DuckDuckGo]https://duckduckgo.com/?q=%S";
    vecSearchEngines.push_back = "[DuckDuckGo Images]https://duckduckgo.com/?q=%S&ia=images&iax=images";
    vecSearchEngines.push_back = "[Wikipedia]https://en.wikipedia.org/wiki/%S";
    vecSearchEngines.push_back = "[Opus Resource Centre]https://resource.dopus.com/search?q=%S";
    vecSearchEngines.push_back = "[YouTube]https://www.youtube.com/results?search_query=%S";
    vecSearchEngines.push_back = "[Amazon]https://www.amazon.com/s?k=%S";
    vecSearchEngines.push_back = "[Amazon Australia]https://www.amazon.com.au/s?k=%S";
    vecSearchEngines.push_back = "[Ebay]https://www.ebay.com/sch/i.html?_nkw=%S";
    vecSearchEngines.push_back = "[Ebay Australia]https://www.ebay.com.au/sch/i.html?_nkw=%S";

    initData.config.search_engines = vecSearchEngines;
	initData.config_desc = DOpus.Create.Map("search_engines", "List of search engines available to select.");

    var cmd = initData.AddCommand();
    cmd.name = "Searcher";
    cmd.method = "onSearcher";
    cmd.desc = "Find selected filename, or clipboard content in various search engines.";
    cmd.label = "Searcher";
    cmd.template = "CLIP/S,FULLNAME/S";
}

function onSearcher(scriptCmdData) {

    var srcTab = scriptCmdData.func.sourcetab;
    var strEngineName;
    var strEngineUrl;

    var dlg = DOpus.Dlg;
    dlg.title = scriptName + " " + scriptVersion+ " - Directory Opus";
    dlg.template = "dlgSearch";
	dlg.LoadPosition("Steve_Searcher");
    dlg.detach = true;
    dlg.Create();

    dlg.Control("listEngines").columns.AddColumn("Search Engine");
    dlg.Control("listEngines").columns.AddColumn("Active");

    for (var eEngines = new Enumerator(Script.Config["search_engines"]); !eEngines.atEnd(); eEngines.moveNext()) {
        strEngineName = eEngines.item();
        
        if (strEngineName.search("]") > -1) {
            strEngineName = strEngineName.substring(1,strEngineName.search("]"));
            strEngineNameSafe = strEngineName.replace(/ /g, "_");
            dlg.Control("listEngines").AddItem(strEngineName);

            if (!DOpus.Vars.Exists("Searcher_" + strEngineNameSafe) || DOpus.Vars.Get("Searcher_" + strEngineNameSafe) == undefined) {
                DOpus.Vars.Set("Searcher_" + strEngineNameSafe) = "Yes";
                
                dlg.Control("listEngines").GetItemByName(strEngineName).subitems(0) = "Yes";
            }
            else {
                dlg.Control("listEngines").GetItemByName(strEngineName).subitems(0) = DOpus.Vars.Get("Searcher_" + strEngineNameSafe);
            }
            
            DOpus.Vars("Searcher_" + strEngineNameSafe).persist = true;
        }
        else {
            DOpus.Output("Error in search_engines configuration.");
            return;
        }
    }

    if (scriptCmdData.func.args.got_arg.clip) {
        dlg.Control("editSearch").value = DOpus.GetClip();
    }
    else {
        if (srcTab.stats.selitems > 0) {
            if (scriptCmdData.func.args.got_arg.fullname) {
                dlg.Control("editSearch").value = srcTab.selected(0).name;
            }
            else {
                dlg.Control("editSearch").value = srcTab.selected(0).name_stem;
            }
        }
        else {
            dlg.Control("editSearch").value = DOpus.GetClip();
        }
    }
    
    if (srcTab.stats.selitems < 1) {
        dlg.Control("btnFile").enabled = false;
    }

    dlg.Control("listEngines").columns.AutoSize();

    dlg.Show();

    while (true) {
        var msg = dlg.GetMsg();
       if (!msg.result) break;

       if (msg.event == "click" && dlg.Control("btnFile").focus == true) {
            if (srcTab.stats.selitems > 0) {
                if (scriptCmdData.func.args.got_arg.fullname) {
                    dlg.Control("editSearch").value = srcTab.selected(0).name;
                }
                else {
                    dlg.Control("editSearch").value = srcTab.selected(0).name_stem;
                }
            }
       }

       if (msg.event == "click" && dlg.Control("btnClip").focus == true) {
           if (DOpus.GetClipFormat() == "text") {
               dlg.Control("editSearch").value = DOpus.GetClip();
           }
       }

       if (msg.event == "click" && dlg.Control("btnClear").focus == true) {
           dlg.Control("editSearch").value = "";
       }

       if (msg.event == "dblclk" && dlg.Control("listEngines").focus == true) {

            var intSelItem = dlg.Control("listEngines").value.index;

            var strEngineNameSafe = dlg.Control("listEngines").GetItemAt(intSelItem).name;
            strEngineNameSafe = strEngineNameSafe.replace(/ /g, "_");

            if(dlg.Control("listEngines").GetItemAt(intSelItem).subitems(0) == "Yes") {
                          
                DOpus.Vars.Set("Searcher_" + strEngineNameSafe) = "No";
                dlg.Control("listEngines").GetItemAt(intSelItem).subitems(0) = "No";
            }
            else {
                DOpus.Vars.Set("Searcher_" + strEngineNameSafe) = "Yes";
                dlg.Control("listEngines").GetItemAt(intSelItem).subitems(0) = "Yes";
            }
       }

       if (msg.event == "click" && dlg.Control("btnSearch").focus == true || msg.event == "click" && dlg.Control("editSearch").focus == true) {

            var intSearch = 0;

            if (dlg.Control("editSearch").value == "") {
                DOpus.Output("No text to search.");
            }
            else {
                for (var eEngines = new Enumerator(Script.Config["search_engines"]); !eEngines.atEnd(); eEngines.moveNext()) {

                    var cmd = scriptCmdData.func.command;
                    strEngineUrl = eEngines.item();
                    strEngineName = eEngines.item();
                    strEngineName = strEngineName.substring(1,strEngineName.search("]"));
                    strEngineNameSafe = strEngineName.replace(/ /g, "_");

                    if (dlg.Control("listEngines").GetItemByName(strEngineName).subitems(0) == "Yes") {

                        intSearch = 1;
                        var strSearch = dlg.Control("editSearch").value;
                        strSearch = strSearch.replace(/ /g, "+");
                        strEngineUrl = strEngineUrl.substring(strEngineUrl.search("]"));
                        strEngineUrl = strEngineUrl.replace("]","");
                        strEngineUrl = strEngineUrl.replace("%S", strSearch);
                        if (strEngineNameSafe == "Wikipedia") {
                            strEngineUrl = strEngineUrl.replace(/\+/g, "_");
                        }
                        cmd.AddLine(strEngineUrl);
                    }
                }

                if (intSearch == 1) {

                    cmd.Run();
                    cmd.Clear();
                }
                else DOpus.Output("No Search Engines Selected.");     
            }
        }
    }

    dlg.SavePosition("Steve_Searcher");
}

function OnAboutScript(aboutData){
    dlg = DOpus.Dlg;
    dlg.window = aboutData.window;
    dlg.title = scriptName + scriptVersion + " - Directory Opus";
    dlg.message = scriptName + " v" + scriptVersion + "\t\t\t\t\t" + scriptDate + "\n\n" + scriptDesc + "\n\n" + scriptCopyright;
    dlg.buttons = "Close";
    dlg.icon = "info";
    dlg.show;
}

==SCRIPT RESOURCES
<resources>
	<resource name="dlgSearch" type="dialog">
		<dialog fontsize="8" height="202" lang="english" resize="yes" title="Searcher - Directory Opus" width="165">
			<control halign="left" height="12" name="editSearch" resize="w" type="edit" width="157" x="4" y="4" />
			<control height="14" name="btnFile" title="&amp;Filename" type="button" width="50" x="4" y="20" />
			<control height="14" name="btnClip" title="&amp;Clipboard" type="button" width="50" x="57" y="20" />
			<control fullrow="yes" height="142" name="listEngines" noheader="yes" nosortheader="yes" resize="wh" type="listview" viewmode="details" width="157" x="4" y="38" />
			<control default="yes" height="14" name="btnSearch" resize="xy" title="&amp;Search..." type="button" width="50" x="57" y="184" />
			<control close="0" height="14" name="btnClose" resize="xy" title="C&amp;lose" type="button" width="50" x="110" y="184" />
			<control height="14" name="btnClear" title="Cl&amp;ear" type="button" width="50" x="110" y="20" />
		</dialog>
	</resource>
</resources>

9 Likes

Thanks!

Loving this!

1 Like

Great tool. Thanks. Just one thing, i type or put something into the text box and press return nothing happens, I have to click on the 'Search' button with mouse :slight_smile:

It does now. :smiley:

I've uploaded v1.1 which now has configurable search engines. Let me know if there are any issues please.

1 Like

That was quick. Many thanks. Works great. All it needs now is a search as you type drop down list lol :grinning:

Been using v 1.1. Works great, thankyou.
This might come in useful for searching amazon for items

[Amazon com]http://amazon.com/s/ref=as_li_ss_tl?field-keywords=%S&tag=lnkref1-20&linkCode=ur2

Also some way to clear the text field would be nice, a button or triple click to select all text.

I've added a button to clear the search field in v1.2.1. There's a new argument available (FULLNAME) and a few more default search engines.

You can right click the search field and choose "Select All" to select all text in it.

1 Like

Hi Steve, it's me once again. I have tried to Drag & drop Searcher 1.2.1.js.txt in to Preferences / Toolbars / Scripts but it's failed (load). I have tried to import also with the import button but the script failed to load. My DO version is 12.21

What does it say in Tools > Script Log?

Tools > Script Log say nothing Leo. Here is my screen shot

That means the file could not be loaded. Either something was locking it so it could not be opened, or antivirus (wrongly) decided it was unsafe and blocked it.

If you go to /scripts and change the extension from .js to .js.txt, that might keep your antivirus happy, if it's that causing the problem.

1 Like

Yes It's Works now. Thank Leo. Thanks Steve for the scriptaddin.

1 Like

Great tool. @Steve Any chance to implement search by image? (Asumming an image file is selected)

Nice idea. I started a new job a couple of months ago which is taking most of my time. Will keep it in mind for when things slow down.

This can do reverse image search nicely via https://imgops.com/ or directly using search engines.

Just download the latest version and drop the exe in a folder somewhere like with your dopus stuff.

@runmode hide
@noprogress 
@hidenosel:type=*.(jpg|jpeg|png|webp|bmp|gif)
@nodeselect
"yourpath\imgops.exe" search {filepath$} -t yandex

This is a button for Yandex search but you can specify other search engines, such as -t google instead. Google image search has been severely nerfed lately so Yandex currently offers the best results imo.