You said, "For some reason other people got this to work", yet you get "Syntax Error Expected end of line, etc. but found identifier.", so obviously if you cannot even get it to compile without error in Script Editor, then there is absolutely no way anyone got that code, as is, to work in Google Chrome!
- The
do JavaScript
syntax is for Safari, not Google Chrome. For Google Chrome it's execute javascript
, however changing that doesn't fix your code. It then errors out "Syntax Error Expected end of line, etc. but found class name." and highlights tab
.
- If you look in Google Chrome's AppleScript Dictionary, it does not support
current tab
, it's active tab
, and fixing that doesn't fix your code. It then errors out with 'AppleScript Error Google Chrome got an error: Can’t make application "Google Chrome" into type specifier.' and highlights execute javascript theScript in active tab of front window
.
So, what's needed next to get it to compile without erring out? The following reworked code has the changes necessary from your original code to compile without error.
tell application "Google Chrome"
open location "https://www.randomwebsite.com"
set theScript to "document.getElementById('term_input_id').value = 'Spring 2015';"
execute active tab of front window javascript theScript
end tell
However, just because it can compile without error doesn't necessarily mean it will work without issues!
When the open location "https://www.randomwebsite.com"
command runs, the lines of code following it can execute without regard for the target web page to have finished loading and therefore the script can fail.
You need to add appropriate code for it to wait for the open location ...
command to complete before proceeding with the rest of the script.
Searching the Internet, you'll find various ways to wait for the page to have finished loading, however one given way that works with this site may not work with that site. So you'll need to test what works with the target web site.
One of the generic ways that should work with Google Chrome is:
repeat until (loading of active tab of front window is false)
delay 0.2 -- # The value of the 'delay' command may be adjusted as appropriate.
end repeat
So, your reworked code now look like:
tell application "Google Chrome"
open location "https://www.randomwebsite.com"
repeat until (loading of active tab of front window is false)
delay 0.2
end repeat
set theScript to "document.getElementById('term_input_id').value = 'Spring 2015';"
execute active tab of front window javascript theScript
end tell
This of course doesn't mean that everything is going to work without issues and the onus is upon you to add appropriate error checking and handling as/where needed.