How can I create a file next to my Application?

You have a file that is auto-generated by your application, and you want to put it next to your application, on the user's "Desktop" folder, into the "Documents" folder or some similar place?

Here are four reasons why you shouldn't do this, and four suggestions for solutions.
You have a file that is auto-generated by your application, and you want to put it next to your application, on the user's "Desktop" folder, into the "Documents" folder or some similar place?

Here are four reasons why you shouldn't do this, and four suggestions for solutions.

1) Only Admin users have write access to the "Applications" folder. So, if your app has been installed there and your app tries to create a file there, it will fail. Similarly, users usually don't have write access to file servers, so if your application resides in a folder on a file server, it will fail as well.

2) Users do not like others cluttering their folders. The "Applications" folder is intended for storing Applications. You'll mess up the entire orderly structure of the user's files if you insist on writing your log file there.

3) The desktop folder is for important files that the user wants to have displayed on their Desktop. Usually, users will not want any files to reside there, apart from the files they're working with very frequently. Other users use DragThing or a similar launcher, and prefer their files there instead of on the desktop. You'll annoy them by repeatedly putting your file there.

4) According to Apple, the "Documents" folder is intended as a default location for users to save their files. It is only intended for files the user explicitly saves using a "Save File" dialog, and which the user may want to explicitly open by doble-clicking it in the Finder. So, keep any files your app wants to save somwehere else, even if AppleWorks still does so for historical reasons.

So, where do you put the file? Several options are available, most of which involve using FSFindFolder() or NSBundle methods for getting at the proper folder:

1) Ask the user where they want to put them, using an "open file" dialog (Navigation Services, NSSavePanel, ...). You can choose one of the other options below as a default, and remember the last location so you don't have to ask the user each time, but this is by far the best option, since the user can set things up the way she likes it. This is the *only* good option you have for files that the user will want to create explicitly, like documents of a Word processing program.

2) Use a folder in the "Library" folder, or rather, create such a folder in the User's "Library" folder. There are various folders for all sorts of files your application needs that the user doesn't have to see: for Preferences, Logs, Caches, Sounds, Cookies, Documentation, Indexes, Plug-ins etc. For things like Logs, which your users will want to view occasionally, provide a special GUI for this purpose in your application, e.g. a menu item that opens the log file in Console.app, or whatever...

3) Create your own subfolder with your application's name in the "Application Support" subfolder of the user's "Library" folder. This is a catch-all location for any support files your application may need to create that don't fit in the other locations. But again, this is only for files the user doesn't have to see, or which only advanced "power users" may wish to manipulate.

4) If you still need to create another file next to your application, use NSBundle or CFBundle to get an NSString or FSRef to your application bundle's package folder.