Getting path of a folder in Cocoa application - macos

Is there any way of getting folder path (any folder and can be at any location in mac) programatically, in a native cocoa application?

What exactly are you trying to achieve? Did you have a look at NSFileManager? To let the user choose a directory have a look at NSOpenPanel.

Related

Mac OSX: How to associate a directory as bundle

I am writing an app for Maverick.
The app creates a folder under /user/document, named "folder.db".
All the user related files will be in a folder "folder.db".
I would like to associate my app with "folder.db" directly, so that clicking on it would open my app and not the Finder.
How to achieve that?
Note: I tried to play with the UTI settings in xcode but not luck...
First .db is generally used for databases. So probably not a good idea. What you are looking for is a package or bundle in Cocoa terms. In Cocoa you want to look for the fileWrapper methods. Those create package/bundle files that are folders with a special flag bit set to make it act like an opaque file in Finder
You might want to study NSWorkSpace, NSFileManager, NSBundle, NSDocument and NSOpenPanel and NSSavePanel.
Those will get you on the path.

(Mac) Cocoa app change core dump location

If my application crashes i wish for the coredump file to be stored in a particular directory. It is possible to change the location from the command line but i wish my application to change this path programatically and if possible only for a single (its self) application. Is this possible?
No it's not. But if you're not sandboxed, you can look at the files in ~/Library/Logs/DiagnosticLogs and move them somewhere else the next time you're launched.

Add mouse right click option in Mac OS X like windows

I have requirement to add mouse right click option in folder or file list like windows explorer. Requirement is like below:
"There are some files with particular extension showed in Finder/folder list in Mac OS X. When user click on file with that particular extension I need to show my .exe or .app name which can open that file".
I want to know how to achieve this functionality in Mac OS X. In windows it is game of registry but I am not sure how to achieve same in Mac OS X.
You get this in the Info.plist file in your app bundle by specifying file extensions your application can open in the CFBundleDocumentTypes array.
http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-101685-TPXREF107
I tried that option. It is working fine and I am getting my application icon with that extension file and also my application is there in Default handler. But now issue is that this application I have created thru Qt and in main function of my application I am getting only application path in argv and argc and not the selected file. So let me know how to achieve this. If more information is required then please let me know. Thank you for valueable answer

how to detect files related to an app on a mac

I am wondering how an application like appzapper can detect the files related to an application?
All ideas are welcome.
Thanks for your help,
Regards,
By looking into the standard locations that usually are used by applications to store preferences (like Library/Preferences/ , Library/Caches, etc., both in your home directory ~/ and under / ) I couldn´t find any file or folder that isn´t using either the applications name (eg. ~/Library/Application Support/Firefox) or it´s bundle identifier (eg. ~/Library/Preferences/org.mozilla.firefox.plist) in its name; so I guess they´re just using that.
In the case of AppZapper you can go ahead and try for any (not currently running) Application to rename the ~/Library/Application Support/AppName folder for example to ~/Library/Application Support/111AppName - AppZapper doesn´t find it anymore. If you change it to ~/Library/Application Support/AppName111 it shows up as a related file.
Out of this you can conclude that there isn´t any magical linkage between a folder full of preferences and it´s associated application that AppZapper is reading out, it´s merely checking for folders starting with the Application´s Name.

Preferred path to applications on OSX?

I want to be able to run a text editor from my app, as given by the user in the TEXT_EDITOR environment variable. Now, assuming there is nothing in that variable, I want to default to the TextEdit program that ships with OSX. Is it kosher to hardcode /Applications/TextEdit.app/Contents/MacOS/TextEdit into my app, or is there a better way to call the program?
Edit: For the record, I am limited to running a specific application path, in C. I'm not opening a path to a text file.
Edit 2: Seriously people, I'm not opening a file here. I'm asking about an application path for a reason.
In your second edit it makes it sound like you just want to get the path to TextEdit, this can be done easily by using NSWorkspace method absolutePathForAppBundleWithIdentifier:
NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:#"com.apple.TextEdit"];
Mac OS X has a mechanism called "uniform type identifiers" that it uses to track associations between data types and applications that can handle them. The subsystem that manages this is Launch Services. You can do one of two things:
If you have a file with a reasonably well-known path extension, e.g. .txt, you can just ask NSWorkspace to open the file in the appropriate application.
If you don't have a well-known path extension, but you know the type of data, you can ask Launch Services to look up the default application for that type, and then ask NSWorkspace to open the file in that specific application.
If you do it this way you'll get the same behavior as the Finder, and you won't have to fork()/exec() or use system() just to open a file.
I believe hardcoding "Applications" will not work if the user's language setting is not English. For example in Norsk the "Applications" folder is named "Programmer".
The Apple document on internationalization is here. Starting on page 45 is a section on handling localized path names.
I believe that Mac OS X provides a default application mechanism, so that .txt will open in TextEdit.app or Emacs or GVim or whatever the user has specified. I couldn't find anything online however.
You could run following command from your application:
open <full path to text file>
This will open the text file in the default text editor. You can open any file type using open command.

Resources