SDL: cocoa: Add text/uri-list entries to the pasteboard as URLs

From a5a387bd79186a4eac7d627bb46715987457bfa8 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Fri, 16 Oct 2026 16:28:40 -0400
Subject: [PATCH] cocoa: Add text/uri-list entries to the pasteboard as URLs

Add the file URLs from text/uri-list to the pasteboard as URLs. This allows pasting file paths into Finder.
---
 src/video/cocoa/SDL_cocoaclipboard.m | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/src/video/cocoa/SDL_cocoaclipboard.m b/src/video/cocoa/SDL_cocoaclipboard.m
index 54bcd68f78295..5c8f4ac110ab6 100644
--- a/src/video/cocoa/SDL_cocoaclipboard.m
+++ b/src/video/cocoa/SDL_cocoaclipboard.m
@@ -96,6 +96,31 @@ - (void)pasteboard:(NSPasteboard *)pasteboard
     }
 }
 
+static void SetURLsFromTextURIs(SDL_VideoDevice *_this, NSPasteboard *pasteboard)
+{
+    @autoreleasepool {
+        size_t size = 0;
+        const char *urls = _this->clipboard_callback(_this->clipboard_userdata, "text/uri-list", &size);
+        if (urls) {
+            char *str = SDL_strdup(urls);
+            if (str) {
+                NSMutableArray *array = [NSMutableArray new];
+                char *saveptr = NULL;
+                char *token = SDL_strtok_r(str, "\r\n", &saveptr);
+                while (token) {
+                    NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:token]];
+                    if (url && [url isFileURL]) {
+                        [array addObject:url];
+                    }
+                    token = SDL_strtok_r(NULL, "\r\n", &saveptr);
+                }
+                [pasteboard writeObjects:array];
+                SDL_free(str);
+            }
+        }
+    }
+}
+
 static char **GetMimeTypes(int *pnformats)
 {
     NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
@@ -221,6 +246,7 @@ bool Cocoa_SetClipboardData(SDL_VideoDevice *_this)
         Cocoa_PasteboardDataProvider *provider = [[Cocoa_PasteboardDataProvider alloc] initWith: _this->clipboard_callback userData: _this->clipboard_userdata];
         BOOL itemResult = FALSE;
         BOOL writeResult = FALSE;
+        BOOL appendURLs = FALSE;
 
         if (_this->clipboard_callback) {
             for (int i = 0; i < _this->num_clipboard_mime_types; i++) {
@@ -230,6 +256,10 @@ bool Cocoa_SetClipboardData(SDL_VideoDevice *_this)
 
                 [utiTypes addObject: (__bridge NSString *)utiType];
                 CFRelease(utiType);
+
+                if (SDL_strcmp(_this->clipboard_mime_types[i], "text/uri-list") == 0) {
+                    appendURLs = TRUE;
+                }
             }
             itemResult = [newItem setDataProvider: provider forTypes: utiTypes];
             if (itemResult == FALSE) {
@@ -241,6 +271,9 @@ bool Cocoa_SetClipboardData(SDL_VideoDevice *_this)
             if (writeResult == FALSE) {
                 return SDL_SetError("Unable to set clipboard data");
             }
+            if (appendURLs) {
+                SetURLsFromTextURIs(_this, pasteboard);
+            }
         } else {
             [pasteboard clearContents];
         }