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(selectedText) 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 "Safari" 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 set name_of_menu_item to name of menu_item log "activate : menu_item : " & name_of_menu_item log "activate : selected text : " & selectedText if selectedText as string is equal to name_of_menu_item as string then click menu_item return true end if set idx to idx - 1 end repeat end tell return false end activateWindow to activateTab(tabName, windowName) tell application "Safari" set aTab to tab 1 of window named windowName whose URL is tabName set current tab of window named windowName to aTab end tell end activateTab 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 "Safari" 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 findIdxOfTab(textToMatch, windowNameList) set idx to 1 set tabIdx to 1 repeat with windowName in windowNameList if windowName is equal to textToMatch then set tabIdx to idx end if set idx to idx + 1 end repeat return tabIdx end findIdxOfTab on traverseWindows(operation, textToMatch, siteTextList, windowList, windowNameList) tell application "Safari" activate set allWindows to every window of application "Safari" 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(name of browserWindow, textToMatch, name of browserWindow, siteTextList, windowNameList) end if if operation is equal to "activateWindow" then if my activateWindow(textToMatch) then set tabIdx to my findIdxOfTab(textToMatch, windowNameList) my activateTab(item tabIdx of siteTextList, textToMatch) return end if end if else if operation is equal to "addNames" then my addName(URL of document of browserWindow of browserWindow, textToMatch, name of browserWindow, siteTextList, windowNameList) end if if operation is equal to "activateWindow" then if my activateWindow(textToMatch) then set tabIdx to my findIdxOfTab(textToMatch, windowNameList) my activateTab(item tabIdx of siteTextList, textToMatch) 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, name of browserWindow, siteTextList, windowNameList) end if if operation is equal to "activateWindow" then if my activateWindow(textToMatch) then set tabIdx to my findIdxOfTab(textToMatch, windowNameList) my activateTab(item tabIdx of siteTextList, textToMatch) return end if end if end repeat end if end repeat end tell end traverseWindows on execute_script(website, websiteurl) -- set website to "macrumors" -- (item 1 of argv) -- set websiteurl to "https://macrumors.com" -- (item 2 of argv) -- your code goes here... tell application "Safari" 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 set selectedWindow to choose from list windowNameList if selectedWindow is false then return end if tell window named selectedWindow to make tab tell last tab of window named selectedWindow to set URL to websiteurl log "selected window name : " & selectedWindow my activateWindow(selectedWindow) my activateTab(URL of last tab of window named selectedWindow, selectedWindow) 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 activateWindow(selectedWindowName) my activateTab(siteText, selectedWindowName) end if end if end tell end execute_script on run set website to "__websitename__" set websiteurl to "__websiteurl__" my execute_script(website, websiteurl) end run