Avoid duplicate domains in Chrome tabs

Hello.

The aim of the script is to avoid the repetition of tabs in Google Chrome that have the same domain.

If the new URL to be opened has the same domain as one of the existing tabs in Chrome, it is positioned and opened in it, replacing it. In this case, we are talking about opening a URL whose domain is Wikipedia.

In the code written below it works correctly (with the exception of some URLs, including the new MacScripter URL and I can’t figure out why and I’d like to know the solution). Its core is a tell block focused on the “Google Chrome” application.

if (the clipboard as string) is "" then
	display dialog "A text on the clipboard is required" with title "Something is wrong…" with icon 0 giving up after 4
	return
else
	set theString to (the clipboard) as text
end if

set domain to "https://es.wikipedia.org/"  
set theURL to "https://es.wikipedia.org/wiki/" & theString

-- for MacScripter;  It used to work well; now it doesn't
--set laURL to "https://macscripter.net/viewforum.php?id=2"  
--set domain to "https://macscripter.net/


tell application "Google Chrome" 
	activate
end tell

tell application "Google Chrome"
	repeat with w in windows
		set i to 1
		repeat with t in tabs of w
			if URL of t starts with domain then
				set active tab index of w to i
				set index of w to 1
				tell its window 1
					activate
				end tell
				
				set URL of active tab of window 1 to theURL
				delay 0.5
				
				return -- Ends if successful
			end if
			set i to i + 1 -- if the flow passes through here it means that the previous cycle was unsuccessful.
		end repeat
		-- If the flow goes through here it means that there is no tab open with a domain from this URL.…	
	end repeat
	
-- If unsuccessful, open a new tab with that theURL
	tell application "Google Chrome" to tell front window to make new tab at after (get active tab) with properties {URL:theURL} 	
	--	activate

end tell

The problem occurs when I want to modify the code so that the kernel is a System Events tell block that runs Google Chrome applications and processes.

if (the clipboard as string) is "" then
	display dialog "A text on the clipboard is required" with title "Something is not right..." with icon 0 giving up after 4
	return 
else
	set theString to (the clipboard) as text
end if

set domain to "https://es.wikipedia.org/"
set theURL to "https://es.wikipedia.org/wiki/" & theString

tell application "Google Chrome" 
	activate
end tell

tell application "System Events"
	set frontmost of application process "Google Chrome" to true
	repeat until application process "Google Chrome" is frontmost
		delay 0.1
	end repeat
	
	repeat with w in windows
		set i to 1
		repeat with t in tabs of w
			if URL of t starts with domain then
				set i to  active tab index  -- ERROR End of line expected but property found (tab)
				set index of w to 1
				tell its window 1
					activate
				end tell
				
				set URL of active tab of window 1 to theURL
				delay 0.5
				
				return
			end if
			set i to i + 1 
		end repeat	
	end repeat

	tell application "Google Chrome" to tell front window to make new tab at after (get active tab) with properties {URL:theURL} 
		--	activate
end tell

I am unable to find the solution and would appreciate help in understanding the reasons for the error and finding a solution while maintaining the System Events block.

The main mistake in the 2nd script is following: after checking frontmost state of Google Chrome, you should stop tell to System Events and begin again tell to Google Chrome block.

Other mistake is: your script should request active tab index of w instead of active tab index.
.

set theString to (the clipboard) as text

if theString is "" then
	display dialog "A text on the clipboard is required" with title "Something is not right..." with icon 0 giving up after 4
	return
end if

set domain to "https://es.wikipedia.org/"
set theURL to "https://es.wikipedia.org/wiki/" & theString

tell application "Google Chrome" to activate

tell application "System Events" to tell process "Google Chrome"
	set its frontmost to true
	repeat until (get its frontmost)
		delay 0.1
	end repeat
end tell

tell application "Google Chrome"
	repeat with w in windows
		set i to 1
		repeat with t in tabs of w
			if URL of t starts with domain then
				set active tab index of w to i -- EDITED BACK
				set index of w to 1
				tell its window 1 to activate
				set URL of active tab of window 1 to theURL
				delay 0.5
				return
			end if
			set i to i + 1
		end repeat
	end repeat
	tell front window to make new tab at after (get active tab) with properties {URL:theURL}
	--	activate
end tell

Thank you Fredrik71 for your answer.

I’m not sure if I understood your answer correctly, but it’s not about closing any tab, it’s about replacing the URL of a tab with a certain domain with another URL of the same domain to avoid duplication of domains between Chrome tabs.

Thank you KniazidisR for your reply.

With the correction you suggest, the URL of the active tab at the time of execution is replaced by another URL of the searched domain, but it does not prevent the existence of duplicate domains in Chrome tabs.

On the other hand, the prominence of the main block falls directly on “Google Chrome” and not on “System Events”, which is placed outside the main block and is only used to put this application in the foreground.

Well, I don’t understand why you replaced set active tab index of w to i of your 1st (working) script with set i to active tab index of w in your 2nd script. Try to replace it back. I updated my script (see EDITED BACK code line).

You are right, KniazidisR.
In my 2nd script that line is different.

I probably forgot to leave it the same as it was in the first script after attempts to modify it to avoid the error message (“A property cannot follow a property”) that occurred in it during compilation inside the tell application block "System Events”.

We are still trying to find a code that is compatible with a tell block presided by "System Events”.

I reiterate my thanks for your time and knowledge in trying to find a solution to the issue.
Regards.

Greetings again, Fredrik71.

The aim of my script is only to avoid, as far as possible, the unnecessary opening of multiple tabs in Chrome with URLs from the same domain.
For example, Google’s search engine and many of its extensions or Wikipedia, among others) are examples of responsible for an overload in the number of tabs in Chrome.

Continuing with the same example, if a script tries to do a search on Wikipedia, it would be enough to go through the open tabs in Chrome to check if any of them has a URL belonging to that domain and if so, make the necessary changes in that tab to reuse it for that new search (not delete that tab and open a new one with the desired URL, which is what I think you have understood).

The first of the scripts I’ve posted works perfectly, but I’m trying to rewrite it in such a way that its main code is in a block addressed by “System Events” instead of “Google Chrome”.

On the other hand, my knowledge of AppleScript is really scarce and my knowledge of Objetive-C is totally null, so I can’t take advantage of your script alternative (use framework “Foundation”).

Thanks for your time and knowledge to help.

What a good idea to have made an adaptation for mascripter.net!

I miss that you didn’t consider the possibility of opening a new tab focused on the macscripter.net homepage in case there was no tab for that domain.

I also miss not taking into account that the clipboard may be empty and, in that case, making a decision like
- terminating the script, preventing it from continuing to traverse, for no reason, through the tabs by reading their URLs,
- or, better yet, as in the previous case, open a new tab focused on macscripter.net, since the user running the script is interested in doing a search on macscripter.net.

My interest in making a version of the script giving prominence to “System Events” was because I thought it would be more efficient, but according to your comments it’s more a source of problems than of improvements, so I abandon the idea of continuing to do it.