jl303 jl303 - 1 year ago 131
AppleScript Question

Applescript Iterate UIElements using Javascript

Could someone please help me to make a script that reports all the children and its properties of an uielement? It's similar to entireContents() function. Here's my recursive function.

function iterate(obj) {
for (var property in obj) {
if(obj[property] instanceof Array) {
console.log("Array: " + property + "," +obj[property])
} else if(obj[property] instanceof Object){
console.log("Object: " + property + ',' + obj[property])
} else {
console.log("Unknown: " + property +"," + obj[property]);

I only get the first level. There are bunch of UIElements and arrays under each item. I think it has to do with Applescript returning object specifier, but not actual object? I'm not sure how to invoke name of the object specifier as function. I tried objproperty, obj.property(), eval("obj." + property + "()"), ,and none of them works. I also tried iterate(app.windows())[0]

Thank you for your help.

Answer Source

I presume you want to use the commands in the Processes Suite of "System Events".

  • To get a property of an UIElement, you must use one of these properties:

(accessibilityDescription, class, description, enable, entireContents, focused, help, maximumValue, minimumValue, name, orientation, position, role, roleDescription, selected, size, subrole, title, value)

  • Example to get the name: obj[i].name().

To get all UIElements from an UIElement, you must use obj[i].uiElements()

Here's an example on the windows of the "TextEdit" process.

function iterate(obj) {
    for (var i in obj) {
        if(obj[i] instanceof Array) {
        } else if(obj[i] instanceof Object){
            console.log("Object: " + i + ': name = ' + obj[i].name() + ', value = ' + obj[i].value() + ', class = ' + obj[i].class() + ', description = ' + obj[i].description())
var sysEv = Application('System Events')
var app = sysEv.processes['TextEdit']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download