Using Hazel to keep my documents on the cloud

I recently started using Hazel. Hazel is a macOS app for managing your files.

Today, cloud providers like Apple, Google, Microsoft and Dropbox offer a decent amount of space for very cheap. For example, most of them provide 200GB for just ~3£ per month. There is no excuse, therefore, not to upgrade to such plan and start storing your documents there. This way we can have a backup of our data on the cloud, and also anywhere access to our data.

I wanted to achieve the following workflow:

  1. Keep all my documents (which they are always under ~/Documents) on the cloud so I can access them from any device.
  2. Keep symbolic links of those files from the cloud to my local ~/Documents directory so I can easily access the data from the terminal.

Some conventions before we start: local directory refers to the directory on the computer that is not on the cloud (in this specific case will be ~/Documents). remote directory refers to the directory on the cloud that holds the files of the local directory and then creates symbolic links of its files to the local directory.

Rules for the local directory

Add the local folder you want to sync (i.e in my case is ~/Documents) in Hazel’s Folders section. In this folder we need to define two rules:

  1. Move any document from that local directory to the remote directory.
  2. Delete any documents/aliases from the local directory if they do not exist on the remote directory.
Local directory's rules Figure 1: Local directory's rules

Local Directory, Rule #1: Move files from local directory to remote directory

For the first rule we need to move files from the local directory to the remote directory:

Move local data to remote directory Figure 2: Local directory's 1st rule: Move local data to the remote directory.

The embedded script has the following code:

if [[ ! -L "$1" ]] # If the file is not a symbolic link.
then
	exit 0
else
	exit 1
fi

At some point, we might delete some files from the remote directory and we need a way to also wipe the symbolic links of those files from the local directory. Unfortunately, there is no “delete-file” trigger for Hazel so we had to find a hack around it.

Delete daed symbolic links Figure 3: Local directory's 2nd rule: Delete dead symbolic links.

The embedded script for the second rule is:

fname=$(basename "$1")

if [ ! -e "/Users/rafael/Library/Mobile Documents/com~apple~CloudDocs/documents/$fname" ]; then
	rm -rf $1
fi

Here we are basically saying the following: “If the file/directory currently being processed by Hazel does not exist on the remote directory, then delete the symbolic link from the local directory.”

So in this rule, we are constantly verifying whether what we have in our local directory (which will always be a symbolic link to the remote directory) actually exist on the remote directory. If not, then this means that the file has been deleted from the remote directory and hence we should delete the symbolic link from the local directory.

Rules for the remote directory

For the remote directory we have only one rule:

Create symbolic links Figure 3: Remote directory's 1st rule: Create symbolic links to the local directory.

The embedded script is:

ln -s "$1" /Users/rafael/Documents/

We are basically creating a symbolic link for every document in the remote directory to the local directory.

Note that Hazel provides a way to create aliases but these are Finder aliases which they are not a UNIX concept and hence won’t work with the terminal. Therefore, in here we are creating a symbolic link instead that has the advantage that you can access the directory/file pointed by the symbolic link directly from the terminal and the finder.

© 2019 Rafael Papallas
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.