开发者

Python "show in finder"

开发者 https://www.devze.com 2023-01-12 10:13 出处:网络
How can I launch a new Finder window (or Explorer on Win) from python in a specific 开发者_StackOverflow社区folder. The behaviour I\'m looking for is the equivalent of \"Show in finder\" link in a tra

How can I launch a new Finder window (or Explorer on Win) from python in a specific 开发者_StackOverflow社区folder. The behaviour I'm looking for is the equivalent of "Show in finder" link in a tracks context menu in iTunes or most other programs come to think of it.

What I have currently is a UI built with PyQt and I'd like to add a menu option like "show log folder" or something similar, which would pop up a new finder window.

UPDATE:

From katrielalex advice trying subprocess.Popen("/System/Library/CoreServices/Finder.app") throws and OSError: Permission denied. Trying to launch the Finder.app by double clicking it says that it is in use by OS X and cannot be opened.

Surely there must be a way to open a new Finder window.


For OS X, you can use the Finder's Apple Events (AppleScript) interface via py-appscript:

>>> 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

EDIT:

An even simpler solution on OS X 10.6 is to use the new -R (Reveal) option to the open command (see man 1 open):

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


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


Windows:

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


For Finder the shell command

open ~/

would open a new window.

0

精彩评论

暂无评论...
验证码 换一张
取 消