Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   File path in Mac OS X (http://www.programmingforums.org/showthread.php?t=12491)

poopman Feb 2nd, 2007 5:41 PM

File path in Mac OS X
 
Hey all,

I'm pretty new Python, but I'm been reading over it and writing some cool scripts. I've developed a script and I need a little help with something that seems to be really easy but, for the life of me, I cannot figure out how to do it. Say I have a finder window open and have a directory selected. If I were to make my pyton script an app and were to select the directory and then run the script/app, how do I get the entire path of the directory? If anyone could help me out it would be greatly appreciated.

Thanks! :banana:

titaniumdecoy Feb 2nd, 2007 7:02 PM

Are you asking how to determine the path of the directory currently selected in the Finder, or just how to get the path of a particular directory to use in your script?

poopman Feb 2nd, 2007 7:06 PM

Thanks for replying back. I'm asking how to determine the path of the directory currenttly selected in the finder.

titaniumdecoy Feb 2nd, 2007 9:59 PM

The only way to do this (that I aware of) is with AppleScript. Open Script Editor, copy and paste the following into a blank document, and save it as an application named GetSelectedFolders.app:

:

on run
        tell application "Finder"
                copy selection to theSelected
                set outputPathList to {}
                repeat with anItem in theSelected
                        if kind of (anItem as alias) is "folder" then
                                copy (POSIX path of (anItem as alias)) to end of outputPathList
                        end if
                end repeat
                return outputPathList
        end tell
end run

You can now call this AppleScript from within your Python code. The result (selected_folders in the code below) will be a list of filepaths.

:

import os
script_path = "osascript ~/path/to/GetSelectedFolders.app"
selected_folders = [path.strip() for path in os.popen(script).readlines()]

Hope that helps! You might find this post useful as well.


All times are GMT -5. The time now is 1:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC