Custom URL Protocols: The Secret to Opening Desktop Apps from the Web
Learn how to use Custom URL Protocols to seamlessly connect your web and desktop applications, enhancing user experience and functionality
Ever wondered how websites can open desktop applications like Notion, Steam, or others directly from the browser? The magic lies in Custom URL Protocols. This method is widely used by popular apps to provide seamless transitions between web and desktop environments, ensuring a smooth user experience. In this article, I’ll walk you through how Custom URL Protocols work and show you how to set one up for your own application using Windows Registry.
What Are Custom URL Protocols?
A Custom URL Protocol is a unique identifier registered in the system’s registry. When a web browser encounters a link starting with a registered protocol (e.g., notion://
), it can invoke a dialog to open the specified desktop application, if installed. This approach is ideal for launching desktop applications directly from web pages, enhancing user interaction across platforms.
Here are a few examples of commonly registered URL protocols:
Notion:
notion://
Calculator:
ms-calculator://
Notepad:
ms-notepad://
These protocols are recognized by Windows (and other operating systems) and can be launched by simply clicking a corresponding link in the browser. Now, let's dive into the process of creating your own Custom URL Protocol.
Registering a Custom URL Protocol in Windows
To register a Custom URL Protocol in Windows, we’ll work with the Windows Registry. Here’s a step-by-step guide to creating your own protocol.
Steps to Register Your Custom URL Protocol
Open the Registry Editor
- Press
Win + R
, typeregedit
, and press Enter to open the Windows Registry Editor.
- Press
Navigate to HKEY_CLASSES_ROOT
- In the Registry Editor, go to
HKEY_CLASSES_ROOT
. This location stores all the registered protocols and file associations.
- In the Registry Editor, go to
Create a New Key for Your Protocol
- Under
HKEY_CLASSES_ROOT
, create a new key with the name of your protocol. For this example, let’s call itmy-custprotocol
.
- Under
Add a
URL Protocol
String Value- Inside the
my-custprotocol
key, create a newString Value
namedURL Protocol
. This value doesn’t require any specific data; simply leave it blank. Adding this key informs Windows that this entry is a protocol handler.
- Inside the
Define the Launch Command
Inside the
my-custprotocol
key, create a subkey namedshell
.Under
shell
, create another subkey namedopen
.Under
open
, create a final subkey calledcommand
.Set the
(Default)
value ofcommand
to the path of the executable you want to open, with the following format:"path\to\your\application.exe" "%1"
For example, if your application is located at
D:\TestApp\CustomProtocol.Tester.exe
, you would enter:"D:\TestApp\CustomProtocol.Tester.exe" "%1"
The %1
argument allows the protocol to pass any query string data from the browser to your application, enabling custom handling based on the URL parameters.
Now, when you or any user navigates to my-custprotocol://example
, the browser will prompt to open your application, passing along any query strings as arguments.
Handling URL Arguments in Your Application
In most frameworks, such as Electron, WPF, Tkinter, or JavaFX, you can access command-line arguments in the application startup process. You can then parse the %1
argument to extract parameters from the query string, allowing you to implement custom logic based on the URL.
For instance, if you navigate to my-custprotocol://open?doc=123
, the query doc=123
will be passed to your application, and you could handle this in your app to open a specific document.
Automating the Registry Key Creation
Adding a Custom URL Protocol manually can be tedious, especially if you want this protocol to register on each installation. The registration process can be automated using scripts in various programming languages, such as PowerShell, C#, or Python.
Here's a simple PowerShell script to automate the registry addition for my-custprotocol
:
$protocolName = "my-custprotocol"
$exePath = "D:\TestApp\CustomProtocol.Tester.exe"
New-Item -Path "HKCR:\$protocolName" | Out-Null
New-ItemProperty -Path "HKCR:\$protocolName" -Name "URL Protocol" -Value "" | Out-Null
New-Item -Path "HKCR:\$protocolName\shell" | Out-Null
New-Item -Path "HKCR:\$protocolName\shell\open" | Out-Null
New-Item -Path "HKCR:\$protocolName\shell\open\command" | Out-Null
Set-ItemProperty -Path "HKCR:\$protocolName\shell\open\command" -Name "(Default)" -Value "`"$exePath`" `"%1`""
Run this script as part of your application installation process to register the protocol automatically.
Conclusion
Custom URL Protocols offer a powerful way to bridge the web and desktop application worlds, creating a smooth and integrated experience for users. By following these steps, you can create and register your own Custom URL Protocol, enabling you to launch your desktop applications directly from the web. With the ability to pass arguments through URL query strings, this approach can add significant functionality to your application, making it highly responsive to user actions initiated from a browser.
So, the next time you need to invoke an app from the web, remember that setting up a Custom URL Protocol might be the key!