keuhda
Last Updated: February 25, 2016
·
5.33K
· bripkens
Avatar2 square

Simple Grunt Task to reload Chrome on OS X

Automatically reload your browser on every save with this custom reload task and the watch task.

1) Add the new task

grunt.registerTask("reload", "reload Chrome on OS X",
    function() {
  require("child_process").exec("osascript " +
      "-e 'tell application \"Google Chrome\" " +
        "to tell the active tab of its first window' " +
      "-e 'reload' " +
      "-e 'end tell'");
});

2) Configure your watch Task

// ...
watch: {
  files: ["src/**/*"],
  tasks: "less concat reload"
}
// ...

3) Use it

grunt watch
Say Thanks
Respond

8 Responses
Add your response

2934
7831f9c652845c34091da263b689cc25
over 1 year ago ·
2936
Avatar2 square

@eskimoblood Right, there are a few plug-ins which do it in a similar manner. Both solve this problem differently:

grunt-macreload changes the windows focus to the browsers, refreshes it and then refocuses your editor. I do not want my Editor to lose focus so instead I am using a Chrome specific refresh command.

grunt-reload uses WebSockets and some code injection. This might be OK for you, but I want to see my page and only its resources during development just like an end-users would see it.

over 1 year ago ·
2937
7831f9c652845c34091da263b689cc25

Ah, thanks for the clarification. Is your solution necessarily bound to chrome or would it work with other browser too?

over 1 year ago ·
2939
Avatar2 square

@eskimoblood Never tried it with other browsers, but I would assume that the command is Chrome specific. Let me know when you try it.

over 1 year ago ·
4402

Thanks ben! This is much simpler and cleaner than installing one of the other grunt reload plugins. Just a note, there's a missing "+" on the 6th line of the first code block

over 1 year ago ·
4408
Avatar2 square

@smartalec43 Thank you, I fixed it!

over 1 year ago ·
8070
38b86511b7a732db7a89d971c14c4aa0

Cool

over 1 year ago ·
9054
5b47db0ad05c288f5983599dce346aa5

@eskimoblood Its just AppleScript. Can easily be made to work with different browsers.. For Safari:

tell application "Safari" to tell the current tab of its first window
do JavaScript "window.location.reload()"
end tell

over 1 year ago ·
Awesome Job

E20ce6a2 ca1f 11e7 82be 7f617c8310c9
SENIOR DEVELOPER (m/w)
·
Berlin, Köln, Hamburg, München
·
Full Time