macosautomatorjavascript-automation

How to click UI element using JXA


Please tell me how do I click in point coordinates in application window? I trying to UI automate my application on OSX 10.10 using JXA technology. In documentation I found that it's possible using click at event. By I'am beginner of JXA and cant find how make a call. Code snippet which I tried in Script Editor:

var app = Application('my_application_path')
app.window.click.at('{100,100}')

Thank you for help


Solution

  • You can interact with an application's user interface using the System Events application. Here is a script that clicks at certain coordinates in Safari:

    // Activate Safari, so you will be able to click like a user
    
    Application("Safari").activate()
    
    // Access the Safari process of System Events
    
    var SystemEvents = Application("System Events")
    var Safari = SystemEvents.processes["Safari"]
    
    // Call the click command, sending an array of coordinates [x, y]
    
    Safari.click({ at: [300, 100] })
    

    If you want to click a specific button (or other element of the user interface), it is more appropriate to click that specific element. For example:

    // Click the third button of Safari's first window to minimize it
    
    Safari.windows[0].buttons[2].click()
    

    To learn what user interface elements can be interacted with and how, check out the Processes Suite in System Events' scripting dictionary. To open the dictionary, in Script Editor's menu bar, choose Window > Library, then select System Events in the Library window.