jl303 jl303 - 4 months ago 21
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])
iterate(obj[property])
} else if(obj[property] instanceof Object){
console.log("Object: " + property + ',' + obj[property])
iterate(obj[property])
} else {
console.log("Unknown: " + property +"," + obj[property]);
}
}
}
iterate(app.windows())


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

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) {
            iterate(obj[i])
        } 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())
            iterate(obj[i].uiElements())
        }
    }
}
var sysEv = Application('System Events')
var app = sysEv.processes['TextEdit']
iterate(app.windows())