Based on @n8henrie's blog post as linked in their answer, here's a full example:
Let's find the path for the "New Tab" button in Safari. Starting from the application name:
tell application "System Events" to tell process "Safari"
set frontmost to true
delay 1
UI elements
end tell
Result:
{window "Start Page" of application process "Safari" of application "System Events", menu bar 1 of application process "Safari" of application "System Events"}
Let's choose a window and see what elements it has:
tell application "System Events" to tell process "Safari"
set frontmost to true
delay 1
tell window "Start Page"
UI elements
end tell
end tell
splitter group 1 of window "Start Page" of application process "Safari" of application "System Events", toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 1 of window "Start Page" of application process "Safari" of application "System Events", button 2 of window "Start Page" of application process "Safari" of application "System Events", button 3 of window "Start Page" of application process "Safari" of application "System Events"}
Toolbar sounds promising, let's see what's in there:
tell application "System Events" to tell process "Safari"
set frontmost to true
delay 1
tell toolbar 1 of window "Start Page"
UI elements
end tell
end tell
{group 1 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", group 2 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", group 3 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 1 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 2 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 3 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events"}
Ah, we found some buttons. Ann after a little trial and error, we end up with this:
tell application "System Events" to tell process "Safari"
set frontmost to true
delay 1
click button 2 of toolbar 1 of window "Start Page" -- open new tab
end tell
UI elements
command from here helpful as well: http://n8henrie.com/2013/03/a-strategy-for-ui-scripting-in-applescript/ – phs Sep 11 '13 at 05:46