I have a program which generates some html documentation. At the end of the script, I use the open command to automatically view the page in the browser.

generate-document > index.html
open index.html

However, after iterating on the code, I end up with numerous obsolete copies of index.html open in my browser.

Is there a way (either from the command line, or within the browser) to say something like "if index.html is already being viewed refresh it, otherwise load it."?

generate-document > index.html
open-or-reload index.html

Safari or Chrome specific methods are fine.

  • 2
    If the the target tab and Safari window has focus and is the frontmost Safari window, then use the following command line to refresh it: osascript -e "tell application \"Safari\" to set sameURL to URL of current tab of front window" -e "tell application \"Safari\" to set URL of current tab of front window to sameURL" May 28, 2015 at 17:01
  • Nice. Add this as an answer and I'll upvote it. May 29, 2015 at 3:23

2 Answers 2


The following command line can be added to your script and it will refresh the Tab that has focus in the frontmost Safari window, (even if the frontmost window only has a single page loaded):

osascript -e \
'tell application "Safari" to set URL of current tab of front window to "file:///foo.html"'
  • 150:157: execution error: The variable sameURL is not defined. (-2753) May 29, 2015 at 4:52
  • @Mark Harrison, I just copied and pasted the command line, as I had it on a single line, in my answer and it works without error, is that what you tried? May 29, 2015 at 5:01
  • yes, with and without wrapping, it seemed to get the error on sameURL. After experimenting, I got the second -e to work and made a suggested edit. If you think that is fine, you can approve it. But it's working for me, so I'm accepting your answer. Many thanks!! May 29, 2015 at 5:09
  • @Mark Harrison, I tried it both ways and it worked without error and without seeing the actual command line that caused the error I can't say why however since it's working for you now I guess it's moot. May 29, 2015 at 5:28

Check out LiveReload. It is an app that watches your HTML files and can automatically for reload the pages in your browser. LiveReload can also do other things like automatically recompile SASS files to CSS and then reload the browser.

In addition to the app at the link above, LiveReload is available on GitHub under a "Open Community Indie Software License" which is not quite open source, but does include a dead man clause to convert to MIT if no new binary updates come out for two years. https://github.com/livereload/LiveReload

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .