use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions to addName(name, textToMatch, windowName, siteTextList, windowNameList) if name contains textToMatch then set end of siteTextList to name set end of windowNameList to windowName end if log "window : " & name end addName to activateWindow(windowName, selectedText, windowList) tell application "System Events" set the_menu to item 1 of UI elements of menu bar item "Window" of menu bar 1 of application process "Google Chrome" set menu_items to menu items of the_menu set idx to (count of menu_items) repeat count of menu_items times set menu_item to item idx of menu_items log "activate : menu_item : " & (name of menu_item) set name_of_menu_item to name of menu_item if selectedText is equal to name_of_menu_item then click menu_item return true end if set idx to idx - 1 end repeat end tell return false end activateWindow to getWindowNameList() set window_names to {} tell application "Safari" set allWindows to every window repeat with aWindow in allWindows set end of window_names to name of aWindow end repeat end tell return window_names end getWindowNameList to getWindowsMenuItemList(window_names) tell application "System Events" set the_menu to item 1 of UI elements of menu bar item "Window" of menu bar 1 of application process "Google Chrome" set menu_items to menu items of the_menu set window_items to {} set idx to (count of menu_items) repeat count of menu_items times set menu_item to item idx of menu_items set name_of_menu_item to name of menu_item if window_names contains name_of_menu_item then set end of window_items to name of menu_item end if set idx to idx - 1 end repeat return window_items end tell end getWindowsMenuItemList on traverseWindows(operation, textToMatch, siteTextList, windowList, windowNameList) tell application "Google Chrome" activate set allWindows to every window of application "Google Chrome" repeat with browserWindow in allWindows set windowTabs to every tab of browserWindow if windowTabs is missing value then if document of browserWindow is missing value then if operation is equal to "addNames" then my addName(title of browserWindow, textToMatch, title of browserWindow, siteTextList, windowNameList) end if if operation is equal to "activateWindow" then if my activateWindow(title of browserWindow, textToMatch, windowList) then return end if end if else if operation is equal to "addNames" then my addName(URL of document of browserWindow of browserWindow, textToMatch, title of browserWindow, siteTextList, windowNameList) end if if operation is equal to "activateWindow" then if my activateWindow(title of browserWindow, textToMatch, windowList) then return end if end if end if else repeat with aTab in windowTabs if operation is equal to "addNames" then my addName(URL of aTab, textToMatch, title of browserWindow, siteTextList, windowNameList) end if if operation is equal to "activateWindow" then if my activateWindow(title of browserWindow, textToMatch, windowList) then return end if end if end repeat end if end repeat end tell end traverseWindows on execute_script(website, websiteurl) -- your code goes here... tell application "Google Chrome" activate set siteTextList to {} set siteCandidates to {} set windowNameList to my getWindowNameList() set filteredWindowNameList to {} set windowList to my getWindowsMenuItemList(windowNameList) my traverseWindows("addNames", website, siteTextList, windowList, filteredWindowNameList) if (count of siteTextList) is equal to 0 then tell window 1 to make tab tell last tab of window 1 to set URL to websiteurl return end if set siteText to choose from list siteTextList if siteText is not false then if (count of siteText) is greater than 0 then set siteTextSelection to item 1 of siteText set optionIdx to 1 set idx to 1 repeat with optionToSelect in siteTextList if optionToSelect is equal to siteTextSelection then set optionIdx to idx end if set idx to idx + 1 end repeat set selectedWindowName to item optionIdx of filteredWindowNameList my traverseWindows("activateWindow", selectedWindowName, siteTextList, windowList, filteredWindowNameList) end if end if end tell end execute_script on run set website to "linkme" set websiteurl to "https://link.me/dlsa" my execute_script(website, websiteurl) end run