7

Does OS X have a separate temp folder for each user?

If so, how to retrieve the temp folder path for the current user programmatically?

PS Looking at my own OS X file system, I can't see such a folder.

P i
  • 29,020
  • 36
  • 159
  • 267

2 Answers2

6

Assuming you are using Objective-C and Foundation: NSTemporaryDirectory() should return an NSString with the users temporary directory. On my machine that directory is under /var/folders/.

For example:

NSString *tempDirectory = NSTemporaryDirectory();

The documentation says that NSTemporaryDirectory() returns "the path of the temporary directory for the current user. If no such directory is currently available, returns nil."

mttrb
  • 8,297
  • 3
  • 35
  • 57
2

OS X does not (or did not, through early Lion releases; 10.7.3 seems to do so) set TMPDIR for use by Unix-like scripts or programs, but many GUI programs make use of a per-user temporary directory under /var/folders which you can retrieve using some AppleScript (temporary items folder in Scripting Additions) or via NSTemporaryDirectory() as noted elsewhere.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 4
    What do you mean? `TMPDIR` is certainly set on my systems. It's the same directory as returned by `NSTemporaryDirectory()`. – Ken Thomases Apr 24 '12 at 07:59
  • 1
    Interesting; I just checked and it does seem to be set. This must be recent, though, as I have had scripts that set it to point to my external drive if it was attached (and now I know why those haven't been firing when they used to). – geekosaur Apr 24 '12 at 08:02
  • `TMPDIR` is set on a Snow Leopard (10.6.8) machine I've just checked. – mttrb Apr 24 '12 at 08:24
  • Just a thought: Is it possible that `TMPDIR` is only set in interactive shells. – mttrb Apr 24 '12 at 08:39
  • TBH I did not check on SL; I only started caring on my MacBook Air which came with Lion. I wonder then if that was a Lion regression. I also did not specifically check *after* 10.7.1, only on 10.7.[01]. – geekosaur Apr 24 '12 at 08:41
  • 2
    @mttrb, it's set for the GUI console session by launchd. Check `launchctl export`. It's present in the environment of GUI apps, too, not just shells. – Ken Thomases Apr 24 '12 at 09:57