# Persistent RAMDisk

Works on macOS Big Sur.

[Reference](https://apple.stackexchange.com/a/345818)

## Persistence

There is a non-deprecated way to do it:

- Open the *Script Editor* app.
- Paste

```AppleScript
do shell script "~/.login"

on quit
	do shell script "~/.logout"
	continue quit
end quit
```

- `File > Save`, Select `Application` and `Stay open after run handler` and *Save*
- `System Preferences > Users & Groups > Login Items tab > + button`
- Add the application and mark it as *Hidden*

To hide it from the Dock:

- Right-click the application and select *Show package contents*
- Open `Info.plist` and add

```XML
    <key>NSUIElement</key>
    <string>1</string>
```

## Create RAMDisk scripts

```shell
$ touch ~/.login
$ touch ~/.logout
$ chmod +x ~/.login
$ chmod +x ~/.logout
```

Open `~/.login` and paste, this creates a 1GB RAMDisk

```shell
#!/usr/bin/env bash
diskutil erasevolume HFS+ "RAMDisk" `hdiutil attach -nomount ram://2097152` && rsync -rauL --ignore-errors ~/.RAMDisk/ /Volumes/RAMDisk
```

Open `~/.logout` and paste

```shell
#!/usr/bin/env bash
rsync -rauL --delete --ignore-errors /Volumes/RAMDisk/ ~/.RAMDisk
```

- Mount the RAMDisk `$ ~/.login`
- The RAMDisk should appear in *Finder*.

## Example use: PHPStorm caches on RAMDisk

1. Create a folder called *PHPStorm* on the RAMDisk.
2. Start `PHPStorm > Help > Edit custom properties...` and paste

```INI
idea.system.path=/Volumes/RAMDisk/PHPStorm
```