如何通过py-appscript访问Path Finder中的文件选择?
在Mac OS X上使用文件管理器Path Finder时,我想通过Python来获取选中的文件或文件夹,使用的工具是py-appscript。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
那么,我该如何把selection_list中的引用转换成Python字符串呢?
2 个回答
0
你可以试试 python-applescript 这个工具,它可以通过 Python 来运行 AppleScript 的脚本。你可以在这里下载:http://pypi.python.org/pypi/python-applescript
0
我对Pathfinder不太熟悉,但如果它有自己特定的文件网址类型(或者可能是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网址的例子,我就能给出更好的答案。