向apple Launch Services注册应用程序以处理UTI和URL的简单包
applaunchservices的Python项目详细描述
应用程序服务
一个简单的软件包,注册一个应用程序与苹果启动服务,以处理UTI和URL。详情请参阅苹果文档。在
网址
启动服务允许GUI应用程序注册URL方案。
这意味着当用户键入一个类似<scheme>://<something>
的URL时,可以调用该应用程序。在
set_URL_scheme_handler
:将给定的bundleid设置为给定url方案的默认处理程序。在get_URL_scheme_handler
:获取给定url方案的默认bundleid。在open_URL
:使用启动服务打开给定的URL
文件
启动服务允许GUI应用程序注册统一类型标识符(UTI)。
这意味着当用户双击finder中与此方案匹配的文件时,可以调用该应用程序。
或者如果用户键入一个url,比如<file:///path/to/file.ext>
。在
set_UTI_handler
:将给定的bundleid设置为给定统一类型标识符和角色的默认处理程序。在get_UTI_handler
:获取给定统一类型标识符和角色的默认bundleid。在open_path
:使用启动服务打开给定路径
角色包括:
- '无'
- '查看器'
- '编辑器'
- '外壳'
- '全部'
捆绑标识符
bundle标识符用于标识应用程序。提供两种功能:
get_bundle_identifier()
:获取当前bundle标识符(如果存在)(该应用程序是GUI应用程序)get_bundle_identifier(pid)
:获取给定进程id的bundle标识符(如果存在)(该应用程序是GUI应用程序)get_bundle_identifier_for_path(path)
:如果路径指向一个bundle,则获取bundle标识符
使用示例:
importapplaunchservicesasalsfromPyQt5.QtWidgetsimportQApplication,QWidgetfromPyQt5.QtCoreimportQEvent,Qt# This app opens python scripts as an editoruniform_type_identifier="public.python-script"role='editor'classMacApplication(QApplication):"""Application that process fileopen events."""defevent(self,event):ifevent.type()==QEvent.FileOpen:widget.setWindowTitle(str(event.file()))returnQApplication.event(self,event)# Create application and windowapp=MacApplication([''])widget=QWidget()# Reset old handler at the endold_handler=als.get_UTI_handler(uniform_type_identifier,role)app.aboutToQuit.connect(lambda:als.set_UTI_handler(uniform_type_identifier,role,old_handler))# When the app is visible, register itself as a handlerdefhandle_applicationStateChanged(state):ifstate==Qt.ApplicationActiveandapp._starting:app._starting=Falsebundle_identifier=als.get_bundle_identifier()als.set_UTI_handler(uniform_type_identifier,role,bundle_identifier)app._starting=Trueapp.applicationStateChanged.connect(handle_applicationStateChanged)# Launch appwidget.setWindowTitle('test')widget.show()app.exec_()# The app can now receive file open events
- 项目
标签: