views:

215

answers:

3

How would I be able to open files from my application? For example, they type in the directory in a textfield called "inputBox" and they press the button open, to open the file.

Thanks

Kevin

+2  A: 

You can use the NSWorkspace class to open files.

It has a new useful methods:

Opening Files

– openFile:  
– openFile:withApplication:  
– openFile:fromImage:at:inView:  
– openFile:withApplication:andDeactivate:  
– openTempFile:  
– openURL:

Their descriptions are in the docs at http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace%5FClass/NSWorkspace%5FClass.pdf

Jasarien
+2  A: 

For example, they type in the directory in a textfield called "inputBox" and they press the button open, to open the file.

Why not use NSOpenPanel instead? Then do what Jasarien said with the paths or file: URLs (your choice) that it gives you.

Peter Hosey
+2  A: 

You would definitely not normally make your user type the directory name to open a file. This is user-unfriendly and contrary to the normal Mac experience. The one exception might be programming-related apps, such as the Quick Open dialog in Xcode.

The standard way to present a user interface for opening files is to use an NSOpenPanel. You can specify the type(s) of file you want the user to be able to choose, and the open panel will return the paths of the file(s) that the user selects.

Rob Keniger