【发布时间】:2014-01-20 21:44:31
【问题描述】:
在 Python 2 中,我可以使用以下代码来解析 MacOS 别名或符号链接:
from Carbon import File
File.FSResolveAliasFile(alias_fp, True)[0].as_pathname()
其中 alias_fp 是我感兴趣的文件的路径,存储为字符串 (source)。
但是,the documentation cheerfully tells me that the whole Carbon family of modules is deprecated。我应该改用什么?
编辑:我相信下面的代码是 PyObjC 方法朝着正确方向迈出的一步。它不解析别名,但似乎可以检测到它们。
from AppKit import NSWorkspace
def is_alias (path):
uti, err = NSWorkspace.sharedWorkspace().typeOfFile_error_(
os.path.realpath(path), None)
if err:
raise Exception(unicode(err))
else:
return "com.apple.alias-file" == uti
(source)
不幸的是,我无法让@Milliways 的解决方案发挥作用(对 Cocoa 一无所知),stuff I find elsewhere on the internet 看起来要复杂得多(也许它正在处理各种边缘情况?)。
【问题讨论】:
标签: python macos cocoa symlink macos-carbon