Last Updated: September 09, 2019
·
5.623K
· bripkens

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

8 Responses
Add your response

over 1 year ago ·

@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 ·

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

over 1 year ago ·

@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 ·

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 ·

@smartalec43 Thank you, I fixed it!

over 1 year ago ·

Cool

over 1 year ago ·

@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 ·