Python“在finder中显示”

时间:2010-08-19 09:42:30

标签: python

如何在特定文件夹中从python启动新的Finder窗口(或Win on Explorer)。我正在寻找的行为相当于iTunes中的音轨上下文菜单中的“Show in Finder”链接,或者大多数其他程序都会想到它。

我目前使用PyQt构建的UI,我想添加一个菜单选项,如“show log folder”或类似的东西,它会弹出一个新的finder窗口。

更新

来自 katrielalex 的建议尝试subprocess.Popen("/System/Library/CoreServices/Finder.app")投掷和OSError: Permission denied。尝试通过双击启动Finder.app,表示它已被OS X使用,无法打开。

当然必须有办法打开一个新的Finder窗口。

4 个答案:

答案 0 :(得分:12)

对于OS X,您可以通过py-appscript使用Finder的Apple Events(AppleScript)界面:

>>> from appscript import *
>>> file_to_show = "/Applications/iTunes.app"
>>> app("Finder").reveal(mactypes.Alias(file_to_show).alias)
app(u'/System/Library/CoreServices/Finder.app').startup_disk.folders[u'Applications'].application_files[u'iTunes.app']
>>> #  Finder window of "Applications" folder appears with iTunes selected

编辑:

OS X 10.6上更简单的解决方案是使用-R命令的新open(显示)选项(参见man 1 open):

>>> import subprocess
>>> file_to_show = "/Applications/iTunes.app"
>>> subprocess.call(["open", "-R", file_to_show])

答案 1 :(得分:1)

视窗:

>>> import subprocess
>>> subprocess.Popen( "explorer i:" )
<subprocess.Popen object at 0x00C46DB0>

答案 2 :(得分:1)

from subprocess import call
targetDirectory = "~/Desktop"
call(["open", targetDirectory])

答案 3 :(得分:-1)

对于Finder的shell命令

open ~/

会打开一个新窗口。