Last active
October 28, 2022 04:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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