Sync Keyboard Maestro with multiple Mac's

January 11, 2013

A tutorial on how-to automatically sync Keyboard Maestro macros with the help of Hazel and Dropbox → via @_patrickwelker

I sat down today and thought to myself that before continuing to share more Keyboard Maestro tips, it would be useful to the ones who own more than one mac to explain how to sync Keyboard Maestro across multiple machines.

The wiki on the Keyboard Maestro website already has a manual that explains how to pull it off. Still, the whole process can be a bit tricky, c.f. I had to try out some Hazel rules before I got it right. That is why I decided to publish a step-by-step tutorial with screen shots and the necessary code snippets.

You can download the macro and Hazel rule on my GitHub.

What you need:

This is how the Macro will look like:

km-sync-macro

All steps in detail:

  1. Create a folder called “Sync” in your Dropbox. It will work as a temporary storage for the Keyboard Maestro Macros1.
  2. Open Keyboard Maestro and create a new Macro with an application trigger that will fire as soon as Keyboard Maestro quits (see screen shot point 1).
  3. The next thing on the agenda is to add two If Then Else actions. We, or more precisely the macro, needs those to determine on which Mac it is running at the moment. Enter the names of your hard drives here (see screen shot point 2).2
  4. The next component will copy the Plist into our sync folder. In both If Then Else actions we need an Execute a Shell Script action. Copy the following snippet in each of those (see screen shot point 3) – in case your folder isn’t called “Sync” remember that spaces are \space in terminal paths:

     cp -rp ~/Library/Application\ Support/Keyboard\ Maestro/Keyboard\ Maestro\ Macros.plist ~/Dropbox/Sync/Keyboard\ Maestro\ Macros.plist 
    
  5. Add an Execute an AppleScript Action to the macro (again to both If-conditions). The next snippet takes care of changing the color label of the macro database file to orange on Mac #1 (here: desktop Mac) if Keyboard Maestro quits. Copy and paste the snippet into the action (see screen shot point 4a):

     tell application "Finder" to set label index of alias POSIX file "/Users/patte/Dropbox/Sync/Keyboard Maestro Macros.plist" to 1
    
  6. On Mac #2 (here: MacBook) paste this snippet (see screen shot point 4b):

     tell application "Finder" to set label index of alias POSIX file "/Users/patte/Dropbox/Sync/Keyboard Maestro Macros.plist" to 2
    
  7. Add the Dropbox “Sync” folder in Hazel (here: ~/Dropbox/Sync) and create this rule:

hazel-km-sync-rule

Copy the following snippet into the embedded script:

osascript -e 'tell application "Keyboard Maestro" to quit'
cp /Users/patte/Dropbox/Sync/Keyboard\ Maestro\ Macros.plist /Users/patte/Library/Application\ Support/Keyboard\ Maestro/Keyboard\ Maestro\ Macros.plist
osascript -e 'tell application "Finder" to set label index of alias POSIX file "/Users/patte/Dropbox/Sync/Keyboard Maestro Macros.plist" to 6'
osascript -e 'tell application "Keyboard Maestro Engine" to reload'

On your second Mac you setup the exact same rule like in step 6 but you change the color of the applied label to red – the embedded script does not change.

Side note:

You’re now all set. If you open Keyboard Maestro on your desktop Mac and close the Keyboard Maestro Editor the database will get copied to your Dropbox folder and an orange color label will be applied to the file.

On your MacBook you will have to wait until Dropbox is finished syncing before opening up Keyboard Maestro there. Also, the system notification entered in Hazel should tell you when our sync is finished.

The database – ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Macros.plist – is now synced and as a reset indicator the synced file in ~/Dropbox/Sync has a green color label.

If you open and close Keyboard Maestro on any of your Mac’s a new sync gets triggered. In short: You’ll have to wait every time for the Dropbox sync to finish before opening Keyboard Maestro on your second Mac.

In case you accidentally haven’t waited till the Dropbox synchronization is done, there’s still a copy of the most recent database in your Dropbox sync folder on that other machine.

UPDATE (2013-05-06):

Peter Lewis from Staircase Software just send me an email with an updated AppleScript which takes care of some problems. I haven’t had any issues with the setup as it is, but if you want to be on the safe side:

A user pointed out that if Keyboard Maestro editor is actually running on the target mac, when it quits it will induce a sync in the reverse direction, which is not desirable.

Changing the first script of the hazel script to:

tell application "Keyboard Maestro"
if it is running then
setMacroEnable "Sync Keyboard Maestro" without enable
quit
end if
end tell

disables the sync macro before quitting the editor, which writes the disabled script to disk only to have it written over by the hazel action’s later behaviour.

Or people could just wait until 6 is released of course, Peter.

  1. Keyboard Maestro saves all macros in this file on your system: ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Macros.plist

  2. The only premise is that the hard disks used for identifying the Mac have different names.

comments powered by Disqus

Related Posts