lab12
lab12

Reputation: 6448

Opening Files - Cocoa

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

Upvotes: 3

Views: 5461

Answers (4)

Jasarien
Jasarien

Reputation: 58458

You can use the NSWorkspace class to open files.

It has a few useful methods:

Opening Files

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

Their descriptions are in the docs at https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/index.html

Upvotes: 11

VSN
VSN

Reputation: 2361

Try this it is worked for me perfectly.

First get the local file path like blow...

 NSString *logFilePath=seedFileDirectory=[[NSString alloc]initWithFormat:@"%@/LOCAL FILE NAME ",NSHomeDirectory()];

Than open the file using logFilePath like below by calling method ...

 - (IBAction)viewFile:(id)sender {

[[NSWorkspace sharedWorkspace] openFile:logFilePath];

}

Your done!!!

Upvotes: 0

Rob Keniger
Rob Keniger

Reputation: 46030

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.

Upvotes: 5

Peter Hosey
Peter Hosey

Reputation: 96373

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.

Upvotes: 3

Related Questions