IconForFile Size and Directory Is Empty for Populated Menu?

Discussion in 'Mac Programming' started by Darkroom, Dec 6, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i'm trying to make my iconsForFiles a smaller size than the default. how is it possible to change the size of the iconForFile(s) using setSize:NSMakeSize(16, 16), or any other way?

    also, to check to see if the Desktop directory is empty or not, i've added an array with directoryContentsAtPath, and an if statement counting the array, but it doesn't seem to work.

    here's my code:

    Code:
    - (void)awakeFromNib
           {
           NSMenu *desktopMenu = [[NSMenu alloc] initWithTitle:@""];
           NSMenuItem *desktopItem = [[NSMenuItem alloc] initWithTitle:@"Desktop Files" action:nil keyEquivalent:@""];
           [desktopItem setSubmenu:desktopMenu];
    
           NSFileManager *fileManager = [NSFileManager defaultManager];
           NSString *desktopDirectory = [@"~/Desktop" stringByExpandingTildeInPath];
           NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:desktopDirectory];
           NSString *fileOrFolder;
           NSArray *theFolderContents = [fileManager directoryContentsAtPath:desktopDirectory];
    
           if ([theFolderContents count] == 0)
                   {
                   NSLog(@"no files");
                   NSMenuItem *noFilesItem = [[NSMenuItem alloc] initWithTitle:@"No Files - Empty Directory" action:nil keyEquivalent:@""];
                   [desktopMenu addItem:noFilesItem];
                   [noFilesItem release];
                   }
                   else
                   {
                   while ((fileOrFolder = [directoryEnumerator nextObject]))
                           {
                           //ignore invisible files
                           if ([fileOrFolder hasPrefix:@"."])
                                   {
                                   continue;
                                   }
    
                           [directoryEnumerator skipDescendents];
    
                           NSMenuItem *desktopItems = [[NSMenuItem alloc] initWithTitle:fileOrFolder action:@selector(launchFileOrFolder:) keyEquivalent:@""];
                           [desktopItems setTarget:self];
                           [desktopItems setRepresentedObject:[desktopDirectory stringByAppendingPathComponent:fileOrFolder]];
    
                           [desktopItems setImage:[[NSWorkspace sharedWorkspace] iconForFile:[desktopDirectory stringByAppendingPathComponent:fileOrFolder]]];
    
                           [desktopMenu addItem:desktopItems];
                           [desktopItems release];
                           }
                   }
    
           [theMenu insertItem:desktopItem atIndex:1];
           [desktopMenu release];
           [desktopItem release];
           }
    
    - (void)launchFileOrFolder:(id)sender
           {
           NSString *filePath = [sender representedObject];
           [[NSWorkspace sharedWorkspace] openFile:filePath];
           }
    
    - (void)dealloc
           {
           [super dealloc];
           }
    
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Maybe:
    Code:
    NSImage *myIcon = [[NSWorkspace sharedWorkspace] iconForFile:[desktopDirectory stringByAppendingPathComponent:fileOrFolder]];
    [myIcon setScalesWhenResized:YES];
    NSSize mySize;
    mySize.width=16;
    mySize.height=16;
    [myIcon setSize:mySize];
    [desktopItems setImage:myIcon];
    
    This could all be stuck into a "shrinkIcon" function that takes an NSImage *, and resizes the thing to 16x16, so you don't have to have this inline.

    -Lee
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    thanks lee... i officially feel retarded after looking at your code :p... all i can say is "of course!"...

    also... i realized that the reason my array count isn't working is because it's counting the invisible files in the directory... so now i'm trying to figure out a way to count the files, count the invisible files, and subtract them. so if that total is zero there are not any files of interest in the directory.

    problem with this is i can't seem to figure out how to check the desktopDirectory for hiddenfiles using hasPrefix... here's what i have:

    Code:
    	NSArray *allFIles = [fileManager directoryContentsAtPath:desktopDirectory];
    	int allFIlesTotal = [allFIles count];
    	
    	NSArray *hiddenFIles = [fileManager directoryContentsAtPath:[desktopDirectory hasPrefix:@"."]];
    	int hiddenFIlesTotal = [hiddenFIles count];
    	
    	if ((allFIlesTotal - hiddenFIlesTotal) == 0)
    		{
    		//there are no files of interest
    		}
    
    [EDIT] i got it... had to enumerate thru the directory to find the hidden file count... yay :) i'll post the code soon.
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    i'm not sure how safe this code is, but it seems to work. i'm concerned that both NSDirectoryEnumerators might get tangled (or something)... i'm assuming that since the enumerator to count the files and determine if files are hidden will execute first because it's written first. i would prefer to break up this method into separate objects as i feel that would be more safe, but i'm still learning how to do that...

    it seems if i broke up this into separate objects it would make the code even longer since i would have to declare fileManager, desktopDirectory, etc. variables in each object... i can't really figure out how to make things static:

    Code:
    static NSString * const desktopDirectory = [@"~/Desktop" stringByExpandingTildeInPath];
    
    that doesn't work for me. neither does this, and i'm not entirely sure why:

    Code:
    static NSString * const desktopDirectory = @"~/Desktop";
    
    - (void)awakeFromNib
    	{
            [desktopDirectory stringByExpandingTildeInPath];
            }
    
    anyway, here's my code:
    Code:
    @interface AppController : NSObject
    	{
    	IBOutlet NSMenu *theMenu;
    	}
    
    - (void)launchFileOrFolder:(id)sender;
    
    @end
    
    -=-=-=-=-=-
    
    #import "AppController.h"
    
    
    @implementation AppController
    
    
    - (void)awakeFromNib
    	{
    	NSMenu *desktopMenu = [[NSMenu alloc] initWithTitle:@""];
    	NSMenuItem *desktopItem = [[NSMenuItem alloc] initWithTitle:@"Desktop Files" action:nil keyEquivalent:@""];
    	[desktopItem setSubmenu:desktopMenu];
    	
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	NSString *desktopDirectory = [@"~/Desktop" stringByExpandingTildeInPath];
    	NSDirectoryEnumerator *directoryCount = [fileManager enumeratorAtPath:desktopDirectory];
    	NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:desktopDirectory];
    	NSString *fileOrFolder;
    	
    	int hiddenFIles = 0;
    	while ((fileOrFolder = [directoryCount nextObject]))
    		{
    		if ([fileOrFolder hasPrefix:@"."])
    			{
    			hiddenFIles++;
    			}
    		}
    	NSLog(@"hiddenfiles = %d", hiddenFIles);
    	
    	NSArray *directoryContents = [fileManager directoryContentsAtPath:desktopDirectory];
    	int allFiles = [directoryContents count];
    	NSLog(@"totalfiles = %d", allFiles);
    	
    
    	if ((allFiles - hiddenFIles) == 0)
    		{
    		NSLog(@"directory contains only hidden files");
    		NSMenuItem *noFilesItem = [[NSMenuItem alloc] initWithTitle:@"Empty" action:nil keyEquivalent:@""];
    		[desktopMenu addItem:noFilesItem];
    		[noFilesItem release];
    		}
    		else
    		{
    		while ((fileOrFolder = [directoryEnumerator nextObject]))
    			{
    			//ignore invisible files
    			if ([fileOrFolder hasPrefix:@"."])
    				{
    				continue;
    				}
    				
    			[directoryEnumerator skipDescendents];
    				
    			NSMenuItem *desktopItems = [[NSMenuItem alloc] initWithTitle:fileOrFolder action:@selector(launchFileOrFolder:) keyEquivalent:@""];
    			[desktopItems setTarget:self]; 
    			[desktopItems setRepresentedObject:[desktopDirectory stringByAppendingPathComponent:fileOrFolder]];
    
    			NSImage *fileOrFolderIcons = [[NSWorkspace sharedWorkspace] iconForFile:[desktopDirectory stringByAppendingPathComponent:fileOrFolder]];
    			[fileOrFolderIcons setScalesWhenResized:YES];
    			NSSize mySize; mySize.width=16; mySize.height=16;
    			[fileOrFolderIcons setSize:mySize];
    			[desktopItems setImage:fileOrFolderIcons];				
    			 			
    			[desktopMenu addItem:desktopItems];
    			[desktopItems release];
    			}
    		}
    	[theMenu insertItem:desktopItem atIndex:1];
    	[desktopMenu release];
    	[desktopItem release];
    	}
    	
    - (void)launchFileOrFolder:(id)sender
    	{
    	NSString *filePath = [sender representedObject];
    	[[NSWorkspace sharedWorkspace] openFile:filePath];
    	}
    	
    - (void)dealloc
    	{
    	[super dealloc];
    	}
    
    @end
    
     

Share This Page