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
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
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.
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.
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.
Example Results
MACRO: Bring Safari Window with URL to Front [Example]
#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/a/2/a21b8bafac5d623f6a8cd3acc99642ea758e93bd.kmmacros">Bring Safari Window with URL to Front [Example].kmmacros</a> (3.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---

---
### AppleScript to Make Safari Window FrontMost with URL
```applescript
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?
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
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.
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
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]
==**UPDATED**==: 2018-10-13 18:06 GMT-5
ADD `activate` command to ensure Chrome window is frontmost
#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/c/8/c8935ee2b7ab1540bd567bd4c94bfe9bc4b7a300.kmmacros">Activate Chrome Browser Tab to Specified URL [Example].kmmacros</a> (4.6 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---

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.