Jump to content

Selecting a Bookmark in Alfred > check to see if tab already exists in Chrome


Recommended Posts

I don't think this is possible with the standard bookmark feature of Alfred.

 

I have an AppleScript in my workflows that does exactly this: open a set of URLs in a browser (Chrome in this case) and check whether it's already open. Works of course with only one URL.

If you can create a workflow triggered by a keyword that actually searches for bookmarks and passes the selected one to the AppleScript action, then it may work.

 

Here's the script (you can probably reduce it if you don't need the option to open several URLs in Tabs):

on alfred_script(q)
	set old_delimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	set the_urls to every text item of q
	set AppleScript's text item delimiters to old_delimiters
	set first_url to first item of the_urls
	
	set the_window to find_url_in_windows(first_url)
	repeat with the_url in the_urls
		find_or_open_url_in_current_window(the_window, the_url, true)
	end repeat
	set the_window to find_url_in_windows(first_url) -- reactivate
end alfred_script

on find_url_in_windows(the_url)
	set window_index to 1
	set found to false
	tell application "Google Chrome"
		repeat with the_window in windows
			set found to my find_or_open_url_in_current_window(the_window, the_url, false)
			if found then
				return the_window
			end if
			set window_index to window_index + 1
		end repeat
		set the_window to front window
		open location the_url
		return the_window
	end tell
end find_url_in_windows

on find_or_open_url_in_current_window(the_window, the_url, open_if_not_found)
	tell application "Google Chrome"
		set tab_index to 1
		repeat with the_tab in tabs of the_window
			if (URL of the_tab) starts with the_url then
				activate
				tell the_window
					set active tab index to tab_index
					set index to 1
				end tell
				tell application "System Events" to tell process "Google Chrome"
					perform action "AXRaise" of window 1
				end tell
				return true
			end if
			set tab_index to tab_index + 1
		end repeat
		if open_if_not_found then
			open location the_url
			return true
		end if
		return false
	end tell
end find_or_open_url_in_current_window

 

Edited by Kassi
Fix typo
Link to comment
7 hours ago, DataLife said:

Admittedly it's beyond my experience. I can follow the script but I don't know how to pass a URL to it.

 

The script goes in a Run NSAppleScript Action and the URL is whatever is passed from the previous workflow element.

 

But you first need a workflows that searches Chrome bookmarks to plug it into.

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...