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

Hello!

How can i activate a specific window of Safari in case there are 3 open windows with different tabs and addresses.

The goal is to activate a window then activate a tab with a specified address / bring the Safari window to the front if one of it's tabs contains specified address and activate the tab. @ccstone :slight_smile:

Thank you in advance

I think this macro should do the trick, or at least show you an example you can build on:

MACRO or PLUGIN: Open or Refresh a URL in a Tab (Safari & Chrome) by @DanThomas

I use @ccstone's script for selecting a tab with a specified address.
I am looking for something else - i am willing to find a way to activate a specific window of Safari if multiple windows are open.

I need to set an index or mark a Safari window somehow
Then this action will do the work

I'm not sure I understand.

The macro I gave you (by @DanThomas) will bring the window of the tab (with the URL) to front. Isn't that what you want?

I'd guess that @ccstone's script does the same.

So, what am I missing?

Try this script. You need to set a KM Variable URLToFind to the URL of interest, and put the script in an Execute AppleScript action.

IF the URL is found, its window will be made frontmost.
ELSE, nothing happens.

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

tell application "Safari"
  set winList to windows

  repeat with oWin in winList
    tell oWin
      try
        set oTab to (first tab whose URL is urlToFind)
        set index of oWin to 1
        exit repeat
      end try
    end tell
  end repeat
  
end tell

This is just a quick-and-dirty script to get you started. It has had very little testing.

Questions?

Unfortunately, the script does nothing.
I don't need to activate a tab with address as i already know how to do it.
I need to understand how can i activate a specific window of Safari if there are more than one window opened.

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"