Fix Populated NSMenu Of Files, Folders And Their Respected Images
Fix Populated NSMenu Of Files, Folders And Their Respected Images
- Subject: Fix Populated NSMenu Of Files, Folders And Their Respected Images
- From: "Chunk 1978" <email@hidden>
- Date: Fri, 5 Dec 2008 10:44:49 -0500
hi.
i've been on this task for almost a week, and i still can't get it
right. basically, i would like to have an NSMenu populated with all
the files and folders of a specific directory. for this example i'll
use ~/Documents. the code below only partially works. it will list
files and folders from Documents in an NSMenu, but they are not
selectable. i've added a @selector, but it doesn't seem to be called.
the selector if just an example method - i don't want to really
terminate the app when an item is selected, i simply want the item to
launch.
second, it would be ideal to have the folders also produce their own
NSMenu list of files contained in them - i have no idea how to do
this.
third, the code below codes the NSMenuItem to be titled "Documents",
but it's title is not displayed when it's inserted at index:1 of
NSMenu *theMenu. however, if i insert it into [NSApp mainMenu], the
title "Documents" will display. this doesn't make sense to me.
and finally, i would love to have each of the files and folders to be
added to the NSMenu with their respected icons. my attempts to do
this have failed because i do not know how to get the path of each
file or folder that is being enumerated. the code below will warn
that NSString does not respond to Path.
to me this whole thing seems like something very common, and would be
used a lot by developers, but i've had such difficulty trying to find
resources. i've found bits and pieces and lots of dead ends. please
someone help me with this. sample code would be amazing... AMAZING!
or at least a guide into the right direction.
thanks in advance :)
-=-=-=- CODE -=-=-=-
- (void)awakeFromNib
{
NSMenu *documentsMenu = [[NSMenu alloc] initWithTitle:@"Documents"];
NSMenuItem *documentsItem = [[NSMenuItem alloc] initWithTitle: @""
action: nil keyEquivalent: @""];
[documentsItem setSubmenu:documentsMenu];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [@"~/Desktop" stringByExpandingTildeInPath];
NSDirectoryEnumerator *directoryEnumerator = [fileManager
enumeratorAtPath:documentsDirectory];
NSString *fileOrFolder;
while ((fileOrFolder = [directoryEnumerator nextObject]))
{
//ignore invisible files
if ([fileOrFolder hasPrefix:@"."])
{
continue;
}
NSLog(fileOrFolder);
NSMenuItem *documentsItem = [[NSMenuItem alloc]
initWithTitle:fileOrFolder action:@selector(launchFileOrFolder:)
keyEquivalent:@""];
NSDictionary *fileAttributes = [fileManager
fileAttributesAtPath:documentsDirectory traverseLink:NO];
if ([[fileAttributes fileType] isEqualToString:NSFileTypeDirectory])
{
[directoryEnumerator skipDescendents];
}
[documentsItem setImage:[[NSWorkspace sharedWorkspace]
iconForFile:[documentsItem path]]];
[documentsMenu addItem:documentsItem];
[documentsItem release];
}
//"theMenu" is an IBOutlet to an NSMenu with 2 items already listed.
i place documentsItem between those 2 items at index:1.
[theMenu insertItem:documentsItem atIndex:1];
//[[NSApp mainMenu] insertItem:documentsItem atIndex:1];
[documentsMenu release];
[documentsItem release];
}
- (void)launchFileOrFolder
{
[NSApp terminate];
}
-=-=-=- END CODE -=-=-=-
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden