get current path of the .app file being executed - objective-c

How can I get the current path of my .app which is being executed in cocoa mac application?
I tried using NSTask to do pwd but returns me /

I found it by using [[NSBundle mainBundle] bundlePath]

Related

how to access a folder present inside project in cocoa app

I have a folder named 'Images' present in my project .The path to that folder is '/Users/username/Documents/Test/ProjectName/Images'.
How to access the images folder in cocoa app.
Thanks
If you really need to work with files and folders in bundle of your app, you could access resources folder with call:
NSURL *resourceURL [[NSBundle mainBundle] resourceURL];
Than you could work with resourceURL, reading content of folder with help of NSFileManager, as you do with regular folders and files on disk.
But that's barely needed. Usually you take resource from bundle by file URL which you obtain with call like:
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:#"user agreement" withExtension:#"rtf"];
Have a look in documentation about other variants to take paths for resources from app bundle.
You can use directly by this
NSButton *closeButton = [[NSButton alloc]initWithFrame:CGRectMake(185,215 , 15, 15)];
[closeButton setImage:[NSImage imageNamed:#"close.png"]];
the close image is now displaying in the button..
this may help you...

How do I use files that I dropped into Xcode in a Mac OS X Cocoa App when running the application?

I'm creating a Cocoa Mac OS X app to install printers using a NSTask and the lpadmin command. I want to be able to use the .ppd files that are bundled with my application, e.g.
lpadmin -p name -E -v lpd://printer-location.com/public hp-laserjet-9050.ppd -L place
How can I do this where the (.ppd) file is located in my application bundle? Should I just write the file to the current directory, install, and then delete it?
Add File.ppd to the "Supporting Files" folder (Xcode 4 screenshot):
Run lpadmin through NSTask using the following file path:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"File" ofType:#"ppd" inDirectory:nil];

Finding path to plist in Xcode project folder

I have a plist in my Xcode project's resource folder but on physical drive it's in the top folder of my project. I want to read and write from this same file. Using NSBundle reads it properly and gives no problem. I'm using this code to read file path:
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"plist"];
but i think NSBundle doesn't allow writing back and so it didn't work when i tried to write back the file. Using the following code creates/ updates file using application path
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
NSString * docsDirectory = [pathArray objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:#"MyFile.plist"];
but for now, I need to write on internal file. Any solution to access it? Besides, I've tried giving path from my working directory to project directory. It also doesn't work.
It's not NSBundle that doesn't support writing, it's iOS itself. iOS will not let your app write to its own bundle.

How do I determine which path my application is run from?

I'm creating a little program to copy the Domain file for iWeb over to a USB stick or external harddrive or such. The program is meant to be run from that USB stick or external harddrive, and then create a directory where the application is run from. E.g. the application is run from ~/Documents, the application should create a folder at ~/Documents/(account name)'s website, and then copy the Domain file to that folder. But when I try to run the application from a USB stick, it creates a folder under /, called /(account name)'s website. How do I fix this?
If you want the current working directory of your app then use NSFileManager's currentDirectoryPath.
NSString *currentPath = [[NSFileManager defaultManager] currentDirectoryPath];
NSBundle has an instance method called bundlePath which will almost get you what you want.
NSString *bundleParentPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
This should return the directory that the application is being run from.

pathForResource inDirectory problem

i want to load html file from Resources in WebView.
in Resources i have:
test.html
testfolder->test.html
this code works perfectly:
[[webview1 mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]]]];
and this one - crash the app (SIGABRT):
[[webview1 mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html" inDirectory:#"testfolder" ]]]];
How can i get files from folders?
You're going about it the right way, so that means either you've got a typo in your file/directory name or the "testfolder" directory isn't getting included in your application bundle.
Make sure that "testfolder" is in the Resources section of your XCode project as a folder (not a group).
Look at the "Build Results" window when you build your application; you should see steps in there that say "copy test.html" and "copy testfolder".
Note that (when building for the simulator) you can also examine the contents of the application bundle directly -- look in Library/Application Support/iPhone Simulator/User/Applications
Documentation for NSURL fileURLWithPath: says
Parameters
path
The path that the NSURL object will represent. path should be a valid
system path. If path begins with a
tilde, it must first be expanded with
stringByExpandingTildeInPath.
Passing nil for this parameter produces an exception.
and for NSBundle pathForResource:ofType:inDirectory:
Return Value
The full pathname for the resource
file or nil if the file could not be
located.
Could it be that the file is not in the directory path that you specify?
Creating a "Group" in XCode doesn't create a real Directory within your app Bundle.
You need to import the directory into your app using the following method in your project "Test":
File -> Select "Add Files To Test"
Select the Directory you want to import
In the pop-up window make sure you select "Copy items into destination group's folder" and "Create Folder References for any added folders"
Hit "Add"
Your done!
The Directory should appear blue instead of yellow.
Your code looks correct.
Are you sure your folder testfolder is included in your build?
If not, your call to pathForResource: ofType: inDirectory: will return nil.
And according to the documentation of the NSURL class, passing nil for the parameter of the fileURLWithPath: class method produces an exception.
To check that your folder is included in your build process, expand the 'Copy Bundle Resources' build phase, and look for your folder. If it is not there, drag and drop it into it.
A way to check that you added it correctly is to navigate to your builded application, right click on it and select the "Show Package Contents" item.
If you want to load from a directory, you first gotta add the directory to the project with the create directory references (so that in Copy Bundle Resources you get a blue icon with the main folder name and not individual files!)
Don't use the in directory parameter at all. Just use the global path with slashes ("/") instead of backslashes ("\") like in Windows.