Skip to content

Instantly share code, notes, and snippets.

@pysysops
Last active October 14, 2019 07:44
Show Gist options
  • Save pysysops/667954dc00b1901352dbd6c3974bdc6e to your computer and use it in GitHub Desktop.
Save pysysops/667954dc00b1901352dbd6c3974bdc6e to your computer and use it in GitHub Desktop.
Override the open command on Mac to open isolated, minimal browsers in incognito and cleanup
#!/usr/bin/env bash
# Add $HOME/.bin to the beginning of your PATH
# Save this file in $HOME/.bin/open and make it executable
if ! echo "$@" | grep -q http; then
/usr/bin/open $@
exit $?
fi
BASE_TEMP_DIR=/tmp
TEMP_DIR=$(mktemp -d $BASE_TEMP_DIR/google-chome.XXXXXXX)
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--user-data-dir=$TEMP_DIR \
--incognito \
--disable-default-apps \
--no-first-run $@ >/dev/null 2>&1 &
APP_PID=$!
while true; do
if ! kill -s 0 $APP_PID 2>/dev/null; then
rm -rf $TEMP_DIR
break
fi
sleep 10
done &
@pysysops
Copy link
Author

pysysops commented Oct 13, 2019

If, like me, you use various tools such as aws-vault to login to different AWS accounts and want to open AWS console windows for multiple accounts at once you'll soon discover that there are various blockers. Incognito windows aren't suitable as they share a cookie store across all Windows meaning - no multiple account logins.

This script and:

# Add $HOME/.bin to the beginning of your PATH
# Save this file in $HOME/.bin/open and make it executable

Will give you a new open command. It creates a temporary Chrome user data directory, starts Chrome in incognito mode with all default apps disabled (using the temporary data directory) and runs a loop in the background which cleans up the temporary profile when the Chrome process goes away.

The while loop probably isn't really needed as /tmp gets cleaned up by the OS but I don't want anything hanging round when I no longer need it.

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