1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

file dialog box

Posted by chris on Mar 07, 2008

i asked a little bit ago if there was a way to
get a nice apple-ish dialog box to choose files
for input; Tom pointed me in the right direction
(Thanks, Tom!) - and here's my code snippet:

def select_file(file_type):
 
    # Initialize a standard Mac filebrowser.
    filebrowser = NSOpenPanel.openPanel()
    # set the title of the choose-me button
    filebrowser.setPrompt_(file_type)
    # set the window name in the title bar
    filebrowser.setTitle_(file_type)
 
    # We'll open the filebrowser to let the user select files.
    # Code execution will halt until the user either presses  
    # "Cancel" or the select button    
    result = filebrowser.runModalForDirectory_file_types_(None, None, ["aim", "txt", "lut", "foo"])
 
    if (result == NSOKButton):
        # If "Open" was pressed, we have some files waiting for us.
        return filebrowser.filenames()
 
...
 
file_path = select_file("Select Aim File")
hope this is useful for everyone


 
Posted by chris on Mar 07, 2008

sorry, but i lost the end of one of the lines.
i'll be more careful next time. here's the
whole line:

result = filebrowser.runModalForDirectory_file_types_
(None, None, ["aim", "txt", "lut", "arriden"])
this is one line, so the open paren follows
the last underscore: ... types_(None, ...

file_path will end up with as many files
as you select, so you may end up with more
than one after the return!



Posted by chris on Mar 13, 2008

Oops, i forgot this:

from AppKit import *



Posted by Tom De Smedt on Mar 13, 2008

Hi Chris, I've generalized your script a bit further:

from AppKit import NSOpenPanel, NSOKButton
 
def select_file(title="Open", types=[]):
    filebrowser = NSOpenPanel.openPanel()
    filebrowser.setTitle_(title)
    result = filebrowser.runModalForDirectory_file_types_(None, None, types)
    if (result == NSOKButton):
        return filebrowser.filenames()
        
print select_file("Select Aim File", ["aim", "txt", "lut", "foo"])