On Dec 5, 2008, at 9:44 AM, Chunk 1978 wrote:

hi.

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.

You have to also call setTarget: on the menu items so it knows which object to send the message to. In your case you'd probably pass self as the object. Also if you want to access the path again during your selector it would probably be easiest to set the path for the item as the representedObject.

NSMenuItem *item = /* your item with @selector */
[item setTarget:self];
[item setRepresentedObject:[documentsDirectory stringByAppendingPathComponent:fileOrFolder]];

Then in your method:

- (void)actionMethod:(id)sender {
        id fullPath = [sender representedObject];

        /* do something with fullPath */
}

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.

You'll have to do some refactoring to accomplish this. Essentially, break apart your method into one that creates a menu for a directory's items and returns the resulting menu. That method will call itself to create menus for subfolders which you would add as subitems to your menu. You would then just call that method on your root directory, Documents in your case, and add the resulting tree of menus wherever you're interested in.

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.

I believe this is a consequence of how you're setting the title and where you're adding the menu. If you set the title on the documentsItem as well as the documentsMenu it should appear regardless of where you attach the menu.

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.

You can get the icon for the path with this:

[[NSWorkspace sharedWorkspace] iconForFile:[documentsDirectory stringByAppendingPathComponent:fileOrFolder]];


Ashley

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to