Open Chrome Programmatically macOS

Table of contents

No heading

No headings in the article.

Can be useful if you want to automate some simple workflows, I use it for instance as part of building a docker image and then opening the homepage of my app.

Open in incognito mode

#!/bin/bash

URL=$"https://twitter.com"

[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
if open -Ra "Google Chrome" --args --incognito "$URL"; then
  echo "opening webpage now"
  open -na "Google Chrome" --args --incognito "$URL"
else
  echo "no system browser installed"
fi

Open in regular mode

#!/bin/bash

URL=$"https://twitter.com"

[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
if open -Ra "Google Chrome" --args "$URL"; then
  echo "opening webpage now"
  open -na "Google Chrome" --args "$URL"
else
  echo "no system browser installed"
fi

Explain shell

exec: execute commands and open, close, or copy file descriptors
xdg: opens a file or URL in the user's preferred application
gnome-open: open files and URLs using the GNOME file handlers
open: check 'man open' in your terminal