Can AppleScript access browser tabs and execute javascript in them?

Allan picture Allan · Feb 27, 2011 · Viewed 13.5k times · Source

I'm curious if AppleScript can access each specific tab in a browser and execute some javascript in them.

Anyone have ideas?

Answer

Fábio Perez picture Fábio Perez · Feb 28, 2011

For Google Chrome, use execute from AppleScript Chromium Suite:

execute v : Execute a piece of javascript.

execute specifier : The tab to execute the command in.

javascript text : The javascript code to execute.

Example:

tell application "Google Chrome"
    execute front window's active tab javascript "alert('example');"
end tell

Can be done in Safari either:

tell application "Safari" to do JavaScript "alert('example')" in document 1

For other browsers, check the functions at File -> Open Dictionary on AppleScript editor.