Skip to content

Instantly share code, notes, and snippets.

@a1ee9b
Forked from nikcub/chrome.refresh.sh
Last active December 23, 2015 02:19
Show Gist options
  • Save a1ee9b/6566308 to your computer and use it in GitHub Desktop.
Save a1ee9b/6566308 to your computer and use it in GitHub Desktop.
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: './chrome.refresh.sh localhost', stdout: true, stderr: true }
#!/bin/bash
# Chrome Refresh
#
# 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.
#
# Use grunt-exec to include it in your build process
#
# 2-clause BSD license
URL=$1
TS=$(date +"%H:%M:%S")
if [ "$URL" = "" ]; then
URL="localhost"
fi
if [ "${URL:0:7}" != 'http://' -a "${URL:0:8}" != 'https://' -a "${URL:0:7}" != 'file://' ]; 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"
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 starts with theUrl then
set found to true
tell theTab to reload
set theWindow's active tab index to theTabIndex
end if
end repeat
if found then
exit repeat
end if
end repeat
if not found then
tell window 1 to make new tab with properties {URL:theUrl}
end if
end tell
__ASCPT__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment