Is it OK to use the /tmp folder?

I'm working on a set of macros that need to create a temporary file. Is it OK to use the /tmp folder? I read somewhere that if I do, it's a good idea to create a subfolder.


Not really answer your question, but I've been using the temp folder for a while.

On my Mac, it's /var/folders/vl/x6z6g6y54gvg60cxn85jgnj80000gn/T/.

I don't know if it would be different for other macs. I use this to get the path:

1 Like

If you were in an public environment where multiple people used the same Mac and someone (potentially malicious) might be messing with or monitoring the /tmp/ folder's contents, then it would be advisable not to use it.

However, for a solo Mac user who isn't dealing with government secrets or similar, using /tmp/ is fine.

If you are using a shell script, you can use $TMPDIR instead and get a temporary folder which is only accessible to you, and is therefore technically more secure, but again, for most people, /tmp/ will be sufficient.


Thanks. It's just an HTML script for use in a KM macro, so I don't think anyone will be knocking down my door because of it. :grinning_face_with_smiling_eyes:

As long as persistence is not a value that you need.

(My understanding is that files unaccessed for 3 days are automatically pruned out)

I personally do this kind of thing:

// writeTempFile :: String -> String -> IO FilePath
const writeTempFile = template =>
    // File name template -> string data -> IO temporary path
    txt => {
            fp = ObjC.unwrap($.NSTemporaryDirectory()) +
            takeBaseName(template) + Math.random()
            .substring(3) + takeExtension(template);

        return (writeFile(fp)(txt), fp);

Hey Guys,

Don't forget that you can do something like this:




Yes, but better is to use the mktemp tool to create a temporary directory for you.

mktemp -d

will return a newly created temporary directory which you can use and then delete.

Personally, I would add an Assert action after the command and abort the macro if the result does not start with “/var/folders/” since you likely will want to delete the folder later and that will ensure it is in the right place.