Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AppleScript access browser tabs and execute javascript in them?

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

Anyone have ideas?

like image 313
Allan Avatar asked Feb 27 '11 20:02

Allan


People also ask

How do I run JavaScript in AppleScript?

For other browsers, check the functions at File -> Open Dictionary on AppleScript editor. Note: As of August 2018, you may need to navigate to View → Developer → Allow JavaScript from Apple Events for execute javascript to work.

How do I run a script in inspect element?

Right click on the page and choose 'inspect element'. In the screen that opens now (the developer tools), clicking the second icon from the left @ the bottom of it opens a console, where you can type javascript. The console is linked to the current page.


2 Answers

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.

like image 74
Fábio Perez Avatar answered Oct 04 '22 19:10

Fábio Perez


The function below runs JavaScript in each tab of the frontmost window in Chrome and concatenates the output. To run JavaScript in in all windows, replace window 1 with windows.

xjss(){ osascript -e'on run {a}
    set o to ""
    tell app "Google Chrome" to repeat with t in (get tabs of window 1)
        tell t to set o to o & (execute JavaScript a) & linefeed
    end
end' -- "$1"; }

This runs JavaScript in only the frontmost tab:

xjs(){ osascript -e'on run {a}
    tell app "Google Chrome" to tell active tab of window 1 to execute JavaScript a
end' -- "$1"; }

Here are similar functions for Safari:

sjss(){ osascript -e'on run {a}
    set o to ""
    tell app "Safari" to repeat with t in (get tabs of window 1)
        tell t to set o to o & (do JavaScript a) & linefeed
    end
end' -- "$1"; }

sjs(){ osascript -e'on run {a}
    tell app "Safari" to tell document 1 to do JavaScript a
end' -- "$1"; }

Since some version of Chrome released in 2018, an error like this is shown by default when running an execute JavaScript command:

78:98: execution error: Google Chrome got an error: Executing JavaScript through AppleScript is turned off. To turn it on, from the menu bar, go to View > Developer > Allow JavaScript from Apple Events. For more information: https://support.google.com/chrome/?p=applescript (12)

Safari has a similar preference in "Develop > Allow JavaScript from Apple Events" which was turned off by default in macOS version 10.11.5 (released in 2016-05-16).

like image 37
nisetama Avatar answered Oct 04 '22 18:10

nisetama