Forum moved here!

Home / Passing selection to an app e.g. Web Search [3.4 Pre-Release]

GitHubRulesOK

There have been many requests to be able to pass the selected text to a translation or web lookup / search like this

[LATER EDIT] See new look-up feature now available in 3.4 Pre-release it is similar but so much simpler. (Described in 2021 posts below)

so as to get a result such as

Most work around suggestions are to use a 3rd party macro tool such as AutoHotKey. However, there is Windows Scripting tool built into most versions.
So here is a configurable method using that tool.

' Script template to paste selected word(s) into a chosen browser search
' Expects a word or more has been selected e.g. double click will select that word
' For multiple words you will need to overstrike, right click, and copy selection.
'
' Usage:- Add to SumatraPdf-settings.txt replacing / adding to existing entry (remove the ' comment marks)
'
' ExternalViewers [
'	  [
'		CommandLine = "C:\Windows\System32\wscript.exe" "%Addins%\Search\WebSearch.vbs" <
'		Name = &Browser Search
'		Filter = *.*
'	  ]
' ]
'
' NOTE unlike other addins where the %addins% environment setting is supplied via cmd
' in this case wscript does not understand that value so you will need to edit the
' %addins% part to suit the location of this file in your system (but then not easily portable).
'
' The portable way is to use cmd.exe to call wscript but then you get the usual console Black Flash.
' CommandLine = c:\windows\system32\cmd.exe /d /c "Mode 15,1 & wscript.exe %Addins%\Search\WebSearch.vbs " <
' The Black Flash is minimised with "Mode 15,1 &". It also helps if your browser is active minimised.
'
' Also change the sections below to suit your browser preferences.
' When added as above then you should be able to select some text e.g. double click
' a word then use shortcut ALT+F+B or file menu item "Open in Browser Search"
'
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "SumatraPDF"
WScript.Sleep 100
' Copy selected text to clipboard,  Note we do not clear current clipboard contents
' which can cause problems, other script examples suggest clearing clipboard first !
' Users need to either pre-select & copy contents or ensure the search term is focused.
WshShell.SendKeys "^c"
WScript.Sleep 500

'Fetch Text from clipboard and prep for search
Set objClipMe = CreateObject("htmlfile")
SearchString = objClipMe.ParentWindow.ClipboardData.GetData("text")

' remove the ' from start of ONLY ONE of "MyBrowser" setting below or replace with your own
'MyBrowser="C:\PortableApps\FirefoxPortable\FirefoxPortable.exe"
'MyBrowser="C:\Program Files (x86)\Microsoft\Edge Beta\Application\msedge.exe"
MyBrowser="C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
'MyBrowser="C:\Program Files\Internet Explorer\iexplore.exe"

' remove the ' from start of ONLY ONE of "MySearch" setting below or replace with your own
'MySearch="https://www.startpage.com/do/dsearch?query="
'MySearch="https://www.google.com/search?q="
'MySearch="https://duckduckgo.com/?q="

'The following are for a Dictionary look-up (use with single words)
'MySearch="https://chambers.co.uk/search/?title=21st&query="
'MySearch="https://www.merriam-webster.com/dictionary/"

' The following are examples for translate English to français (use website address bar to see codes for other languages
'MySearch="https://www.deepl.com/en/translator#en/fr/"
'MySearch="https://translate.google.co.uk/#view=home&op=translate&sl=auto&tl=fr&text="
MySearch="https://www.bing.com/translator/?from=en&to=fr&text="

' ensure value is not too low otherwise commands may fail
WScript.Sleep 200
WshShell.Run "c:\windows\System32\cmd.exe /d /c "+""""+""""+MyBrowser+""""+" "+""""+MySearch+SearchString+""""+"""", 2
Set objClipMe = Nothing


Ensure you select everything in the code window and save to a filename such as WebSearch.vbs beware the extension must be .vbs NOT .vbs.txt
Alternative is to save latest copy from this link download as .txt then rename .txt to .vbs
Note the correct path to that .vbs file and use the guide at start of file as to how to add the CommandLine into Settings > Advanced Options especially the need to replace “known path” with the folder location you used.

kjk

This could be a good addition to Sumatra docs.

@GitHubRulesOK (and anyone else who’s interested in improving Sumatra documentation): I just moved majority of Sumatra docs to Notion and build a system to automate publishing the docs to the website. That makes it easy to add more content / edit existing content.

In other words: the source to https://www.sumatrapdfreader.org/docs/SumatraPDF-documentation.html is https://www.notion.so/SumatraPDF-documentation-fed36a5624d443fe9f7be0e410ecd715

Currently anyone can leave comments in Notion.

If you sign up to https://notion.so (they have a free tier) and send me the e-mail you use for that, I can add you as an editor so that you can make direct changes, add more content.

I’ll periodically push those changes to the website.

GitHubRulesOK

2021 Pre-Release 3.4 sees the introduction of user definable searches and translation. There are now built in choices and also Customised entries can be added to the context menu Here I simply added one off my own preferences above the two default ones. More on this feature soon.

PortableApp

My compliments for the option with a user definable searches\translation which can then be translated\searched on the web, a real enrichment of SumatraPDF reader.
I am happy
Alexei

GitHubRulesOK

The new section for adding customised web queries and searches (or URL other string requests is described here

  • added ability to search / translate selected text with web services

it is similar to external viewers but in a different Advanced Options (SumatraPDFsetting.txt) section

list of handlers for selected text, shown in context menu when text selection is active
SelectionHandlers [
  [
    url to invoke for the selection. ${selection} will be replaced with current selection and
    ${userlang} with language code for current UI (e.g. 'de' for German)
    URL =

    name shown in context menu
    Name =
  ]
]

To add your own just add a few lines

# This is a comment and not needed in settings

# I wish to add an alternative translate to the defaults that uses yandex for Russian (I dont use google and DeepL is defaulting to German!)
# In Google it may be something like URL = https://translate.google.com/?sl=auto&tl=${userlang}&q=${selection}
# In Bing it would be URL = https://www.bing.com/translator/?from=ru&to=${userlang}&text=${selection}
# For Yandex that needs to be forcing russian (ru) to (-) my english (en) language  using &text=${selection}

SelectionHandlers [
  [
    URL = https://translate.yandex.com/?lang=ru-en&text=${selection}
    Name = Translate from Russian to English
  ]
  [
    URL = https://translate.yandex.com/?lang=ru-es&text=${selection}
    Name = Translate from Russian to Spanish
  ]
]

Here I have just added one section based on the Spanish example