Run a Site-Specific Search on Frontmost Website

This is a trio of macros I use to dynamically generate a "search site in DuckDuckGo" URL for the frontmost web site:

DuckDuckGo Site-Specific Search Macros.kmmacros (7.6 KB)

The second macro starts by running a copy operation and checks to see if the clipboard's contents have changed. If they have, it automatically sets the search term to the copied text; if they haven't, it then prompts the user to enter a search term of their choice. The third macro is designed to be run from either Alfred or LaunchBar and so it always just uses the %TriggerValue% token for its search term variable.

At this point, both the second and third macro then execute the first macro as a sub-routine, which takes the URL from the frontmost browser window (either Safari or Chrome) and automatically sets it to a URL-encoded "site:" version. Finally, both the second and third macros combine the search term and site search variables into a single URL and open it in the default browser.

For anyone who would rather use Google instead of DuckDuckGo for this, you can just change the final "Open URL" action in both the second and third macros to use "https://www.google.com/search?q=" instead of "http://duckduckgo.com/?q=", or you can preface the search term with "!g", which will make DDG search Google automatically.
If you aren't sure how to run the third macro from Alfred or LaunchBar, I suggest first reading the Script trigger and URL trigger KM wiki pages, then check the Alfred and LaunchBar documentation to see how to set up a corresponding custom workflow or action. If you get stuck, feel free to ask questions here.

5 Likes