r/applescript • • 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?

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/l008com Mar 29 '25

So, your post answers this but let me just reiterate it directly...

So theres no way to tie an applescript to a specific window with some kind of unique ID? Using "document 1" means that if 10 of these scripts run at the same time, they're going to significantly interfere with each other. But if I could tie the script to the window it creates, and then send the javascript to that specific window, then i could run tons of them at the same time and not have any problems.

1

u/HelloImSteven Mar 31 '25 edited Mar 31 '25

Correct, AFAIK it's not feasible in AppleScript alone, at least not 100% reliably. Windows do have an id property, but you can't create a window directly in Safari ('just because'). So you'd encounter issues when trying to get the window with the latest document, as any windows opening to an empty page can't be distinguished by the document alone.

If you can ensure there's always a delay of ~0.5s between each script, then you can check if a current window exists whose document has a missing URL, if so delay until the URL changes to something else, then create the window for the current script.

The problem with that approach is if the script before gets stuck before loading a URL, how does the current script know when to resume? Maybe have a timeout of 10s before taking over the current window? Might not work if your URLs have vastly different loading times...

If possible, the easiest approach would be to use tabs of an existing window, since that removes all ambiguity in window target and allows you easily address tabs by URL (assuming they're unique — and if you don't need the script to close tabs during execution, you can just use the object returned from make new tab).

You could also explore having a parent script that controls execution. Then you could assign an index to each script and have it control the window at that index. That's probably the solution I'd go with if I needed to guarantee safe parallelism.