Bring a specified window of an app to the front (if there are more than one window)

Glad to hear it.

I have an update to the script, that should make it more reliable.
Please replace the script text in the macro with this one:

property ptyScriptName : "Make Safari Window with URL FrontMost"
property ptyScriptVer : "1.2" --  CHG Method to ID Front Win
property ptyScriptDate : "2018-06-22"
property ptyScriptAuthor : "JMichaelTX"

tell application "Keyboard Maestro Engine" to set urlToFind to getvariable "URLToFind"
--set urlToFind to "https://wiki.keyboardmaestro.com/doku.php"

set scriptResults to "FAILURE!" & linefeed & ¬
  "Sorry, could NOT find any tab in any window with this URL" & linefeed & urlToFind

tell application "Safari"
  set winList to windows
  set winTitle to ""
  
  repeat with oWin in winList
    tell oWin
      try
        set oTab to (first tab whose URL is urlToFind)
        set index of oWin to 1
        set current tab to oTab
        set scriptResults to "SUCCESS!" & linefeed & ¬
          "Window with tab URL found and set frontmost:" & linefeed & urlToFind
        
        tell application "Safari" to set oWin to front
        exit repeat
      end try
    end tell
  end repeat
  
end tell

if (scriptResults starts with "SUCCESS") then
  --- Make Sure Front Safari Window IS Active ---
  -- Window index must have been set to 1 previously
  
  tell application "System Events"
    tell application process "Safari"
      tell (front window) to perform action "AXRaise"
    end tell
  end tell
end if

return scriptResults

1 Like