wxLaunchDefaultBrowser() on macOS doesn't work with file URLs (Issue #25563)

34 views
Skip to first unread message

Will Cosgrove

unread,
Jun 24, 2025, 6:52:01 PM6/24/25
to wx-...@googlegroups.com, Subscribed
willco007 created an issue (wxWidgets/wxWidgets#25563)

Description

When calling wxLaunchDefaultBrowser() with a local file URL on macOS, it fails with a -50 error.

Bug description:

wxLaunchDefaultBrowser() calls wxDoLaunchDefaultBrowser() on macOS which doesn't work with file system urls. Also, wxDoLaunchDefaultBrowser() is leaking

Expected vs observed behaviour:

Expected to work, observed fails with -50 error which is a parameter error.

Patch or snippet allowing to reproduce the problem:

This line Creates a standard URL with a file:/// uri. It will return NULL which then causes LSOpenCFURLRef to fail.

If you're creating a local file URL you need to use CFURLCreateWithFileSystemPath() with the file system path.

Here's an example:

CFStringRef cfPath = CFStringCreateWithCString(NULL, file.GetFullPath().utf8_str(), kCFStringEncodingUTF8);
if ( cfPath )
{
	CFURLRef curl = CFURLCreateWithFileSystemPath(NULL, cfPath, kCFURLPOSIXPathStyle, false);
	if ( curl )
	{
		LSOpenCFURLRef(curl, NULL);
		CFRelease(curl);
	}
	CFRelease(cfPath);
}

To Reproduce:

Call wxLaunchDefaultBrowser() with a local file path URL.

Platform and version information

  • wxWidgets version you use: 3.2.7
  • wxWidgets port you use: macOS
  • OS and its version: macOS 15.5


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/25563@github.com>

VZ

unread,
Jun 25, 2025, 7:15:35 PM6/25/25
to wx-...@googlegroups.com, Subscribed
vadz left a comment (wxWidgets/wxWidgets#25563)

It looks like we could just do this:

diff --git a/src/osx/utils_osx.cpp b/src/osx/utils_osx.cpp
index 26b5c36696..5830546ca6 100644
--- a/src/osx/utils_osx.cpp
+++ b/src/osx/utils_osx.cpp
@@ -84,6 +84,13 @@ bool wxLaunchDefaultApplication(const wxString& document, int flags)
 
 bool wxDoLaunchDefaultBrowser(const wxLaunchBrowserParams& params)
 {
+    if ( !params.path.empty() )
+    {
+        // We need to use a separate URL-creation function for local paths, but
+        // luckily we already have it.
+        return wxLaunchDefaultApplication(params.path);
+    }
+
     wxCFRef< CFURLRef > curl( CFURLCreateWithString( kCFAllocatorDefault,
                               wxCFStringRef( params.url ), nullptr ) );
     OSStatus err = LSOpenCFURLRef( curl , nullptr );

Does it work for you?


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/25563/3006498637@github.com>

Will Cosgrove

unread,
Jun 27, 2025, 1:40:59 PM6/27/25
to wx-...@googlegroups.com, Subscribed
willco007 left a comment (wxWidgets/wxWidgets#25563)

This works, however, it points out that wxLaunchDefaultBrowser() doesn't actually launch the default browser, it is simply opening the default application for the file type.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/25563/3013899766@github.com>

Will Cosgrove

unread,
Jun 27, 2025, 2:14:16 PM6/27/25
to wx-...@googlegroups.com, Subscribed
willco007 left a comment (wxWidgets/wxWidgets#25563)

Here's something I quickly cooked up to always open the file with the default browser. This would be much easier if you bridge into cocoa instead of using Core Foundation. Also note the lack of error checking or memory management.

CFStringRef cfPath = CFStringCreateWithCString(NULL, file.GetFullPath().utf8_str(), kCFStringEncodingUTF8);
if ( cfPath )
{
	CFURLRef curls[1];
	curls[0] = CFURLCreateWithFileSystemPath(NULL, cfPath, kCFURLPOSIXPathStyle, false);
	if ( curls[0] )
	{
		CFURLRef scheme = CFURLCreateWithString(NULL, CFSTR("http://"), NULL);
		CFURLRef appURL = LSCopyDefaultApplicationURLForURL(scheme, kLSRolesAll, NULL);
		CFArrayRef urls = CFArrayCreate(NULL, (const void **)curls, 1, NULL);
				
		LSLaunchURLSpec spec = {};
		spec.appURL = appURL;
		spec.itemURLs = urls;
						
		LSOpenFromURLSpec(&spec, NULL);
	}
}


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/25563/3013990060@github.com>

VZ

unread,
Jun 28, 2025, 7:21:01 PM6/28/25
to wx-...@googlegroups.com, Subscribed
vadz left a comment (wxWidgets/wxWidgets#25563)

We could define a function using Cocoa directly in src/osx/cocoa/utils.mm and call it from wxDoLaunchDefaultBrowser(). I'd still reuse wxOSXCreateURLFromFileSystemPath() because there doesn't seem to be any reason not to (and it calls CFStringNormalize(), which is not done by the code above).

As usual, if you could please make a PR implementing this, it would be gratefully accepted!


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/25563/3016144149@github.com>

Reply all
Reply to author
Forward
0 new messages