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?
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.