Skip to content

Instantly share code, notes, and snippets.

@kyptin
Last active November 3, 2022 13:47
Show Gist options
  • Save kyptin/86530c28a3b91889e290741cf38fbf85 to your computer and use it in GitHub Desktop.
Save kyptin/86530c28a3b91889e290741cf38fbf85 to your computer and use it in GitHub Desktop.
reload-safari-tab - use osascript to reload a safari tab by url substring - composable with entr for auto-reloading
#!/bin/bash
set -euo pipefail
usage() {
echo "$(basename "$0") - Reload any Safari tabs that contain the given string."
echo "Usage:"
echo " $(basename "$0") STRING"
echo " fd <opts> | entr reload-safari-tab STRING"
}
if [ $# != 1 ]; then
echo "Error: specify exactly one argument. Use -h for help." >&2
exit 1
fi
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
exit 0
fi
# Credit: https://brettterpstra.com/2011/03/07/watch-for-file-changes-and-refresh-your-browser-automatically/
osascript <<OSASCRIPT
tell application "Safari"
set windowList to every window
repeat with aWindow in windowList
set tabList to every tab of aWindow
repeat with atab in tabList
if (URL of atab contains "$1") then
tell atab to do JavaScript "window.location.reload()"
end if
end repeat
end repeat
end tell
OSASCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment