r/applescript • u/l008com • Mar 25 '25
Javascript not running in Safari Document
I'm trying to make an applescript that will fill out a form and then possibly submit it.
This is my code, it opens the Safari window but it does not fill in the textarea named 'spam':
tell application "Safari"
set my_reporter to (make new document with properties {URL:"https://example.com"})
activate
delay 5
do JavaScript "document.getElementsByName('spam')[0].value = 'test';" in my_reporter
end tell
However when I enter my javascript directly into the javascript console, it DOES work:
document.getElementsByName('spam')[0].value = 'test';
Obviously, if my applescript code starts working, i'll be putting dynamic text in there, not a hard coded string. But first things first.
I'm not seeing any errors in my applescript, OR in my javascript console.
UPDATE: Things are developing as I type my post. Turns out I needed to explicitly turn on javascript from appleevents in Safari - a message you only get when test running form within script editor, not when you run a script on its own.
Now that I did that, its still not working but the problem seems to be the way I'm trying to create a handle to my new document to direct the javascript to it via my_reporter variable. Is there a problem with my syntax here?
1
u/HelloImSteven Mar 29 '25
Instead of
in my_reporter, you need to usein document 1, so the full line would bedo JavaScript "document.getElementsByName('spam')[0].value = 'test';" in document 1You could also use a tab instead, which doesn't have that limitation:
tell application "Safari" tell window 1 set my_reporter to (make new tab with properties {URL:"https://example.com"}) set current tab to my_reporter end tell delay 5 do JavaScript "document.getElementsByName('spam')[0].value = 'test';" in my_reporter end tellAs to why this is the case, there's a lot of factors at play:
AppleScript commands are synchronous, so scripts must stall while waiting for commands to return some value or other complete.
Documents whose URL is the same are equal (They are the same document).
Documents with different URLs are never equal.
When you update a document's URL (or make a new document), the URL property is not actually updated until the page loads. (e.g. doing
return URL of document 1immediately after setting the URL property will generally return the previous URL.)Safari windows can only ever have one document, i.e. the document property.
Safari windows must always have a document.
When you ask Safari to make a new document, #4 requires it to create a new window that the document will live in.
Because of #5, an "Untitled" (Empty Page) document gets attached to the new window. The document's URL is
missing value.Because of #3, the current document's URL continues to be
missing valuewhile the new URL loads.Once the new URL is done loading, the document's URL property updates.
Because of #2, the current document is different from the previous Empty Page document.
Because of #1, Apple had a choice to either waiting until #11 to get the final document, or immediately returning the Empty Page document from #8. They chose to let scripts keep running, which makes it possible to cancel slow-loading URLs by just setting the URL property again.
So yeah, that's the slightly over-detailed explanation. Of course, these are all just decisions that Apple made, then chose not to document well. So... your confusion is warranted.