Gerald Cormeraie
Gerald Cormeraie

Reputation: 53

Enter value in a webform and push the return key with applescript

I want to enter a value on a webform and then push enter as there is no button to click on the page

I cannot provide any URL as it is a private web tool.

The search box is a simple webform where I enter a value and then push enter to charge the results.

I already wrote the part of the script adding the value in the box, no problem with that. My issue is to find a way for the keystroke return work on the page.

set theValue to "1111111"

to inputByID(theId, theValue)

    tell application "Safari"

        do JavaScript "document.getElementById('" & theId & "').value = '" & theValue & "'" in document 1

    end tell

end inputByID

inputByID("TextField", theValue)

So this works perfectly. I then tried to add a Keystroke return every where I could imagine and I always get the same error message:

Result:
error "Safari got an error: Can’t get keystroke \"
\"." number -1728 from keystroke "
"

I'm not sure if there is anything else to do here. Looking at the Page Source I cannot find any relevant information, that's why I really wanted to go through this road.

Upvotes: 1

Views: 422

Answers (1)

user3439894
user3439894

Reputation: 7555

To use a keystroke in your scenario, two conditions must exist.

  • Safari needs to be frontmost and active.
  • The cursor must be in a text field that can receive input.

The AppleScript code for that would be:

tell application "Safari" to activate
delay 0.5
tell application "System Events" to keystroke return

With System Events, for return, you can also use:

tell application "System Events" to key code 36

Note that the delay commands allow time for the app to be frontmost and active before the keystroke is sent. Adjust as/if necessary.

Upvotes: 1

Related Questions