Jump to content

Run Browser Web Search in the Background

Recommended Posts

Howdy Fellow Alfred-itors!


I've been trying to figure out how I could modify Alfred's awesome keyword / search engine feature to enable me to do a Google search in a new background tab—i.e., *without* automatically shifting from my current browser tab (or current application) to the new browser tab where the search opens, as Alfred does by default.  There were a few forum posts over the years from users seeking this feature—but none of them was satisfactorily answered… Ha, but since I managed to figure out a workaround riiiiiight before I was about to give up, I thought I'd pay it forward and post it here —> since perhaps there are other users who'd like to enable to this search-in-background feature.


This workflow enables you to (1) search Google using Alfred while browsing in Safari in a new background tab—as well as (2) to stay in a different (i.e., non-Safari) app and run the Google search entirely in the background… which can be helpful if you're, say, poring over an Excel spreadsheet and you want to remember to Google search something later—but don't want your focus to get yanked away from your current work.


It's a simple two-step workflow design:




INPUTS = "Keyword" —> ACTIONS = "Run Script"


NOTE:  You must select "/usr/bin/osacript (AS)" from the "Language" drop-down menu on the script action—and then select "with input as {query}" from the adjacent drop-down.


Here is the Applescript to enter into the "Script" box:

set myURL to "https://www.google.com/search?q={query}"

tell application "Safari"
	tell front window
		make new tab at end of tabs with properties {URL:myURL}
	end tell
end tell



And finally, here are screen-grabs of how I configured both workflow items:







I hope someone out there finds this helpful!





Link to comment

For those who prefer Google Chrome over Safari, here's the alternative AppleScript that should enable the same background search features in Chrome:


set myURL to "https://www.google.com/search?q={query}"

tell application "Google Chrome"
	set activeIndex to get active tab index of window 1
		tell window 1
			set newTab to make new tab with properties {URL:myURL}
		end tell
		set active tab index of window 1 to activeIndex
end tell



Edited by AvantGardener
Link to comment
  • 5 months later...
  • 1 month later...
  • 9 months later...
On 8/20/2018 at 7:17 AM, AvantGardener said:

set activeIndex to get active tab index of window 1

I made the workflow exactly as you did, but it doesn't stay in the app and goes to Google Chrome, although it remains at the previous tab and let the tab searching google in background. 


Any suggestions? Thanks.


Link to comment

Thanks a lot, deanishe! Though I'm not able to add code, since I don't know how to program🙄. Would you, or anyone else, be able to help me modify the code so that I can make the web search in the currently-active app and let Chrome search in background? I would much appreciate!  

Link to comment

@asphoa There you go. This alternative Workflow works by opening the search in the browser then switching to the app that was frontmost.

It has the disadvantage that there’s a short “flash” of the browser’s window being brought to the front and then back, but it has a few advantages as well: it doesn’t depend on a browser’s AppleScript dictionary, so there’s no need to add permissions for it and will keep working even if the dictionary changes; works for any browser (even Firefox); if you change your default browser, no need to modify the Workflow.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...