Search n Activate Chrome Tab

Hi

I have tried to mod @ccstone’s script

I wanted to select a tab & activate if URL search returned more then 1 tab

I tried but cud not Display Title of tab in Prompt to select tab

I think the Activate tab Handler cud be lot better !

Third Try-> works, is it possible to hide “winID” & “tabID”, show titleID in prompt, but return winID & tabID of selection ?



tell application "Google Chrome"
	set restoreURL to URL of active tab of front window as text
	#set targetURL to "https://www.extendoffice.com/"
	set targetURL to "nysora.com"
	tell application "Google Chrome"
		tell windows
			set tabList to tabs where its URL contains targetURL
			-- -->>> set TitleList to title of tabs where its URL starts with targetURL
		end tell
	end tell
	
	try
		tabList / 0
	on error e
		set oldDelims to (get AppleScript's text item delimiters)
		set AppleScript's text item delimiters to {"Can’t make {", "} into type real.", "{", "}", ", ", "«class asDB» id (application \"Google Chrome\") of "}
		set tabList to text items of e
		repeat with theTab in tabList
			if contents of theTab = "" then set contents of theTab to 0
		end repeat
		
		set tabList to text of tabList
		
		set AppleScript's text item delimiters to oldDelims
		if length of tabList = 0 then
			display dialog "No match was found!" buttons {"OK"} default button 1
			--Or Create new tab with url
			# tell application "Google Chrome" to make new tab at window 1 with properties {URL:targetURL}
		else if length of tabList = 1 then
			set theTab to item 1 of tabList
			
			ActivateTab(theTab) of me
			
		else
			set tab_MatchList to {}
			
			set oldDelims to (get AppleScript's text item delimiters)
			set AppleScript's text item delimiters to {"id ", " of"}
			
			repeat with theTab in tabList
				set {tabID, winID} to {text item 2, text item 4} of theTab
				set titleID to GetTitle(tabID, winID) of me
				set end of tab_MatchList to winID & "- " & tabID & "- " & titleID
				set tabID to tabID as integer
			end repeat
			set AppleScript's text item delimiters to oldDelims
			-- Req help to get Title of Tab Here
			set which_Tab to choose from list of tab_MatchList with prompt "The following Tabs matched, please select one:"
			
			if which_Tab is not equal to false then
				set oldDelims to (get AppleScript's text item delimiters)
				set AppleScript's text item delimiters to "-"
				set tmp to text items of (which_Tab as string)
				set tabID to (item 2 of tmp) as integer
				set winID to (item 1 of tmp) as integer
				
				set AppleScript's text item delimiters to oldDelims
				
				findTabID(tabID, winID) of me
				raiseWindowOne() of me -- brings tabs to front
			end if
		end if
	end try
end tell


--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on raiseWindowOne()
	tell application "System Events"
		tell application process "Google Chrome"
			tell window 1
				perform action "AXRaise"
			end tell
		end tell
	end tell
end raiseWindowOne
------------------------------------------------------------------------------
on GetTitle(tabID, winID)
	tell application "Google Chrome" to set titleTab to (get title of tab id tabID of window id winID)
	return titleTab
end GetTitle
-------------------------------------
on ActivateTab(theTab)
	-- Needs Handlers --> findTabID & raiseWindowOne
	set oldDelims to (get AppleScript's text item delimiters)
	set AppleScript's text item delimiters to {"id ", " of"}
	set {tabID, winID} to {text item 2, text item 4} of theTab
	set tabID to tabID as integer
	set AppleScript's text item delimiters to oldDelims
	tell application "Google Chrome" to set tabIDList to id of tabs of window id winID
	
	findTabID(tabID, winID) of me -- Finds tabID & activates it
	raiseWindowOne() of me -- brings tabs to front
	
end ActivateTab
----------------------------------------------------------------------------------
on findTabID(tabID, winID)
	tell application "Google Chrome" to set tabIDList to id of tabs of window id winID
	
	repeat with i from 1 to length of tabIDList
		if (item i of tabIDList) = tabID then
			set tabIndex to i
			exit repeat
		end if
	end repeat
	tell application "Google Chrome"
		tell window id winID
			set index to 1
			set active tab index to tabIndex
		end tell
	end tell
end findTabID


I can’t disagree with you.


set searchURL to "nysora.com" -- open some webpage with similar URL to test

tell application "Google Chrome"
	set restoreURL to URL of active tab of window 1 -- remember active page's URL (optional)
	
	-- provide matchs list to choose diialog
	set tab_MatchList to {}
	repeat with aWindow in windows
		set winID to id of aWindow
		repeat with aTab in (tabs of aWindow where its URL contains searchURL)
			tell aTab to set {tabID, |title|} to {id, title}
			set end of tab_MatchList to "" & winID & "- " & tabID & "- " & |title|
		end repeat
	end repeat
	
	set which_Tab to choose from list of tab_MatchList with prompt "Following Tabs matched, please select one:"
	if which_Tab is false then return
	
	-- determine tab ID and window ID of the choice
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "-"}
	tell (which_Tab as string) to set {winID, tabID} to {text item 1 as integer, text item 2 as integer}
	set AppleScript's text item delimiters to ATID
	
	-- make chosen tab and its window active
	set tabIDList to id of tabs of window id winID
	repeat with i from 1 to length of tabIDList
		if (item i of tabIDList) = tabID then exit repeat
	end repeat
	tell window id winID to set {index, active tab index} to {1, i}
end tell

Hi KniazidisR

Thank you for looking at the Script

the script failed when nothing matched → it returned “Google chrome got an error:List is empty”

Also wud open a dialog if it matched one tab

I have taken liberty to mod it


tell application "Google Chrome"
	set restoreURL to URL of active tab of front window as text
	set searchURL to "nysora.com"
	# set targetURL to text returned of (display dialog "Enter a URL to search for:" default answer "" with title "Find Google Chrome Tab")
	
	-- provide matchs list to choose diialog
	set tab_MatchList to {}
	repeat with aWindow in windows
		set winID to id of aWindow
		repeat with aTab in (tabs of aWindow where its URL contains searchURL)
			tell aTab to set {tabID, title} to {id, title}
			set end of tab_MatchList to "" & winID & "- " & tabID & "- " & title
		end repeat
	end repeat
	if length of tabList = 0 then
		display dialog "No match was found!" buttons {"OK"} default button 1
		--Or Create new tab with url
		# tell application "Google Chrome" to make new tab at window 1 with properties {URL:targetURL}
	else if length of tab_MatchList = 1 then
		ActivateChromeTab(tab_MatchList) of me
	else if length of tab_MatchList > 1 then
		set which_Tab to choose from list of tab_MatchList with prompt "Following Tabs matched, please select one:"
		if which_Tab is false then return
		ActivateChromeTab(which_Tab) of me
	end if
end tell

on ActivateChromeTab(which_Tab)
	tell application "Google Chrome"
		-- determine tab ID and window ID of the choice
		set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "-"}
		tell (which_Tab as string) to set {winID, tabID} to {text item 1 as integer, text item 2 as integer}
		set AppleScript's text item delimiters to ATID
		
		-- make chosen tab and its window active
		set tabIDList to id of tabs of window id winID
		repeat with i from 1 to length of tabIDList
			if (item i of tabIDList) = tabID then exit repeat
		end repeat
		tell window id winID to set {index, active tab index} to {1, i}
	end tell
end ActivateChromeTab

Yes, these are 2 omissions on my part. This happens to me when I’m in a hurry to go to work. But you fixed them perfectly yourself. Congratulations on a well-written script.

Thanks @ KniazidisR for being generous, it was your code, I am glad I cud mod it

Cheers