我正在尝试使用 pyobjc 将 MPEG-4 注册为拖动类型,具体目标是能够将语音备忘录直接从应用程序拖到我自己的 python 应用程序中。
我尝试按照苹果官方文档注册UTI类型“public.audio”、“public.mpeg-4-audio”、NSPasteboard.URLType和NSPasteboard.fileURLType。然而,在我的程序上删除文件总是会导致相同的错误:“无法打开文档 x。Python 无法打开“Apple MPEG-4 音频”格式的文件。
这是一个最小的可重现示例:
from AppKit import NSApplication, NSObject, NSDragOperationCopy, NSWindow, NSView, NSPasteboard, NSPasteboard
from PyObjCTools import AppHelper
class DropView(NSView):
def initWithFrame_(self, frame):
self = super(DropView, self).initWithFrame_(frame)
if self:
self.registerForDraggedTypes_(["public.audio", "public.mpeg-4-audio", NSPasteboard.URLType, NSPasteboard.fileURLType])
return self
def draggingEntered_(self, sender):
pboard = sender.draggingPasteboard()
return NSDragOperationCopy
def performDragOperation_(self, sender):
pboard = sender.draggingPasteboard()
print(pboard)
return True
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(((100, 100), (400, 300)), 1 << 1 | 1 << 10, 2, False)
self.window.setTitle_("Drag-and-Drop Example")
drop_view = DropView.alloc().initWithFrame_(((0, 0), (400, 300)))
self.window.contentView().addSubview_(drop_view)
self.window.makeKeyAndOrderFront_(None)
def run_app():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
run_app()
以下源代码在我的系统(MacOS,Sonoma 14.4.1)上运行。我无法运行您在 Thonny 中发布的代码。更改了 registerForDraggedTypes() 的最后两个参数以及 objc 导入;它现在返回文件路径。您仍然需要添加 AVPlayer 才能听到删除的文件。
from Cocoa import (
NSApplication,
NSObject,
NSDragOperationCopy,
NSWindow,
NSView,
NSPasteboard,
NSPasteboard,
NSPasteboard,
NSDragOperationCopy,
NSPasteboardType,
NSPasteboardTypeURL,
NSPasteboardTypeFileURL,
NSBezelStyleCircular,
NSFilenamesPboardType,
)
from PyObjCTools import AppHelper
from objc import super
class DropView(NSView):
def initWithFrame_(self, frame):
self = super(DropView, self).initWithFrame_(frame)
if self:
self.registerForDraggedTypes_(
[
"public.audio",
"public.mpeg-4-audio",
NSPasteboardTypeURL,
NSPasteboardTypeFileURL,
]
)
return self
def draggingEntered_(self, sender):
pboard = sender.draggingPasteboard()
print("dragging entered.")
print(pboard)
return NSDragOperationCopy
def performDragOperation_(self, sender):
pboard = sender.draggingPasteboard()
files = pboard.propertyListForType_(NSFilenamesPboardType)
print(files.objectAtIndex_(0))
return True
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
((100, 100), (400, 300)), 1 << 1 | 1 << 10, 2, False
)
self.window.setTitle_("Drag-and-Drop Example")
drop_view = DropView.alloc().initWithFrame_(((0, 0), (400, 300)))
self.window.contentView().addSubview_(drop_view)
self.window.makeKeyAndOrderFront_(None)
def run_app():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == "__main__":
run_app()