Skip to content

Instantly share code, notes, and snippets.

@lfender6445
Forked from nikcub/chrome.refresh.sh
Last active March 9, 2020 15:01
Show Gist options
  • Save lfender6445/5062078 to your computer and use it in GitHub Desktop.
Save lfender6445/5062078 to your computer and use it in GitHub Desktop.
#!/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, refresh an existing tab, or open
# a file from your current working directory.
#
# Link this up with watchr to auto-refresh browser windows when you save files
# or bind it in vim, textmate etc.
#
# Example install:
#
# $ echo $PATH
# /Users/nik/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
# $ cd ~/bin/
# $ ln -s ~/Projects/applescript/chrome-refresh.sh cr
# $ cr localhost:3333/admin/posts
# 13:53:35 Opening http://localhost:3333/admin/posts
#
# link to watchr:
#
# $ 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:
#
# $ 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/"
fi
if [ -e $URL ]; then
open /Applications/Google\ Chrome.app/ "$URL"
echo "$TS Opening file $URL"
exit 0
elif [[ "${URL:0:7}" != 'http://' && "${URL:0:8}" != 'https://' ]]; then
URL="http://${URL}"
echo "$TS Opening url $URL"
fi
/usr/bin/osascript > /dev/null <<__ASCPT__
tell application "Google Chrome"
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__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment