尝试编写Python脚本来删除重复的字体Mac OSX
我正在尝试编写一个实用程序来检查 OSX 上的字体并删除重复项 - 到目前为止,我已经成功地迭代了文件夹结构并写出了包含字体和路径的 xml 文件。缺点是,这不会找到重复的字体,因为相同的字体可以在不同的位置安装两次,但文件名不同;字体系列信息位于字体文件内部。
我发现了这个: List of Installed fonts OS X / C
import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())
使用 PyObjC列出已安装的字体。这看起来是一个可能的解决方案,但我不是重量级编码员,对 PyObjC 一无所知!我确实在终端中使用 dir(manager)
和 dir(NSFontManager)
进行了尝试,看看是否可以找到返回已安装字体的文件路径的方法,但是无济于事。
非常感谢任何帮助。
最美好的祝愿
//西蒙
I am trying to write a utility to check through the fonts on OSX and remove duplicates - so far I've had success iterating through the folder structure and writing out an xml file containing fonts and paths. The drawback is that this doesn't find duplicates as the same font can be installed twice in different locations but with a different file name; font family information is internal to the font file.
I have found this: List of installed fonts OS X / C
import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())
Which uses PyObjC to list installed fonts. This looks like a possible solution but I'm not a heavy weight coder and know nothing about PyObjC! I did have a play around in a terminal with dir(manager)
and dir(NSFontManager)
to see if I could find a method that returned the file paths of installed fonts but to no avail.
Any help gratefully received.
Best wishes
//Simon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
安装
fonttools
软件包并查看此博客文章。那里有一个脚本给出了第一个参数的字体名称,我相信您可以根据您的用例调整它。Install the
fonttools
package and take a look at this blog post. There's a script there that gives the font name of its first argument and I'm sure you could adapt it to your use case.再次感谢您的回复 - 我只是想更新此内容,希望它有所帮助。我现在正在查看 fontTools,但承认我在查找文档时遇到了困难。根据开发者页面,除了 ttf 之外,还有一些库可以处理其他字体,但我仍然在寻找,但还没有成功!
同时,我确实在使用 Apple Font 上打开了此页面工具,这似乎满足了我的需要 - 即列出已安装的字体和位置。看起来 ftxinstalledfonts 命令将接受一个管道,因此理论上我可以将输出通过管道传输到构建 xml 数据库的现有 python 脚本。我假设我可以通过 sys 模块调用 ftxinstalledfonts 来做到这一点,但不清楚如何获取输出......我会去掠夺 python 文档,但如果运气不好的话如果有人能在 10 秒内给出指导,我将不胜感激。
顺便说一句,上面的 Font Geek 网站也有一些关于 TTX 的非常有用的东西。
最美好的祝愿
//西蒙
Thanks once again for the replies - I just wanted to update this in the hope it helps. I'm looking at fontTools now, but admit I am having trouble finding documentation. According to the developer's page there are libraries to handle other fonts apart from ttf, but I'm still looking with little success just yet!
In the mean time, I did turn up this page on Using Apple Font tools, which seems get half way to what I need - ie listing installed fonts and locations. It looks like the
ftxinstalledfonts
command will accept a pipe so in theory I could pipe the output to my existing python script that builds an xml database. I'm assuming I can do this by callingftxinstalledfonts
via the sys module but am not clear as to how to get the output... I'll go and plunder the python docs but if there's no luck and someone has 10 secs to give a pointer I'd be grateful.As a quick aside, the Font Geek site above has some very useful stuff about TTX as well.
Best wishes
//Simon