Sending keyboard commands directly to Safari (or another solution)

I believe I know the answer to this based on my searching, but was wish to verify since it’s not what I’d hoped for. I am going to present what I need to do first so as not to exclude possibilities I am ignorant of.

What I need to do:

A script to log into an account on a school website and check regularly for available substitute teacher jobs for a friend, then email them the results. After the login I can navigate directly to the postings page via the address I managed to figure out (manually you press a button).

How I’m doing it now:

  1. Navigate to login address with Safari
  2. Wait a few seconds for it to load (and auto-fill login info)
  3. Execute a return keypress and then put Safari into the background
  4. Wait a few seconds for it to log in
  5. Navigate to available jobs address with Safari
  6. Get the text, process it, and email it out if new jobs (this part I have no issues with)

The sub-optimal bit:

Since Safari autofills the login information I have System Events simply press the return key. Because it’s mimicking a literal keypress the potential exists for the scope to be changed, sending it to the wrong place. To minimize this chance, I have the script go to the login page, wait a few seconds for loading, then activate Safari, hit return, and put it in the background again. My friend uses Chrome not Safari, but this doesn’t stop Safari flashing in front for an instant and messing up scope for anything they are doing at the moment it enters the keypress which is a touch annoying (right now it polls every five minutes). To clarify: everything is in the background right now save for the moment where I activate Safari just before the keypress to ensure scope is correct (all the waiting for loading is performed in the background).

My goal:

Is there any way to make it operate completely in the background? Sending key commands directly to Safari rather than mimicking a system wide keypress was my initial idea but that doesn’t seem to be possible based on my searching.

Thanks very much for any suggestions. Apologies if this is a noob request and I’ve missed the obvious.


you could use cURL to access the website directly without any browser by providing the credentials in the URL or the respective switches

Thanks for the suggestion; I’ll check it out. Also looking into an approach using Python.

Good news is that this preliminary version is already helping my friend get a lot more sub work.

Hi Maxim,

With javascript, you can wait intil the page is loaded.


You should be able to do the whole thing in AppleScript without making Safari frontmost. You can use Javascript to fill the username/password and click the submit/login button. I think if you can do it using cURL or with a Python script, that would be better as you can bypass the need to script a browser. Here’s an example script I used a while ago for automating a login to a frequently used site:

set javaScriptArray to {("document.forms[1].username.value ='" & "myUsername" & "'") as string, ("document.forms[1].password.value ='" & "myPassword" & "'") as string, ("document.forms[0]") as string}

tell application "Safari"
	open location ("") -- LOAD URL
	set pageLoaded to "incomplete"
	delay 1
	repeat until pageLoaded is "complete"
		set pageLoaded to (do JavaScript "document.readyState" in document 1) as text
		delay 0.5
	end repeat
	repeat with aScript in javaScriptArray
		do JavaScript aScript in document 1
		delay 1
	end repeat
end tell

As long as you don’t tell Safari to “activate” then Safari should stay in the background.

Hi x74353,

Nice. What happens if the page doesn’t load? does it time out?


Excellent! Learning Javascript is on my todo list anyway. I’ll try it out. Thanks very much for all the responses.

Wound up going with Python, using the Mechanize module. Works great and totally in the background. Thanks again.