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
Written by Ben Ripkens
Related protips
8 Responses
There are also some grunt plugins for this:
@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.
Ah, thanks for the clarification. Is your solution necessarily bound to chrome or would it work with other browser too?
@eskimoblood Never tried it with other browsers, but I would assume that the command is Chrome specific. Let me know when you try it.
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
@smartalec43 Thank you, I fixed it!
Cool
@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