Programmatically discover the name of the .DMG - objective-c

say I have a .DMG
I click it to open it
there's an app inside.
I run the app (which I develop)
Is there a programatic way for the executable to find out the name of the .DMG it's running from at runtime (if any)? perhaps a "Get full path" which will include the name of that DMG? There are certain keywords on that .DMG name that may exist and if they do, I need to act upon them.

[[NSBundle mainBundle] bundlePath]; //Or Bundle URL, depends if you prefer NSString or NSURL.

Related

How to create and use bundles in cocoa?

I have an application that I wrote that uses an external unix executable file. It needs to run this file in order to get some data needed for further processes.
Right now this executable is located in my project folder and in order to use it in my app I have hardcoded paths to it (which is bad).
I've heard that to avoid this hardcoded paths issue it's possible to use bundles.
Can anyone explain me if this is the best way to achieve what I want, and direct me how to do it if so?!
I already looked through similar questions on stackoverflow and went through this:
https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html#//apple_ref/doc/uid/10000123i
it didn't really help me so far...
You use the NSBundle object to locate your executable. You start by getting your application's bundle using [NSBundle mainBundle]. Depending on where you've placed your Unix tool, you can use NSFundle's pathForAuxiliaryExecutable: or pathForResource:ofType: to locate your executable.
For example, if your Unix tool is in your Application bundle's Resources folder, you could do the following:
NSString* toolPath = [[NSBundle mainBunble] pathForResource:#"toolname" ofType:nil]

Using NSBundle to load my resources

I wanted to separate my resources, nib files and localization files into a common reusable bundle. And so I created a bundle for my ios application and specified resources to be included inside the bundle using build phases, copy bundle resources. But, now if I try to load the bundle, I am not able to load the bundle. I try using [NSBundle allBundles] and the array shows only the main apps bundle.
I also tried to enumerate the directory for NSApplicationPath but again the only bundle available is my application default bundle. I wanted to learn this technique and make use of it to separate my resources. Any help and suggestions would be greatly appreciated. Thank you
[NSBundle bundleWithPath:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] bundlePath], pathToYourBundleWithinTheDotAppDirectory];
Let me know how you get on.
Try something like this:
NSBundle* bundle=[NSBundle bundleWithIdentifier: #"bundle name"];
And make sure that you have selected these options when you have dragged the bundle to the project:
For projects with one bundle, I use:
// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"];
What makes this so convenient is that when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the #"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.

Reveal bundle contents in Mountain Lion using ObjC

How can I reveal a bundle contents in Mountain Lion using [NSWorkspace selectFile:nil inFileViewerRootedAtPath:pathEndingWithDotBundle]? I mean: pathEndingWithDotBundle is a path whose basename is something like "folder.bundle". If I call this method this way, the Terminal opens up and I don't know why...
It opens Terminal because it thinks you want to launch the bundle, and that's the default application. I would open a bugreport against this, because the documentation does not say that it will open the path. It says it that it will display it in a file viewer. It would be reasonable for this to be an error (since a bundle is not logically a directory; it's just phyiscally a directory). But it makes no sense for it to do something random like try to launch another program.
That said, it's fairly easy to work around. Just select the Contents folder, which is required to be within the bundle:
[[NSWorkspace sharedWorkspace] selectFile:[pathEndingWithDotBundle stringByAppendingPathComponent:#"Contents"]
inFileViewerRootedAtPath:pathEndingWithDotBundle];
In 10.6+, you can use activateFileViewerSelectingURLs:
NSURL *URL = [NSURL fileURLWithPath:[pathEndingWithDotBundle stringByAppendingPathComponent:#"Contents"]];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:#[URL]];

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.