How To Create a Ram Drive Using Keyboard Maestro

While it may not be common on most computers, I am a fan of using a RAM drive. For my work Mac, this is where all my music plays from. I also play desktop videos such as beach scenes to create a festive atmosphere. I use the RAM drive to save wear on the spinning disk. Although I have a 3TB data drive for the purpose, there is no reason to play 8 hours of music, all day, every day, from a disk when memory is more efficient.

In order to create a RAM drive, you need a lot of RAM. Setting one up on an 8GB machine will lead to tears. Lots of RAM is one of the reasons I love these older model Mac Pros. I can allocate 6-8GB of RAM without issue.

Although it’s a rarity, when the Mac needs to reboot, such as a security update, the RAM drive disappears. After the reboot, the RAM drive has to be recreated and the files recopied. Not a big deal, but why do it manually? Another task easily handled through automation with Keyboard Maestro.

Through Keyboard Maestro, I can prompt for the size of the RAM disk, create it, then use a shell script to copy over my list of files. I use a script file for ease of editing in CodeRunner. The script can be written in the Keyboard Maestro code block.

Keyboard maestro ram drive

What’s good about this script is it shows a Prompt for User Input block, then uses that response in the Shell statement.

The value for RAM drive size is stored in the variable: `instance__Size`
This is used in the Shell Script by prefixing `$KMVAR`, as in `$KMVAR_instance__Size`

This is an effective way to build a UI in Keyboard Maestro, and combine it with the power of the Shell.

The RAM drive is created using the `diskutil` command within the `Execute Shell Script` Action.
The Volume size is calculated using – `instance__Size * 1024 * 2048`.

The complete command is shown in the `Execute Shell Script` block:

`diskutil erasevolume HFS+ “RAMDisk” `hdiutil attach -nomount ram://$KMVAR_instance__Size`

The next command in the block calls the shell script to copy over a dozen files for several hours worth of music.

A playlist file based on the contents of the RAM drive is also copied over. Double click this and the files load into VLC.

Now everything is back to the pre-reboot condition. It’s all handled behind the scenes by triggering the process with a keyword. I don’t have to open iTerm, enter the command, then run the shell script manually.

Create a ram drive using Keyboard Maestro

Maybe I should've written that in a different font.
Author Signature for Posts
0