Skip to content

Instantly share code, notes, and snippets.

@mikaello
Last active Oct 14, 2019
Embed
What would you like to do?
Pipe JS from a file in terminal to be executed in Chrome via AppleScript
(* Save this script as (e.g.) jsChrome.scpt and run it as follows
in a terminal:
osascript jsChrome.scpt someJSFile.js
The content of someJSFile.js will now be run in the active tab of
what is considered your "window 1" of a running Chrome instance.
If this is your first time running JavaScript in Google Chrome from
AppleScript, you will need to enable this in
View -> Developer -> Enable JavaScript from Apple events
Combinate this with e.g. nodemon to get JS run in a Chrome tab on
file save.
*)
on run jsfile
tell application "Finder"
set current_path to container of (path to me) as alias
set p to POSIX path of current_path
end tell
tell application "google chrome"
tell active tab of window 1
execute javascript (read (p & jsfile))
end tell
end tell
return "Evaluated content of " & p & jsfile
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment