Home Interests MacOS

How to Open URLs in Google Chrome from the macOS Terminal

howchoo
howchoo   (467)
September 16, 2023
9 minutes

Share

Interests
Posted in these interests:
bash • 2 guides
chrome • 7 guides
macos • 20 guides

If you spend any amount of time working on the command line in macOS, you’ll realize it’s much more capable than just finding your IP address. It can do plenty of useful tasks, like running Python scripts and even automating the process of opening URLs in your favorite browser. In this guide, we’ll learn how to open Chrome from the command line (or a shell script) on macOS.

In the following steps, we’ll learn a few variations of the open command to learn how to:

  • Bring the Chrome window into focus
  • Open a specific URL in Chrome
  • Open a URL in a specific Chrome profile


1 – Use the `open -a` command

The open command accepts a -a option that allows us to specify which application we want to open.

So if you just want to open Google Chrome from the command line, it’s as simple as this:

open -a "Google Chrome"

This will do nothing more than bring the Google Chrome window into focus.

2 – Open a URL in Chrome

To open a specific URL in Chrome, simply pass the URL as the first argument of the command, like this:

open -a "Google Chrome" https://howchoo.com

This will open your favorite website in your favorite browser.

3 – Open a URL in Chrome using a specific profile

If you’re like me and use multiple Chrome profiles, you’ll want to be able to specify which profile to use when opening the URL. For this, we’ll have to move away from the convenient open command.

Before we get to the command, we’ll need to figure out the correct profile directory to use. Sadly, this will not be the same value as your profile name. To get your profile directory, open Chrome in the profile you want to use and navigate to chrome://version/. Find the item labeled Profile Path, and copy only the last part of the path. In my case, it’s Profile 5.

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome https://howchoo.com --profile-directory=Profile\ 5

I know its not realistic to type this every time you want to open a URL. You could bake this into a script or see the next step where I’ll cover using aliases and bash functions to make this less painful.

4 – Helpful aliases

If you’re going to open URLs frequently, there are a few ways to dramatically increase efficiency. The easiest way is to use bash aliases. An alias allows you to basically create a shortcut for longer commands.

Alias for opening URLs in Chrome

Here are a few examples (you can add them to your ~/.bash_profile).

alias gc="open -a Google\ Chrome"

Or if you’d like to create an alias to the command that let’s you specify a profile, you could do the following:

alias gc="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory=Profile\ 5"

Anytime you edit your bash_profile, you need to run source ~/.bash_profile or open a new shell to see the changes take effect.

With this, you can run:

gc  # Open or focus on the Chrome window
gc https://howchoo.com  # Open the url in Chrome

Similarly, the gc {url} command will open the url in Chrome, but it will use the specified profile every time.

Alias that accepts a URL from STDIN

In some cases, you might want to accept a URL from STDIN. The open command handles stdin the way we’d like, so if you’re using the first alias, you don’t need to do anything.

If you’re using the alias for Google Chrome, one easy way is to create a second alias (using xargs) like this:

alias gcx="xargs /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory=Profile\ 5"

This will allow you to do something like this:

echo "https://howchoo.com" | gcx

If you’ve got a better way of doing this, let me know in the comments below 🙂

NEXT UP

How to Enable the “Popping” Sound When Adjusting the Volume on Your Mac

howchoo
howchoo   (467)
December 13, 2023

Starting with MacOS Sierra and High Sierra, your Mac will no longer play a “pop” sound when you adjust your volume. If you prefer to have this sound when you adjust volume up and down (as I do), this guide will teach you how to reenable it. 1 – Open Sound System Preferences Navigate to System

Continue Reading

howchoo

 467 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.

Explore