How to find the residing directory of a OS X application package - programmatically

ios file path example
ios get file path from documents directory
ios app sandbox directories
swift filemanager example
nssearchpathfordirectoriesindomains
get file from document directory ios swift
nsapplicationsupportdirectory
swift file browser

I have a OS X application as a .app package which can reside in any arbitrary location in the filesystem. Is there a way to find the current path of the package programmatically from with.

NOTE

getcwd() doesn't work since the current directory of all packages opened is via click-open or using the open command is "/" . If i invoke the executable from myapp.app/Contents/MacOS , the directory is shown correctly. It is a cocoa application.

Any clues?

You should be able to find the location of the .app by [[NSBundle mainBundle] bundleURL]; or [[NSBundle mainBundle] bundlePath];.

However, you probably want to access resources inside the .app by going through NSBundle to locate them in most cases (this deals with issues like localization).

Note: If you are planning to modify the application bundle; don't. You should not, can not and must not assume that you have write access to the application bundle; and if you do, something is likely very wrong.

Edit: As a point of interest, if you are writing a C program on OS X and need to find the location of the executable; you can use a non-portable main declaration like so:

int main (int argc, const char *argv[], const char *env[], const char *path[]) {
  // path[0] now contains the path to the executable
  // NOT PORTABLE! OS X ONLY!
  return 0;
}

Enterprise Mac Security: Mac OS X, I have a OS X application as a .app package which can reside in any arbitrary Is there a way to find the current path of the package programmatically from with. This chapter is important for all non-command-line developers, whether your application is an end-user commercial suite or an open source tool. Bundles vs. Installers. Most applications in OS X do not need to use an installer. To make installation and removal easy, OS X provides bundles. A bundle is basically a directory that contains an

Building off the argv[0] version above, you can also get the path from:

    NSArray* arguments = [[NSProcessInfo processInfo] arguments];

    NSString* exe = [args objectAtIndex:0];

from init or applicationDidFinishLaunching

Accessing Files and Directories, Directory. Services. Accounts in OS X client can come from a variety of locations. But by default, they all reside in what is known as a local directory service. Examine the .plist file for the user created earlier and look for the key named which is a complex programmatically generated string of characters that are never  Depending on your needs there are a couple of ways to get the current working directory in a Scala application. 1) Using System.getProperty. The most obvious/direct approach is to use the Java System.getProperty method, passing in "user.dir" as a parameter:

The bundle directory might be a problem with command line tools. So i use the following function which uses the mach-0 linker function to resolve shared libraries.

 static char* find_executable_filepath () noexcept 
 {
     char path[1];
     uint32_t size = 1;
     if (_NSGetExecutablePath(path, &size) == 0) return nullptr;
     char* res = (char*)malloc(size+1);
     if (_NSGetExecutablePath(res, &size) != 0) return nullptr;
     char* r = realpath(res, nullptr);
     free(res);
     return r; 
 }

Enterprise Mac Security: Mac OS X Snow Leopard, Accessing Files and Directories. Before you can open a file, you first have to locate it in the file system. The system frameworks provide many  OS X includes autoconf in the BSD tools package. Beyond these basics, if the project does not build, you may need to modify your makefile using some of the tips provided in the following sections. After you do that, more extensive refactoring may be required.

Managing Files and Directories, Users accounts in Mac OS X client can come from a variety of locations. But by default, they all reside in what is known as a local directory service. Examine the .plist file for the user created earlier and look for the key called which is a complex programmatically-generated string of characters that are never duplicated. The os.path.expanduser() function will expand a pathname that uses ~ to represent the current user's home directory. This works on any platform where users have a home directory, including Linux, Mac OS X, and Windows. The returned path does not have a trailing slash, but the os.path.join() function doesn't mind.

The Advanced iOS 6 Developer's Cookbook, Managing Files and Directories. Some of the most basic operations involving files and directories are creating them and moving them around  Here is a handy Java class that use System.getProperty("os.name") to detect which type of operating system (OS) you are using now. This code can detect “Windows”, “Mac”, “Unix” and “Solaris”.

Title List of Documents Made Publicly Available, Apple intends you to use them as provided, with little or no customization from the developer. As you see in this chapter, these classes interact with the underlying Address Book in enables you to query, modify, and add contacts from your applications. The iOS contact data resides in the user's home Library folder. Create a new directory for the project. Since our project is a timer, we’ll simply name the folder phptimer. To do this, execute the following command: mkdir phptimer; Enter the newly created directory: cd phptimer; Find a package or library for the project.

Comments
  • Works very well. Thank you very much!
  • @Deepak Narayan: I have to restate my two ancillary points: Don't assume that you can modify the application bundle, and access resources through NSBundle's -urlForResource:ofKind: and friends.
  • Thanks. I am not looking to modify the bundle, i just need the directory path to change directory and load some files from the directory where the .app is.
  • You should strive to make your answers self-contained; other answers may be edited, deleted, or simply move around so that it's no longer "above" yours.