通过 PyObjC 在 Mac OS X 中使用 ScriptingBridge 恢复垃圾项

发布于 2024-09-16 23:48:48 字数 522 浏览 3 评论 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()

任何建议?

谢谢!

附: 我用的是雪豹。

I'm trying to figure out a way to restore (Put Back) Trash Item using ScriptingBridge via PyObjC.

There isn't enough documentation here

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

Any suggestions?

Thanks!

PS:
I'm using Snow Leopard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-09-23 23:48:48

当从 Python 处理支持 AppleScript 的应用程序时,您几乎总是会发现使用 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 事件引用。作弊一下并获取垃圾文件夹的路径并对其使用标准文件系统操作可能更容易:

>>> app("System Events").trash.POSIX_path()
u'/Users/nad/.Trash'

When dealing with AppleScript-able applications from Python, you will almost always find it easier to use appscript rather than Apple's ScriptingBridge or PyObjC. One way to do it:

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)

The trick is getting the right Apple Event reference to the desired files and folders. It may be even easier to cheat a bit and get the path to the trash folder and use standard file system operations on it:

>>> app("System Events").trash.POSIX_path()
u'/Users/nad/.Trash'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文