如何通过 py-appscript 访问路径查找器中的文件选择?

发布于 2024-10-01 02:47:16 字数 680 浏览 1 评论 0原文

在 mac os x 上使用文件管理器路径查找器,我想通过使用 py-appscript 使用 python 检索选定的文件/文件夹。 py-appscript 是一个高级事件桥,允许您从 Python 控制可编写脚本的 Mac OS X 应用程序。

在 applescript 中,它会类似于

tell application "Path Finder"
 set selection_list to selection -- list of fsItems (fsFiles and fsFolders)
 set _path to posix path of first item of selection_list
 do shell script "python " & quoted form of _path
end tell

在 python 中,它会类似于

from appscript import *
selectection_list = app('Path Finder').selection.get()   # returns reference, not string

那么,我如何将选择列表中的引用转换为 python 字符串?

Using the filemanager Path Finder on mac os x, i wanna retrieve the selected files/folders with python by using py-appscript. py-appscript is a high-level event bridge that allows you to control scriptable Mac OS X applications from Python.

In applescript it would be something like

tell application "Path Finder"
 set selection_list to selection -- list of fsItems (fsFiles and fsFolders)
 set _path to posix path of first item of selection_list
 do shell script "python " & quoted form of _path
end tell

In python it would instead something like

from appscript import *
selectection_list = app('Path Finder').selection.get()   # returns reference, not string

So, how can i convert the references in selection_list to python-strings?

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

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

发布评论

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

评论(2

从﹋此江山别 2024-10-08 02:47:16

我不熟悉 Pathfinder,但如果它有自己的文件 URL 类型(或者可能是 POSIX 路径?),那么可能存在某种分隔符来分隔路径中的文件层次结构级别。要在其中之一之间进行转换,您需要使用 Applescript 的文本项分隔符。按照这些思路应该可以

set thePathFinderPath to "/pathfinder/path/to/finder"

set pathFinderPathDelimiter to "/" -- whatever it may be here
set finderPathDelimiter to ":"

set AppleScript's text item delimiters to {pathFinderPathDelimiter}
set thePathComponents to (get every text item in thePathFinderPath) as list
set AppleScript's text item delimiters to {finderPathDelimiter}
set theFinderPath to thePathComponents as text
set AppleScript's text item delimiters to "" -- very important you clear the TIDs.

加盐调味。但是,如果您可以提供 PathFinder URL 的示例,那么我可以提供更好的答案。

I'm not familiar with Pathfinder, but if it has its own file URL type (or maybe it's a POSIX path?), then there is presumably a delimiter of some kind that separates the levels of file hierarchy in the path. To convert between one and the other, you need to work with Applescript's text item delimiters. Something along these lines should work

set thePathFinderPath to "/pathfinder/path/to/finder"

set pathFinderPathDelimiter to "/" -- whatever it may be here
set finderPathDelimiter to ":"

set AppleScript's text item delimiters to {pathFinderPathDelimiter}
set thePathComponents to (get every text item in thePathFinderPath) as list
set AppleScript's text item delimiters to {finderPathDelimiter}
set theFinderPath to thePathComponents as text
set AppleScript's text item delimiters to "" -- very important you clear the TIDs.

Add salt to taste. But, if you can provide an example of the PathFinder URL, then I can provide a better answer.

酒与心事 2024-10-08 02:47:16

为什么不尝试一下 python-applescript,它可以通过 python 运行 applescript 的脚本。在这里获取它: http://pypi.python.org/pypi/python-applescript

Why don't you try python-applescript, it can run applescript's script thru python. Get it here : http://pypi.python.org/pypi/python-applescript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文