Let Us Open URLs in a Specific Browser Profile

Most browsers have the ability to launch different browser profiles. Each profile can come with a different theme and a different set of website logins (cookies, application state, etc). This can be helpful if you want to segregate browsing behavior. For example, I have different browser profiles set up for my personal email, my "consulting" email, the nonprofit I volunteer with.

Browser dialog that lets you select a profile

Along with this, command line tools frequently open URL's for various purposes. Docusaurus launches http://localhost:3000 so you can view documentation content in your browser. Lots of different tools open browser profiles to complete authentication workflows - for example, Tailscale starts a web server on your local computer, asks you to log in via the browser, and then redirects to the local web server with a valid token.

When you have multiple profiles, it's frustrating when a URL opens in the "wrong" profile, because this means it's frequently opening in a window where you're not logged in. When this happens, web services typically redirect you to a login page, and remove the information about the URL you were trying to visit (or store it in a cookie). This means if you try and copy and paste the URL to the "right" profile, you frequently lose track of the initial URL you wanted to visit! Very annoying.

Fortunately it turns out there is a solution to this! The way most command line tools open URL's on Macs is by simply invoking open (/usr/bin/open), followed by the URL.

$ open https://example.com/path/to/page

But you can pass additional arguments to the application with --args, and Chrome/Chromium support arguments that let you select the right profile.

open -na "Google Chrome" --args --profile-directory="Profile 4" --new-tab "https://example.com/path/to/page"

That will always open URL's in "Profile 4", whatever you have that configured to.

How do you find which profile is which? The simplest way is to open chrome://version in your URL, and then look at the last bit of "Profile Path" in the page that opens there - it should be 'Default' or 'Profile N'.

Screenshot of the chrome version page, with the profile directory highlighted

On Firefox, you provide the name of the profile from about:profiles, e.g.:

open -n -a "Firefox" --args -no-remote -P 'kevin@burke.services' -new-tab 'https://example.com'

Example configuration

If you operate a command line tool (or an application that has access to /usr/bin/open!), particularly one that opens URL's for authentication, please consider implementing support for opening URL's in specific browser profiles. Example configuration options might be:

# The name of the application you would like to use to open URL's. Defaults to
# "Google Chrome"
browser_application = "Google Chrome"

# The specific browser profile to open URL's in.
browser_profile = "Profile 5"

And then your CLI tool would implement logic similar to this, in the language of your choice, instead of just shelling out to open.

cmd = ['open', url]
if browser_application and browser_profile:
    if any(name in browser_application.lower() for name in ['chrome', 'chromium', 'brave', 'edge']):
        cmd = ['open', '-na', browser_application, '--args',
               f'--profile-directory={browser_profile}', '--new-tab', url]
    elif 'firefox' in browser_application.lower():
        cmd = ['open', '-n', '-a', browser_application, '--args',
               '-no-remote', '-P', browser_profile, '-new-tab', url]

subprocess.run(cmd, check=True)

Please consider implementing this change in your CLI tool! It would really help improve usability for CLI based tools. With sufficient interest, maybe we could create an RFC for both the arguments browsers should accept, and for environment variables that can be read by CLI's for opening URL's in a given browser.

Liked what you read? I am available for hire.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments are heavily moderated.