Skip to content

Instantly share code, notes, and snippets.

@nikcub
Created October 31, 2011 03:05
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nikcub/1326820 to your computer and use it in GitHub Desktop.
Chrome Refresh
#!/bin/bash
# Chrome Refresh
#
# nik cubrilovic - nikcub.appspot.com
#
# Simple applescript browser reloader for Google Chrome. It will either open a
# new tab with the url passed in as an argument or refresh an existing tab.
#
# Link this up with watchr to auto-refresh browser windows when you save files
# or bind it in vim, textmate etc.
#
# example install:
#
# [nik@nikcub ~] $ echo $PATH
# /Users/nik/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
# [nik@nikcub ~] $ cd ~/bin/
# [nik@nikcub ~/bin] $ ln -s ~/Projects/applescript/chrome-refresh.sh cr
# [nik@nikcub ~/bin] $ cr localhost:3333/admin/posts
# 13:53:35 Opening http://localhost:3333/admin/posts
#
# link to watchr:
#
# [nik@nikcub ~/bin] $ watchr -e "watch( '\w+/.*\.py' ) { system('cr localhost:3333') }"
# (edit a file and save..)
# 14:00:23 Opening http://localhost:3333
#
# watchr is easy to install:
#
# [nik@nikcub ~] $ sudo gem install watchr
#
# 2-clause BSD license all this, do what you want with it.
#
URL=$1
TS=$(date +"%H:%M:%S")
if [ "$URL" = "" ]; then
URL="http://localhost:3333/"
fi
if [ "${URL:0:7}" != 'http://' -a "${URL:0:8}" != 'https://' ]; then
# set as https here if req
URL="http://${URL}"
fi
echo "$TS Opening $URL"
/usr/bin/osascript > /dev/null <<__ASCPT__
tell application "Google Chrome Canary"
activate
set theUrl to "${URL}"
if (count every window) = 0 then
make new window
end if
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if theTab's URL = theUrl then
set found to true
exit
end if
end repeat
if found then
exit repeat
end if
end repeat
if found then
tell theTab to reload
set theWindow's active tab index to theTabIndex
set index of theWindow to 1
else
tell window 1 to make new tab with properties {URL:theUrl}
end if
end tell
__ASCPT__
@a1ee9b
Copy link

a1ee9b commented Aug 29, 2013

you can use

if theTab's URL starts with theUrl then
if you only want to specify the host and don't want to set the path.

For those using Grunt, you can run this script with the grunt module grunt-exec (https://github.com/jharding/grunt-exec).

I put the script in the same folder where the Gruntfile.js is located and used the following configuration:

exec: {
  reload: {
    command: './reload.sh <%= conf.host %>',
    stdout: true,
    stderr: true
  }
}

If you want your editor to stay the active window, you might want to remove line 52 activate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment