I am new to cocoa programming, Using the below code I want to show the selected file names in window. How can I do that?
- (IBAction)selectFile:(id)sender {
int i; // Loop counter.
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
NSArray *fileTypes = [NSArray arrayWithObjects:#"wmv", #"3gp", #"mp4", #"avi", #"mp3", #"mma", #"wav", nil];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
//Enable multiple selection of files
[openDlg setAllowsMultipleSelection:YES];
// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil types:fileTypes] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
NSString* fileName = [files objectAtIndex:i];
NSLog(#"filename::: %#", fileName);
// Do something with the filename.
}
}
}
In NSLog I am getting the names, what I want is to show the names on window too, to show the user that these files are selected.
Which view can be used? What is the way to achieve this?
Thanx
Use an NSTextView or an NSTextField.
NSArray* files = [openDlg filenames];
NSString* fileName;
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
fileName =[fileName stringByAppendingString:[files objectAtIndex:i];
// Do something with the filename.
}
NSLog(#"filename::: %#", fileName);
textView.text=fileName;
runModalForDirectory:file:types: is deprecated in OS X v10.6. You could use runModal instead.
You can set path using setDirectoryURL:, and you can set fileTypes using setAllowedFileTypes:.
Related
Im trying to save the file into the path, which I manual input into text field or select from FileBrowser. However it doesn't work.
If I drop path to textfield then it working fine. Could you help me.
- (IBAction)btnRawDataPath:(id)sender {
_txtLog.stringValue = #"";
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:NO];
// Multiple files not allowed
[openDlg setAllowsMultipleSelection:NO];
// Can't select a directory
[openDlg setCanChooseDirectories:YES];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModal] == NSModalResponseOK )
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* urls = [openDlg filenames];
// Loop through all the files and process them.
for(int i = 0; i < [urls count]; i++ )
{
//fileList.push_back(std::string([[[urls objectAtIndex:i] path] UTF8String]));
NSString* url = [urls objectAtIndex:i] ;
NSLog(#"Url: %#", url);
_txtRawDataPath.stringValue = url;
}
}
}
// EXCUTE
NSString *filePath = [_txtRawDataPath.stringValue stringByAppendingPathComponent:#"outputFIle.csv"];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
if ([settingsData writeToFile:filePath atomically:YES ])
NSLog(#"%#", filePath);
I already refered this link
iMedia
Now i am using NSOpenPanel to open iPhoto library folder.
Here is the code which allow to open.
int i = 0;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setAllowedFileTypes:[NSArray arrayWithObjects:#"public.image",#"public.video",nil]];
[openDlg setAllowsMultipleSelection:TRUE];
[openDlg setAllowsOtherFileTypes:NO];
if ( [openDlg runModal] == NSOKButton )
{
NSArray *files = [openDlg URLs];
for( i = 0; i < [files count]; i++ )
{
NSLog(#"File path: %#", [[files objectAtIndex:i] path]);
}
}
This code always open my Finder library folder, As i want to open Media Photo and Movies folder directly.
Please give me any solution.
I have attached here one screen shot.
Thanks & Regards,
You may consider adding the following code snipet:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSURL *picturesURL = [fileManager URLForDirectory:NSPicturesDirectory inDomain:NSUserDomainMask appropriateForURL:NULL create:NO error:&error];
NSURL *iPhotoURL = [picturesURL URLByAppendingPathComponent:#"iPhoto Library.photolibrary"];
[openDlg setDirectoryURL:iPhotoURL];
// Now run the dialog
This won't help if the user has moved his iPhoto library to some non-standard location.
There is a private API of Apple that contains exactly the control you want to access;
this control is an ILMediaBrowserView and provides the exact same view than the one in NSOpenDialog. If you are planning an AppStore release of your app don't use it but it can be useful. The framework to integrate to your project to get that view is iLifeMediaBrowser.framework in /System/Library/PrivateFrameworks.
I am selecting files through this code:
- (IBAction)selectFile:(id)sender {
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setPrompt:#"Select"];
fileTypes = [NSArray arrayWithObjects:#"wmv", #"3gp", #"mp4", #"avi", #"mp3", #"mma", #"wav", #"jpeg", #"png", #"jpg", #"tiff", nil];
// NSArray *JpegfileTypes = [NSArray arrayWithObjects:#"jpeg", #"png", #"jpg", #"tiff", #"mp3" nil];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
//Enable multiple selection of files
[openDlg setAllowsMultipleSelection:YES];
// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil types:fileTypes] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
files = [[openDlg filenames] retain];
int i; // Loop counter.
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
NSString *tempFilePath = [files objectAtIndex:i];
NSLog(#"tempFilePath::: %#",tempFilePath);
inputFilePath = [[files objectAtIndex:i] retain];
NSLog(#"filename::: %#", inputFilePath);
// Do something with the filename.
[selectedFile setStringValue:inputFilePath];
NSLog(#"selectedFile:::: %#", selectedFile);
}
}
}
Then after selection I have used this code to process the selected file.
- (IBAction)setMessage:(id)sender {
[fileGenProgress startAnimation:self];
NSString *message = [[NSString alloc] initWithFormat:#"Started"];
[lblMessage setStringValue:message];
[message release];
[self startProcessingVideoFile];
[self startProcessingAudioFile];
[self startProcessingJpg];
}
The issue I am facing is that, I am not getting that how would I compare the different strings like if the selected file was 3gp/mp4 or jpg or mp3. As if user has selected some video file then the method [self startProcessingVideoFile]; will run and if he has selected some JPG or PNG etc file then [self startProcessingAudioFile]; method will run.
As selected will be having a path not only the extension of the file. So in this scenario how can I force the - (IBAction)setMessage:(id)sender method to run the appropriate method.
You can get string's extension (format) like this:
NSString *extension = [stringPath pathExtension];
And for comparing now it's very easy when You know what extension Your file is. For example:
NSLog(extension);
if ([extension isEqualToString:#"3gp"] || [ext isEqualToString:#"mp4"]) {
[self startProcessingVideoFile];
}
and etc.
Update:
Your IBAction:SetMessage should look like this:
- (IBAction)setMessage:(id)sender {
[fileGenProgress startAnimation:self];
NSString *message = [[NSString alloc] initWithFormat:#"Started"];
[lblMessage setStringValue:message];
[message release];
NSString *extension = [inputFilePath pathExtension];
NSLog(extension);
if ([extension isEqualToString:#"3gp"] || [ext isEqualToString:#"mp4"]) {
[self startProcessingVideoFile];
}
// And etc for others formats.
//[self startProcessingAudioFile];
//[self startProcessingJpg];
}
I have a File Open Dialog box in my application to select files from, but when the user clicks the 'Select' button in the box, it obviously won't do anything. How do I extract the filepath from the selected file? I need the filepath so I can get the contents of the file to encrypt. Initially, I hard coded the file I would use into my application, but that was only for testing purposes. Here is what I am using for the File Open Dialog Box:
int i;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:YES];
[openDlg setPrompt:#"Select"];
NSString *fileName = [pathAsNSString lastPathComponent];
[fileName stringByDeletingPathExtension];
if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
NSArray* files = [openDlg filenames];
for( i = 0; i < [files count]; i++ )
{
[files objectAtIndex:i];
}
}
Thanks so much for the help.
Use - (NSArray *)URLs method instead of filenames.
Your code is already handling the files that the user has selected, you're just not doing anything with them.
The array returned from the ‑filenames method contains the paths to the files that the user selected as NSString objects. If they have only selected one file, there will only be one object in the array. If they have selected no files, the array will be empty.
if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
NSArray* files = [openDlg filenames];
for(NSString* filePath in [openDlg filenames])
{
NSLog(#"%#",filePath);
//do something with the file at filePath
}
}
If you only want the user to be able to select a single file, then call [openPanel setAllowsMultipleSelection:NO] when you're configuring the panel. That way, there will be a maximum of one entry in the filenames array.
As #VenoMKO points out, the ‑filenames method is now deprecated and you should use the ‑URLs method instead. This will return an array of file NSURL objects rather than an array of NSStrings. Since pretty much all the file handling APIs in Snow Leopard were revised to take URLs, this would be the preferred option.
You Want to Get File Path Using Following Code
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection: NO];
[openPanel setAllowedFileTypes:ArrExtension ];
if ([openPanel runModal] == NSOKButton ){
NSString *FilePath = [NSString stringWithFormat:#"%#",[openPanel URL]];
[openPanel canHide];
}
I'm learning Objective-C and trying to develop a simple zipper application, but I stopped when now, when I need to insert a button at my dialog and this button opens a Open File Dialog that will select a file to compress, but I never used a Open File Dialog, then how I can open it and store the user selected file in a char*? Thanks.
Remember that I'm using GNUstep(Linux).
Thanks #Vlad the Impala I am updating your answers for people who use OS X v10.6+
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
// Multiple files not allowed
[openDlg setAllowsMultipleSelection:NO];
// Can't select a directory
[openDlg setCanChooseDirectories:NO];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModal] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* urls = [openDlg URLs];
// Loop through all the files and process them.
for(int i = 0; i < [urls count]; i++ )
{
NSString* url = [urls objectAtIndex:i];
NSLog(#"Url: %#", url);
}
}
In case someone else needs this answer, here it is:
int i;
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
// Multiple files not allowed
[openDlg setAllowsMultipleSelection:NO];
// Can't select a directory
[openDlg setCanChooseDirectories:NO];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
NSString* fileName = [files objectAtIndex:i];
}
For people who use OS X v10.10+, replace in Far Jangtrakool answer:
if ( [openDlg runModal] == NSOKButton )
by
if ( [openDlg runModal] == NSModalResponseOK )