GHC 2019-04-27

1 comment.

, https://git.io/fjspd in jarun/googler
On macOS, `webbrowser` uses `osascript` to execute either

```applescript
open location "..."
```

or

```applescript
tell application "..."
    activate
    open location "..."
end
```

`autoraise` does nothing.

I actually do know a way to open browser windows in the background specifically on macOS: `open -g`. (See `man 1 open`: "-g  Do not bring the application to the foreground.") Interestingly, `open -g Safari https://google.com/` and `open -g -a FirefoxDeveloperEdition https://google.com/` both open the relevant browser in the background, whereas `open -g -a 'Google Chrome' https://google.com/` still launches Chrome (which is my default browser, but I doubt that matters) in the foreground. So, depending on your browser of choice, it may be possible to open results without losing focus.

However, we delegate to `webbrowser` in googler and won't hack around it except when there are breaking bugs in `webbrowser`, so you'll have to patch googler yourself if you want to use `open -g` instead, which could be as simple as:

```diff
diff --git a/googler b/googler
index 01cf0be..a287e56 100755
--- a/googler
+++ b/googler
@@ -1348,7 +1348,10 @@ def open_url(url):
         os.dup2(fd, 2)
         os.dup2(fd, 1)
     try:
-        browser.open(url, new=2)
+        # Replace "Safari" with your browser of choice, or omit `-a
+        # Safari` altogether to use the default browser.
+        p = Popen(["open", "-g", "-a", "Safari", url])
+        p.communicate()
     finally:
         if open_url.suppress_browser_output:
             os.close(fd)
```

Closing as I've provided a solution and explained why it won't be incorporated into the mainline. Feel free to ask questions.