Number of children of an UI element

I would like to know the number of childer if a particular UI element (the group I need is relative to the end of the group). So far, statement like this have been shot down by the compiler:

length of the children of UI element 1 of scroll area 1 of group 4 of window 1

I know the data is there (UI browser indicates there are almost 400 children in this particular instance). Any ideas?

Thanks,

Andy

P.S. Any idea how to register a double-click? I did click twice in a row (also tried with a delay) – no luck.

You may want to check this script out.
I made it during a very boring day, and I don’t know how complete it is, but it worked for me.
(You can compare results with UI browser, a GUI with the same purpose)
Applescript can’t record doubleclicks AFAIK; you could program interface elements (buttons etc.) that send AE to applescript to respond on doubleclicks, however.

Cheerio.

Eelco Houwink


--------------------------------Browse UI hierarchy by Eelco Houwink, Oct 2005------------------------------

tell application "System Events"
	set fApp to name of some application process whose frontmost is true and background only is false
	set Appz to name of application processes
	tell process fApp
		set frontmost to true (* optional *)
	end tell
	tell application fApp to ¬
		set appn to choose from list Appz with prompt "Select app" OK button name "OK"
	--set fApp to some application process whose frontmost is true
	set targName to item 1 of appn
	set mainTarget to application process targName
	
	repeat
		set UIlist to (get mainTarget's UI elements)
		set UInames to {}
		repeat with h from 1 to number of items in UIlist
			try
				set name1 to (get name of item h of UIlist)
				if name1 = "" then set name1 to ("itempje " & h) as string
			on error
				set name1 to (class of item h of UIlist & " " & h) as string
			end try
			set UInames to UInames & {name1}
		end repeat
		if UInames = {} then exit repeat
		tell me to set selname to (choose from list UInames with prompt "UI elements of \"" & targName & "\": " default items 1 OK button name "Browse") as string
		if selname is "false" then exit repeat
		set itemNo to my getIndex(selname, UInames)
		set mainTarget to item itemNo of UIlist
		(*with timeout of 1 second
				ignoring application responses*)
		--	if class of mainTarget ≠ menu bar then click mainTarget
		(*end ignoring
			end timeout*)
		try
			set targName to name of mainTarget
			if name of mainTarget = "" then set targName to fApp
		on error
			set targName to fApp
		end try
	end repeat
end tell
return mainTarget

on getIndex(i, l)
	repeat with n from 1 to count l
		if l's item n is i then return n
	end repeat
	0
end getIndex

While terms like ‘parent’ and ‘children’ are used to describe general relationships between UI elements, Andy, they aren’t actually relevant keywords in this context. The term ‘length’, used as a property in certain situations, is not really relevant to GUI scripting, either. (In other words, you won’t find the terms in the Processes Suite of System Events’ AppleScript dictionary.)

The situation you’ve outlined could easily be one in which you’re trying to script a browser like Safari. Even if that’s not the case, I’ll use Safari as an example, anyway - just to provide a convenient ‘tell’ wrapper. (Hopefully, you’re right about the ‘path’ so far to the area that interests you.)

One way to get the children of a UI element is to use attribute “AXChildren”:

tell application "System Events" to tell process "Safari"
	value of attribute "AXChildren" of UI element 1 of scroll area 1 of group 4 of window 1
end tell

However, this is usually pretty much the same as saying:

tell application "System Events" to tell process "Safari"
	UI elements of UI element 1 of scroll area 1 of group 4 of window 1
end tell

To count objects, use the the count command:

tell application "System Events" to tell process "Safari"
	count UI elements of UI element 1 of scroll area 1 of group 4 of window 1
end tell

As Eelco says, double-clicking can be problematic - although there are sometimes ways around the obstacles. It’s hard to generalise with this stuff - since each situation can differ slightly.

Let’s say we wanted to open an item on the desktop. (There are obviously better ways than using GUI scripting for this, but this is just to demonstrate the point.) To do this manually, we’d normally just double-click the icon - but we can’t do that effectively using GUI scripting. However, Finder items have an action that could prove useful here: “AXOpen”:

tell application "System Events" to tell group 1 of scroll area 1 of process "Finder"
	set l to name of UI elements
	tell me to tell (choose from list l)
		if it is false then error number -128
		set i to item 1
	end tell
	perform action "AXOpen" of UI element i
end tell

For more specific help on this, though, I’m afraid you’ll need to give us a bit more information…

And if all else fails, it might also be worth checking out Extra Suites, which has a ‘double click’ parameter for its ‘ES click mouse’ command.