Automatically Copy Files To A Flash Drive Using Keyboard Maestro

On previous episodes of Mac Power Users, I've made reference to my Tuff 'n Tiny flash drive that I keep on my keychain with copies of my "mission critical" files. I reviewed the Tuff 'n Tiny drive on my site last year. Previously I partitioned the drive into two sections to use both with a PC and a Mac (described in this earlier post) so I could use one partition as a bootable drive for my Mac keeping the other a more commonly formatted FAT drive for PCs. Some have recommended that I use the more universal exFAT format instead.

On this drive I keep a copy of my 1Password keychain, a secure disk image, and other important files. I want these files to be updated regularly from my Mac to my flash drive so I've setup a Keyboard Maestro Macro to update the contents of the flash drive every time it's plugged into my Mac. (Learn more about Keyboard Maestro on episode 145 of MPU).

Here's how it's done:

Identify The Volume

Keyboard Mestro version 6 can be triggered by a USB device but to do this we need to know the name of the volume. With the flash drive mounted, launch disk utility and look for the disk description. This will later be used to trigger your Keyboard Maestro Macro. Here you'll see this device is identified as "Verbatim STORE N GO Media."

Identify The Volume

Setup a Macro Trigger

Setup a new Keyboard Maestro Macro with a USB trigger so it activates whenever a USB device matching your flash drive is attached. Here I've just used a trigger with a name containing "Store N Go" since I only have one of this particular type of flash drive.

Setup a Macro Trigger

Create a Secure Disk Image on the Drive

This step is optional, but if you want the information on the flash drive protected by an additional layer of security, you'll want to create a secure sparse bundle disk image on the drive. A sparse bundle uses only as much space as is necessary to store the data inside the bundle and will grow up to the size you specify. That's why I prefer a sparse bundle over a traditional disk image, though either will work.

Create your disk image using Disk Utility by choosing New > Blank Disk Image.

Now specify a name, size and encryption for your image and the location where you want your image to be saved.

If you need more detailed instructions on creating Secure Disk Images, check out this Screencast from MacSparky. It's a bit dated, but still mostly applies.

Create a Secure Disk Image on the Drive

Create a Password for Your Disk Image

This is where you'll need to create a password for your disk image. You can optionally save this password in your Mac's Keychain for easy access. But think about this. If your password is stored in the keychain, the process of automatically updating your files will be faster and require less user interaction because OS X will automatically unlock the disk image and mount it so long as your keychain is unlocked. However your files will be accessible to anyone who has access to your computer if the keychain is unlocked (which generally occurs at login.)

Create a Password for Your Disk Image

Set Keyboard Maestro to Launch the Disk Image

The first step of your Keyboard Maestro Macro is to launch the disk image you just created when you insert the flash drive into your computer. This can be done using the "open" command in Keyboard Maestro and pointing to the location of the disk Image on the flash drive. Note if you did not have the Finder remember your password in the last step, you will be prompted to enter it.

I've additionally added a five second pause before executing the first step. I find that sometimes it takes a few seconds for my computer to recognize the flash drive has been mounted and adding the additional five second buffer keeps the Macro from timing out. Depending on the speed of your computer and flash drive, you may or may not need the additional buffer.

Set Keyboard Maestro to Launch the Disk Image

Copy Files to the Disk Image

Next you'll want to copy files from your computer into the disk image. Or, if you didn't create a disk image, you you would skip the previous steps and copy the files directly to the flash drive.

In this example, I've set Keyboard Maestro to copy my 1Password keychain, though you can copy as many files as you like. Simply repeat this step for the number of files you want to copy.

Again, I've added a 5 second pause before and after each file to allow for processing time, though you may find more or less time is needed based on the size of the file and your computer's processing power. It may take some trial and error when creating the Macro to get the timing down.

Copy Files to the Disk Image

Eject the Disk Image

After all your copy jobs have completed, you'll want to eject the disk image (or possibly the flash drive itself). To do this I use an AppleScript with the following code. You'll want to replace DISK NAME with the name of the disk you want to eject.

set diskName to "DISK NAME"
tell application "Finder"
    if disk diskName exists then
        eject disk diskName
    else
        tell current application
            set deviceLine to (do shell script "diskutil list | grep \"" & diskName & "\" | awk '{ print $NF }' }'")
            if deviceLine = "" then
                display dialog "The disk \"" & diskName & "\" cannot be found." buttons {"OK"} default button 1 with title "Error" with icon caution
            end if
            set foundDisks to paragraphs of deviceLine
            repeat with i from 1 to number of items in foundDisks
                set this_item to item i of foundDisks
                if this_item contains "disk" then
                    do shell script "diskutil mountDisk /dev/" & this_item
                end if
            end repeat
        end tell
    end if
end tell

Eject the Disk Image

All Done

You've successfully setup the Keyboard Maestro Macro. Now you can customize the Macro to meet your needs. To keep your files up to date you'll need to remember to plug in your flash drive on a regular basis. I try to make a habit of this by doing it every Sunday morning. Or you can just set a reminder in your task management system.