1
votes

I'm trying to come up with an apple script I can run in order to fill out some web forms I do every day (JIRA ticket forms).

I've created this JS script which is working when I run it on Chrome's console:

document.getElementById('summary').value="Inserted text"

So now I create my applescript in order to be able to externally execute it but I always get "Syntax Error . Expected end of line, etc. but found unknown token."

This is the code:

tell application "Google Chrome"

execute javascript document.getElementById('summary').value="Inserted text" in document 1   

end tell

I know tell application works, but I don't understand how to solve the Syntax Error and make the applescript work

2

2 Answers

1
votes

The in document 1 syntax is for Safari.

With Google Chrome you should to talk to the active tab of the target window, e.g.:

tell application "Google Chrome"
    tell active tab of window 1
        execute javascript "document.getElementById('summary').value = 'Inserted text';"
    end tell
end tell

Or substitute the window number with its name.

1
votes

This works for me using the latest version of Sierra

tell application "Google Chrome"
    execute front window's active tab javascript "document.getElementById('summary').value = 'Inserted text';"
end tell

OR - Compacted down to one line of code only

tell application "Google Chrome" to execute front window's active tab javascript "document.getElementById('summary').value = 'Inserted text';"

The reason I chose this code is because of this following example..

In Google Chrome I started out with one window, then one window with 4 tabs. Next, Split up those tabs into 4 separate windows. After this, I selected the bottom right window to make that the “active window”. Thinking that, that bottom right window, being active, would now be “window 1”… I would be wrong (according to the results of this following script)

enter image description here

If I ran the script in “window 1” it would have run on the upper left window in the browser, which was window 1 when I only had one window with one tab. It retained its initial value. The problem is that is not the window I wanted to run my script on

enter image description here

So apparently window 1 is not necessarily the front window