Skip to content

Instantly share code, notes, and snippets.

@mikaello
Last active October 28, 2022 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaello/86bb8720c935b74182b804181a295c12 to your computer and use it in GitHub Desktop.
Save mikaello/86bb8720c935b74182b804181a295c12 to your computer and use it in GitHub Desktop.
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