asmagill / hammerspoon_asm

Hammerspoon modules in progress...
MIT License
52 stars 12 forks source link

Question - Number of UI Element? #5

Closed latenitefilms closed 8 years ago

latenitefilms commented 8 years ago

Sorry for all the questions! Hope you don't mind!

Is there any way to return the number of a specific UI item?

For example, say I have an AXGroup which contains three other AXGroup's (let's call them group 1, 2, 3). If I have my mouse over group 2, and use elementAtPosition, is there any easy way to return the group number (i.e. 2)?

screen shot 2016-09-23 at 10 32 56 am
latenitefilms commented 8 years ago

Here's my hacky workaround. It works - but it gets really slow the more items you have. If you have any ideas how to do better, let me know!

firstClipPosition = hs.mouse.getAbsolutePosition()
    currentElement = ax.systemWideElement():elementAtPosition(firstClipPosition)    
    oneElementBack = currentElement:attributeValue("AXParent")  
    selectedClipPosition = oneElementBack:attributeValue("AXPosition")
    twoElementsBack = oneElementBack:attributeValue("AXParent") 
    twoElementsBackChildren = twoElementsBack:attributeValue("AXChildren")  

    clipNumber = 0
    loopCount = 0
    for i=1, #twoElementsBackChildren, 1 do
        loopCount = loopCount + 1
        if (twoElementsBackChildren[i]:attributeValue("AXPosition")['x']) == selectedClipPosition['x'] then
            if (twoElementsBackChildren[i]:attributeValue("AXPosition")['y']) == selectedClipPosition['y'] then
                clipNumber = i
            end
        end
    end
    print("clipNumber: " .. tostring(clipNumber))
asmagill commented 8 years ago

If the number exists as an attribute of the element (or as part of an attribute's value -- say in a string for it's title, for example), then yes, but we have to identify which attribute contains the value. If the number indicates its array index in the parent's children attribute, then it's a little harder, but still probably doable... The code I'm working on right now has more functions/methods added for identifying an elements relationship to those around it -- most of them probably won't be used in production code, but I'm including them so that during development the coder can more easily determine which portions of an elements path are static (don't change from query to query and can thus be used to prune the search space quickly each time) and which portions are dynamic (change because of activity within the application, thus requiring a search every time you want to locate the element). i think one or more of these support functions should be able to identify this for you, but its not ready for testing yet... I'll be keeping https://github.com/asmagill/hs._asm.axuielement/ more or less current with what I'm working on, though the documentation (what there is) is currently lagging, so... up to you if you want to take a look now or wait until I've posted some more with examples and suggestions.

latenitefilms commented 8 years ago

Awesome - thanks @asmagill!

Curious... will the changes you make "break" features that are working good in the previous version of the script I downloaded? I'm just wondering if I can safely replace your new axuielement code with the old version without causing issues?

asmagill commented 8 years ago

As of right now, I'm just adding new methods, not removing or changing the old ones (well, a few slight tweaks when I find a better/faster way to do something)... it might be "cleaner" if they eventually go away (if the new ones prove sufficiently better)... then again, having multiple ways of getting at something allows you to choose what works best/easiest for each given problem, so... at present I don't see a need for the existing functions and methods to go away.