0

我试图找出一种通过 PyObjC 使用 ScriptingBridge 恢复(放回)垃圾项的方法。

这里没有足够的文档

from AppKit import NSURL
from ScriptingBridge import SBApplication
targetfile = NSURL.fileURLWithPath_(f.realpath)
finder = SBApplication.applicationWithBundleIdentifier_("com.apple.Finder")
trash_items = finder.trash.items()

有什么建议么?

谢谢!

PS:我用的是雪豹。

4

1 回答 1

1

在处理AppleScript来自 Python 的 -able 应用程序时,您几乎总是会发现使用appscript比使用 Apple 的ScriptingBridgePyObjC. 一种方法:

from appscript import *
# move file to trash
app("Finder").move(mactypes.File(f.realpath),to=its.trash)
# get names of all items in the Trash
app("Finder").trash.items.name.get()
# move file x.txt from Trash to Desktop Folder
app("Finder").trash.files["x.txt"].move(to=its.desktop)

诀窍是获得对所需文件和文件夹的正确 Apple Event 引用。欺骗一下并获取垃圾文件夹的路径并对其使用标准文件系统操作可能更容易:

>>> app("System Events").trash.POSIX_path()
u'/Users/nad/.Trash'
于 2010-09-04T09:04:04.173 回答