UI Scripting, how to access AX Properties

Hello,

I’m writing a script that searches through links in the currently open window in Safari… On one of the Web area has a attribute, AXLinkUIElements, which contains an array of all the links on the current webpage. However, when I try to access it, I get a set of {application “System Events”, application “System Events”, application “System Events”, …}

Here’s my code so far…


tell application "System Events"
   tell process "Safari"
      get value of attribute "AXLinkUIElements" of UI element 1 of scroll area 1 of group 2 window 1
   end tell
end tell

Is there a way I can access the individual UI elements these references point to? I want to be able to be able to get the text value and click on the links in that Array.

Thanks,

Hi,

a better way to gather the links on a web page is to use the do Javascript command of Safari


tell application "Safari" to set num_links to (do JavaScript "document.links.length" in document 1)
set the_links to {}
repeat with i from 0 to num_links - 1
	tell application "Safari" to set end of the_links to do JavaScript "document.links[" & i & "].href" in document 1
end repeat

Thanks. Didn’t realize I can send JavaScript commands.

Now I got to figure out how to simulate a mouse click on those links. (What I want to do is scan for a link with a particular text and click on it.)

I still would also like to figure out how to access those attributes. I’ll probably be running into this alot as I work with other objects.

You don’t need any mouse click.
Safari can open an URL via AppleScript :wink:

try this example


property site_url : "http://bbs.applescript.net/index.php"
property link_text : "AppleScript | OS X"

tell application "Safari"
	activate
	open location site_url
	-- wait until page loaded
	if my page_loaded(20) is false then return
	set linkIndex to my find_link(link_text)
	if linkIndex is false then -- link text not found
		display dialog "Link \"" & link_text & "\" not found."
	else
		-- get the url and open the link url
		set link_url to do JavaScript "document.links[" & linkIndex & "].href" in document 1
		open location link_url -- opens in new page
	end if
end tell
--
on find_link(the_link)
	tell application "Safari" to set num_links to (do JavaScript "document.links.length" in document 1)
	set current_link to ""
	repeat with i from 0 to num_links - 1
		tell application "Safari" to set current_link to do JavaScript "document.links[" & i & "].text" in document 1
		if the_link is current_link then return i
	end repeat
	return false
end find_link

on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

Thanks for all the help so far.

If anyone knows, I would like to be able to figure out how to access these AX Properties. The Prefab UI Browser is able to access them, and I would like to be able to access them the same way so I can navigate and inspect the properties of the various UI elements.

Thanks,