def register_app_launchservices( uniform_type_identifier="public.python-script", role='editor'): """ Register app to the Apple launch services so it can open Python files """ app = QApplication.instance() old_handler = als.get_UTI_handler(uniform_type_identifier, role) app._original_handlers[(uniform_type_identifier, role)] = old_handler # Restore previous handle when quitting app.aboutToQuit.connect(restore_launchservices) if not app._never_shown: bundle_identifier = als.get_bundle_identifier() als.set_UTI_handler( uniform_type_identifier, role, bundle_identifier) return # Wait to be visible to set ourselves as the UTI handler def handle_applicationStateChanged(state): if state == Qt.ApplicationActive and app._never_shown: app._never_shown = False bundle_identifier = als.get_bundle_identifier() als.set_UTI_handler( uniform_type_identifier, role, bundle_identifier) app.applicationStateChanged.connect(handle_applicationStateChanged)
def register_app_launchservices(uniform_type_identifier="public.python-script", role='editor'): """ Register app to the Apple launch services so it can open Python files """ app = QApplication.instance() # If top frame is MAC_APP_NAME, set ourselves to open files at startup origin_filename = get_origin_filename() if MAC_APP_NAME in origin_filename: bundle_idx = origin_filename.find(MAC_APP_NAME) old_handler = als.get_bundle_identifier_for_path( origin_filename[:bundle_idx] + MAC_APP_NAME) else: # Else, just restore the previous handler old_handler = als.get_UTI_handler(uniform_type_identifier, role) app._original_handlers[(uniform_type_identifier, role)] = old_handler # Restore previous handle when quitting app.aboutToQuit.connect(restore_launchservices) if not app._never_shown: bundle_identifier = als.get_bundle_identifier() als.set_UTI_handler(uniform_type_identifier, role, bundle_identifier) return # Wait to be visible to set ourselves as the UTI handler def handle_applicationStateChanged(state): if state == Qt.ApplicationActive and app._never_shown: app._never_shown = False bundle_identifier = als.get_bundle_identifier() als.set_UTI_handler(uniform_type_identifier, role, bundle_identifier) app.applicationStateChanged.connect(handle_applicationStateChanged)
def test_UTI(): # This app opens python scripts as an editor uniform_type_identifier = "public.python-script" role = 'editor' url_scheme = "alstest" url = url_scheme + '://hello' class MacApplication(QApplication): """Application that process fileopen events.""" def event(self, event): if event.type() == QEvent.FileOpen: filename = str(event.file()) url = event.url() if filename: app._last_file = filename elif url: app._last_url = url widget.setWindowTitle(filename) return QApplication.event(self, event) # Create application and window app = MacApplication(['']) widget = QWidget() # Reset old handler at the end old_UTI_handler = als.get_UTI_handler(uniform_type_identifier, role) old_URL_handler = als.get_URL_scheme_handler(url_scheme) def reset_handlers(): als.set_UTI_handler(uniform_type_identifier, role, old_UTI_handler) als.set_URL_scheme_handler(url_scheme, old_URL_handler) app.aboutToQuit.connect(reset_handlers) # When the app is visible, register itself as a handler def handle_applicationStateChanged(state): if state == Qt.ApplicationActive and app._starting: app._starting = False bundle_identifier = als.get_bundle_identifier() als.set_UTI_handler(uniform_type_identifier, role, bundle_identifier) als.set_URL_scheme_handler(url_scheme, bundle_identifier) app._starting = True app.applicationStateChanged.connect(handle_applicationStateChanged) # Launch app widget.setWindowTitle('test') widget.show() # Send an event in the future timer_url = QTimer(app) timer_url.setSingleShot(True) timer_url.timeout.connect(lambda: als.open_URL(url)) timer_url.start(1000) # Send an event in the future timer_open = QTimer(app) timer_open.setSingleShot(True) timer_open.timeout.connect(lambda: als.open_path(__file__)) timer_open.start(1100) # Send an event in the future timer_close = QTimer(app) timer_close.setSingleShot(True) timer_close.timeout.connect(lambda: app.quit()) timer_close.start(2000) app.exec_() # Check that we recieved the open file event assert app._last_file == __file__ assert app._last_url.scheme() == url_scheme # Check that the previous handler is restored assert old_UTI_handler == als.get_UTI_handler(uniform_type_identifier, role) assert old_URL_handler == als.get_URL_scheme_handler(url_scheme)