Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "show in finder"

Tags:

python

How can I launch a new Finder window (or Explorer on Win) from python in a specific 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.

like image 217
Matti Lyra Avatar asked Aug 19 '10 09:08

Matti Lyra


3 Answers

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])
like image 146
Ned Deily Avatar answered Nov 06 '22 21:11

Ned Deily


from subprocess import call
targetDirectory = "~/Desktop"
call(["open", targetDirectory])
like image 42
Tashi Trieu Avatar answered Nov 06 '22 19:11

Tashi Trieu


Windows:

>>> import subprocess
>>> subprocess.Popen( "explorer i:" )
<subprocess.Popen object at 0x00C46DB0>
like image 20
Katriel Avatar answered Nov 06 '22 20:11

Katriel