I have several Safari Windows, and the ID of one of them.
I want to do a click on it through System Events. To do that I need to tell the application “System Events” to get the element UI Element by ID and execute the click command.
I don’t know how to write that in Applescript
Thanks,
Regards
I don’t follow.
You have the ID of the window. DO you have a UI Element ID (like a button) or do you just want to click on the window to bring it frontmost?
Hi,
Thanks for you reply. In the process of trying to find a way I got the Safari windows object through system events, and they do have an ‘id’ attribute but it’s not populated. So the ID I have from the Safari dictionary window object won’t be of much use. I thought the latter was an override of the first.
Anyway, I went through the path of the menu bar. The window menu lists at the bottom the title of all the windows and through system events I just click on the item I want.
But what I really wanted was to activate the window and switch to a tab through the name or URL. I still don’t know how to do that or if it’s even possible.
Thanks,
Regards
Here is a sample script I whipped up…
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
local myWins, aWin, myTabs, aTab, TabURLS, aURL, winIndex, winIndexes, TabNames
tell application "Safari"
set myWins to windows
set {TabURLS, winIndexes, TabNames} to {{}, {}, {}}
repeat with aWin in myWins
set {aWin, winIndex, myTabs} to {it, index, tabs} of contents of aWin
repeat with aTab in myTabs
set end of TabURLS to URL of aTab
set end of winIndexes to winIndex
set end of TabNames to name of aTab
end repeat
end repeat
end tell
if (count TabNames) < 2 then return
set aTab to (choose from list TabNames with title "Choose Safari Tabs…" with prompt "Select a tab name below…")
if aTab = false then return
set aTab to item 1 of aTab
set winIndex to getItemIndex(aTab, TabNames)
set aURL to item winIndex of TabURLS
set aWin to item winIndex of winIndexes
tell application "Safari"
set aTab to tab 1 of window aWin whose URL is aURL
set current tab of window aWin to aTab
set index of window aWin to 1
end tell
on getItemIndex(anItem, aList) -- returns index of item in list
local i
repeat with i from 1 to count aList
if (item i of aList) = anItem then return i
end repeat
return 0
end getItemIndex
Thanks for you reply.
I understood it completely.
tell application "Safari"
set aTab to tab 1 of window aWin whose URL is aURL
set current tab of window aWin to aTab
set index of window aWin to 1
end tell
This was mostly what I needed to know.
Thanks,
Regards
I’m happy that @robertfern was able to answer your question, but I don’t follow your posted code. Maybe this snippet is just missing bits that would clarify your usage. Please pardon my response if it’s not needed.
I assume before the above code you’re hard-coding aWin as the target Safari window’s index, since the whose clause delivers the tab reference, but that cannot be used to extract the tab’s window reference.
The code below will select any tab, whose URL matches the given string, in any open safari window and set that window’s index to 1.
set targetURL to "https://www.macscripter.net/t/how-do-i-get-an-ui-element-by-id-from-the-application/77735"
tell application "Safari"
set windowList to every window
repeat with thisWindow in windowList
try
set tabWithTargetURL to item 1 of (every tab of thisWindow whose URL is targetURL)
set index of thisWindow to 1
set current tab of thisWindow to tabWithTargetURL
exit repeat
on error
--this window does not contain the target tab.
end try
end repeat
end tell
I re-posted the excerpt that most interested me for what I was trying to do.
It is not a self contained script by itself.