MacOS – AppleScript: How to open a link in Google Chrome in a new, adjacent tab

applescriptgoogle-chromemacostabs

The following AppleScript code will open the link in a new tab, at the location of the very end of the current tabs:

set myLink to "https://www.google.com/"

tell application "Google Chrome"
        activate
        open location myLink
end tell

Is it possible to open a link in a new tab, that is located immediately after the currently open tab, using AppleScript?

For example, if my current Chrome window has 10 tabs, and the currently displayed tab is the fifth tab, then I would like the new tab to be inserted as the sixth tab, pushing all of the subsequent tabs over to the right one. I also do not want the new tab to be hidden; I want the currently displayed tab to be changed to the new, sixth tab upon its creation.

You can witness the desired behavior by using Chrome's built-in Google search feature. Right-click any selected text on a webpage, select Search Google for "SelectedText", and a new tab will be created, adjacent to the tab in which the search was initiated.

Best Answer

It's possible, like this :

set myLink to "https://www.google.com/"
tell application "Google Chrome"
     activate
    tell front window to make new tab at after (get active tab) with properties {URL:myLink} -- open a new tab after the current tab
end tell