Searching Within .app For Files

Posts: 1,066
Joined: 2004.07
Post: #1
Currently I'm loading my images with the standard code:
NSImage myImage = [NSImage imageNamed:@"File.png"];
This works fantastically if File.png is right in the resources folder in my .app package. What if I add a bunch of folders in there by dragging a big folder tree into my app and setting it to create references for each folder? How can I get the program to find File.png without having to use something like
NSImage myImage = [NSImage imageNamed:@"Folder1/Folder2/Folder3/Folder4/File.png"];
I tried just leaving it but I don't get anything. Basically I don't want to have to worry about what folder an image is in. I just want my app to find it and load it. Any advice? This would help greatly.
Quote this message in a reply
Posts: 567
Joined: 2004.07
Post: #2
PHP Code:
system("find | grep filename"); 

It's not magic, it's Ruby.
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #3
Nayr Wrote:
PHP Code:
system("find | grep filename"); 

Nayr: Try running that code and see what it actually does. Wink

Nick: The feature you're looking for doesn't really exist. As far as Cocoa knows, you could have a file with the same name in every one of your subdirectories. You're going to have to specify which subdirectory the file is in. It doesn't have to be part of the filename; take a look at NSBundle's -pathForResource:ofType:inDirectory:.

Having a nice, well-organized directory structure in your Resources folder isn't really necessary, as it's not visible to the user unless they go poking inside your bundle. I'd suggest just sticking it all in one directory, or a few subdirectories if it makes sense.

- Alex Diener
Quote this message in a reply
Posts: 776
Joined: 2003.04
Post: #4
Nick Wrote:Basically I don't want to have to worry about what folder an image is in. I just want my app to find it and load it.

...And I thought users would be the ones getting spoiled by Spotlight, I guess I was wrong! Rasp
Quote this message in a reply
Posts: 3,591
Joined: 2003.06
Post: #5
I haven't tried this code but I think it should work for what you're trying to do Nick. Like ThemsAllTook said, there isn't anything currently available that will do exactly what you're asking for. You'll have to specify the directory.
NSString    *path;
NSImage    *myImage;

path = [[NSBundle mainBundle] bundlePath];
path = [path stringByAppendingPathComponent:@"Contents/Resources/Folder1"];
myImage = [[NSImage alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"File.png"]];
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Searching for vertexes... mikey 20 12,358 Nov 10, 2009 10:37 AM
Last Post: mikey
  Searching Headers & Documentation OneSadCookie 0 3,112 May 1, 2006 11:31 PM
Last Post: OneSadCookie