QDesktopServices.setUrlHandler not called on OS X

Marko Luther <[email protected]>
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <[email protected]>
Dear all,

I am struggling to get the expected call-back from a URL handler registered via QDesktopServices.setUrlHandler on opening the registered URL scheme (test://<path>).

The scheme is registered via the following info.plist entry 

	<key>CFBundleURLTypes</key>
	<array>
		<dict>
			<key>CFBundleURLName</key>
			<string>com.simple</string>
			<key>CFBundleURLSchemes</key>
			<array>
				<string>test</string>
			</array>
		</dict>
	</array>

The handler is installed in the PyQt script via

QDesktopServices.setUrlHandler('test', open_desktopservices_url)

with 

def open_desktopservices_url(url):
    print("open",url)

or

handler = URLHandler()
QDesktopServices.setUrlHandler("test", handler.handleURL) 

class URLHandler(QObject):
    def handleURL(self, url):
        open_desktopservices_url(url)


To open the URL I put the URL "test://me" into Safari or use the terminal with

# open test://me

Opening the URL brings the app to the foreground, but the handler is never called.

I found a way to catch this URL request by catching a FileOpen event and extracting the given URL from the event as follows

class Simple(QApplication):
    def __init__(self, args):
        super(Simple, self).__init__(args)

    def event(self, event):
        if event.type() == QEvent.FileOpen:
            url = event.url()
            if url.isValid():
                print("url",event.url())
        return super(Simple, self).event(event)


Is this the way it has to be done?

I attached a minimal sample app sample.py together with a setup.py script to build the app with py2app that builds and runs as follows:

# python3 setup.py py2app
# dist/Simple.app/Contents/MacOS/Simple

This is on OS X 10.13.6, Python 3.7.3, PyQt 5.12.13, py2app 0.19

Thanks for any pointer in the right direction,
Marko

_______________________________________________
PyQt mailing list    [email protected]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
setup.py (text/x-python-script, 796 B)
"""
Usage:
    python3 setup.py py2app
"""

from setuptools import setup
import os

APP = ['simple.py']
APP_NAME = "Simple"
OPTIONS = {}

OPTIONS = {
    'argv_emulation': False,
    'plist': {
        'test': 'test',
        'CFBundleName': APP_NAME,
        'CFBundleDisplayName': APP_NAME,
        'CFBundleGetInfoString': "Simple",
        'CFBundleIdentifier': "com.simple",
        'CFBundleVersion': "0.1.0",
        'CFBundleShortVersionString': "0.1.0",
        'NSHumanReadableCopyright': u"Copyright 2019",
        'CFBundleURLTypes': [
            {
                'CFBundleURLName' : 'com.simple',
                'CFBundleURLSchemes': ['test']
            }
        ]
    }
}

setup(
    name=APP_NAME,
    app=APP,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)
simple.py (text/x-python-script, 1.1 KB)
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QObject, Qt, QEvent


def open_desktopservices_url(url):
    print("open",url)

class URLHandler(QObject):
    def handleURL(self, url):
        open_desktopservices_url(url)

class Simple(QApplication):
    def __init__(self, args):
        super(Simple, self).__init__(args)

    def event(self, event):
        if event.type() == QEvent.FileOpen:
            url = event.url()
            if url.isValid():
                print("url",event.url())
        return super(Simple, self).event(event)

if __name__ == '__main__':
    app = Simple(sys.argv)

    # custom URL schemes via setUrlHandler seem not to work
    #QDesktopServices.setUrlHandler('test', open_desktopservices_url)
    handler = URLHandler()
    QDesktopServices.setUrlHandler("test", handler.handleURL)

    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)

    if len(sys.argv) > 1:
        title = str(sys.argv[1])
    else:
        title = "simple"
    w.setWindowTitle(title)
    w.show()

    sys.exit(app.exec_())
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.