Search code examples
macosapplescriptmouseeventcoordinates

Applescript - get the coordinates of an element


I need to get the coordinates {X,Y} of an element in order to generate a click event on it (I can't do that by using "click" since System Events has problem doing that and returns: "missing value"). I'll use these coordinates to pass them to a command line tool that simulates a mouse click.

How can I achieve that?


Solution

  • Can you get the properties of the ui element? I think you can even if you can't actually click it. One of the properties should be "position" and another will be "size". Those 2 things should help you find the screen coordinates so you can click the element.

    For example try this and look at the properties...

    tell application "System Events"
        tell process "Safari"
            properties of UI element 1 of window 1
        end tell
    end tell
    

    So if I wanted to know the coordinates of the middle of that element so you can click it this would give you the x and y coordinates of where to click...

    tell application "System Events"
        tell process "Safari"
            tell UI element 1 of window 1
                set p to position
                set s to size
            end tell
        end tell
    end tell
    
    set xCoordinate to (item 1 of p) + (item 1 of s) / 2
    set yCoordinate to (item 2 of p) + (item 2 of s) / 2
    return {p, s, xCoordinate, yCoordinate}