Implementing custom URL schemes on macOS and Windows

Apologies if this has been covered elsewhere, but I haven’t been able to find anything about it using the forum search tools.

Is it possible to implement custom URL schemes on macOS and Windows with Xojo?

I know that we can do this on iOS, but I’m trying to build something that will work with our desktop app.

of course.
For Windows you add them to registry, probably via installer.
And than you get them as command line parameter.

For MacOS you handle apple Event GURL/GURL which brings URL as default parameter.

See Xojo’s Blog Posts:

[quote=423286:@Jürg Otter]See Xojo’s Blog Posts:

I would suggest that what is missing from the Windows blog post is how an already running app can react to a custom URI triggered from another application. In macOS you get an AppleEvent straight into your running app, whereas for Windows your app gets launched with the relevant info as a command line parameter.

Have I missed how this may be done for a running app?

[quote=423294:@Tim Streater]I would suggest that what is missing from the Windows blog post is how an already running app can react to a custom URI triggered from another application. In macOS you get an AppleEvent straight into your running app, whereas for Windows your app gets launched with the relevant info as a command line parameter.

Have I missed how this may be done for a running app?[/quote]
A second instance of your app will launch. For instance, Feedback uses a mutex to notice that an instance is already running and passes the request to it using an up socket.

[quote=423286:@Jürg Otter]See Xojo’s Blog Posts:

Thanks, Jürg: I knew I’d seen something about this somewhere but couldn’t find it on the forum.

Hmmm. OK, thanks for that warning. A second instance of my app would start messing with the databases used by the first. It will require some re-arrangement to avoid that.

Edit: or, more likely, a helper command-line app. Food for thought.

[quote=423421:@Tim Streater]Hmmm. OK, thanks for that warning. A second instance of my app would start messing with the databases used by the first. It will require some re-arrangement to avoid that.

Edit: or, more likely, a helper command-line app. Food for thought.[/quote]

Here’s a ripped-out slightly modified for ease-of-use version of code from one of our payment services…

For a Mutex, you can add a property mMutex to your App as type Mutex

and in the App.Open Event try something like

[code]mMutex = New Mutex(“MyAppMutex”)

If Not mMutex.TryEnter Then
'We only need one copy running to access the built-in payment server…
//If you need to snatch any commands off the command line you can use the following line…
Dim cmdline as String = System.CommandLine
///set some flags to not interact with the database… and pass info to wherever it needs to be…
Dim httpnew as new HTTPSocket
httpnew.Yield = true
Dim y as String = httpnew.Get("http://127.0.0.1:7777/?" +EncodeURLComponent(cmdline),15)
//Above we pass one-click payment data to the main application from the website or wherever the URI was clicked…the first instance is listening for the request…
mMutex = Nil
//Quit the second copy if needed.
Quit
End If[/code]

To test this on windows… add a socket to listen for incoming information

And to create the URI… here’s a re-usable method for Windows…

[code]Public Sub RegisterURI(URI as String, Name as String)

#if TargetWindows then
Dim reg As RegistryItem

Try
  reg = new RegistryItem("HKEY_CLASSES_ROOT\" + URI, False) 'RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\ORACLE\", FALSE)
Catch e as RegistryAccessErrorException
  reg = new RegistryItem("HKEY_CLASSES_ROOT")
  reg = reg.AddFolder(URI)
  reg.DefaultValue = "URL:" + Name
  reg.Value("URL Protocol") = ""
  
  Dim defaultIcon As RegistryItem = reg.AddFolder("DefaultIcon")
  defaultIcon.DefaultValue = """" + App.ExecutableFile.NativePath + """,1"
  
  Dim commandItem As RegistryItem = reg.AddFolder("shell").AddFolder("open").AddFolder("command")
  commandItem.DefaultValue = """" + App.ExecutableFile.NativePath + """ ""%1"""
Catch f as RegistryAccessErrorException
  exit
End

#Endif

End Sub[/code]

For Mac, to do the above, you will need to modify the application plist.

I rarely see anyone inquire about Linux, but for full cross-platform Custom URI handline, you will need to manually, or use a shell, to do the following

http://archive.is/8C3zb