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

The macro by @DanThomas shows me an error, not sure what does it means
I see that other users of this macro are also having the same problem

Actually, the script works fine in my testing with Safari 11.1.1 (12605.2.8) on macOS 10.12.6.
But it does require that you properly set the KM Variable URLToFind to the full URL of an existing tab in one of the Safari windows.

My script does exactly that. It sets the Safari Window frontmost which has a tab with the specified URL.

It does exactly as you asked in your OP:

Here's a macro with the enhanced script embedded.
You will need to change the first Action to be the URL of an existing tab, whose window you want to make frontmost.

image


Example Results

image


MACRO:   Bring Safari Window with URL to Front [Example]

~~~ VER: 1.0    2018-06-22 ~~~

DOWNLOAD:

Bring Safari Window with URL to Front [Example].kmmacros (3.8 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


image


AppleScript to Make Safari Window FrontMost with URL

property ptyScriptName : "Make Safari Window with URL FrontMost"
property ptyScriptVer : "1.1"
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 "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 winID to id
        set winTitle to name
        set current tab to oTab
        set scriptResults to "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 (winTitle ≠ "") then
  tell application "System Events"
    tell application process "Safari"
      set oWin2 to first window whose name is winTitle
      set focused of oWin2 to true
      tell oWin2 to perform action "AXRaise"
    end tell
  end tell
end if

return scriptResults

Questions, comments, issues, suggestions?

1 Like

Works perfectly!

Not sure what i did wrong previously, thank you @JMichaelTX!

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

BTW, there are some reports (which I cannot confirm) that occasionally this method of activating a Safari window will fail. Please let me know if you have any failures.

Thanks.

1 Like

Hey ,@JmichaelTX!

How can i do the same trick in Chrome? Don't know how to modify the script for it to work with Chrome.

Thanks in advance

See:

SCRIPT: Activate Google Chrome Tab for Specified URL

1 Like

Hello, @JMichaelTX!

One more thing about the script:

If a tab with "URLToFind" is at the front then a desired window gets frontmost, if the tab with an URL is not at the front then it just switching to the tab but the window stays deactivated.
Any ideas how activate the window after selecting the tab with specified URL?
I can record a video for if my explanation isn't clear enough :slight_smile:

Thanks a lot in advance!

That is NOT the behavior I observe on my Mac:
Running Google Chrome 69.0.3497.100 (3497.100) on macOS 10.12.6.

In ALL cases, both the Window and the Tab with the existing URL are made frontmost.

Perhaps that would be best.

You can see in the video that GC window is not activated, after triggering the macro it does find the required url and activates the tab but the window stays deactivated.

Actually, I can NOT see what you described. I just tested this again with this macro, and it works for me in all cases:

MACRO:   Activate Chrome Browser Tab to Specified URL [Example]

~~~ VER: 1.2    2018-10-13 ~~~

UPDATED: 2018-10-13 18:06 GMT-5
ADD activate command to ensure Chrome window is frontmost

DOWNLOAD:

Activate Chrome Browser Tab to Specified URL [Example].kmmacros (4.6 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


image

Please test this macro as is with no changes.

I have 2 GC windows on different screens and searching for an url in both windows, the macro above does the work but doesn't make the window frontmost after activating a tab.

In the video you can see that the window is not active - it is greyed out, and after selecting a tab with URL it is still not active. I am not sure how to activate it, the only way i see is to use "move and click mouse" to activate the window as i know on which screen it is located.

OK, I was finally able to reproduce your behavior.
I have revised the complete script here:
SCRIPT: Activate Google Chrome Tab for Specified URL

But it is just adding one command here;

  if not found then
    make new tab at window 1 with properties {URL:targetURL}
  end if
  
  activate # <--- ADD THIS
  
end tell

Let me know if this fixes the issue for you.

Unfortunately, nothing changed
GC gets activated but wrong window, not the window with "URLToFind"

Well, actually that is a change. You said before that no Chrome window was activated.

Are you using my Macro exactly as is with no changes?
What versions of macOS, KM, and Chrome are you running?

Can anyone else reproduce the behaviour that @kreal is reporting?

After some exhaustive, frustrating testing, I have found this:

  1. This only fails when the macro is triggered from the Chrome window that does NOT contain the target URL
  2. Works fine triggered from the KM Editor
  3. Works fine when script is run from Script Debugger.

My only tentative conclusion is that this is somehow related to the other bug/change produced by Google in their latest Chrome update to not properly identify the frontmost window name.

@peternlewis and Chris (@ccstone) , any thoughts?

I am using the exact macro you provided above without editing.

MacOS Mojave, Google Chrome Version 69.0.3497.100, KM 8.2.4

You are right, it fails when the macro is trigerred from the Chrome window.
There is another scenario that fails:

Chrome 1st window is opened in main screen
Chrome 2nd window (which containts the target URL) is opened in 2nd screen
Trigerring macro from 3rd screen from any other app makes the Chrome tab active but doesn't make the 2nd Chrome window active.

I guess finding 1st, 2nd, 3rd etc browser window OR incognito window and make it frontmost would solve my question.

I've confirmed with another experienced user that Chrome is currently broke. So, I'll not spend any more time on this until it is fixed.

1 Like

Is there a way to perform this action in Safari?
I was unable to activate a Safari window with the target URL.

Didn't you already ask, and get an answer to this question:

It does work, finds and brings a tab with the target URL frontmost but it doesn't make the Safari window with the target URL active.
As you previously said - Chrome is broken and and it's not possible to activate a window with searchable tab so i am wondering is Safari also broken or we need to modify the script? As the script doesn't activate the window after selecting a tab.