Jurriaan's Blog

Maid as a Daemon on OS X

What is Maid?

According to the project’s README:

Maid keeps files from sitting around too long, untouched. Many of the downloads and temporary files you collect can easily be categorized and handled appropriately by rules you define. Let the maid in your computer take care of the easy stuff, so you can spend more of your time on what matters.

An example: Automatically uploading screenshots

A while ago I contributed watch and repeat functionality that is available in the just released v0.6. This means that Maid can now listen to filesystem changes (like Hazel), and run tasks periodically like cron. This is realized using the Listen and Rufus-Scheduler gems.

This functionality can be used to do things like this (an exerpt from my rules.rb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Maid.rules do
  repeat '1h' do
    rule 'Remove old screenshots' do
      dir('~/Desktop/Screen Shot *').each do |path|
        if 2.hours.since?(accessed_at(path))
          move(path, '~/Desktop/Old/')
        end
      end
    end
  end

  watch('~/Desktop') do
    rule 'Move screenshots to server' do |modified, added, removed|
      added.select {|f| f =~ /Desktop\/Screen Shot.*\.png$/ }.each do |file|
        filename = upload_screenshot(file)
        system "terminal-notifier -message '#{filename}' -open '#{filename}' "\
               "-title '💻  Screenshot uploaded'"
        system "echo '#{filename}' | pbcopy"
      end
    end
  end
end

Which will automatically upload your screenshots to an external server and copies the URL to your clipboard. And if the screenshots are older than 2 hours they will be moved to the Old directory so your desktop stays uncluttered.

Launch Agent

In order to make this more useful you’ll want to run Maid as an OS X Launch Agent.

Launchd is a service that is used in OS X to manage processes. You can create your own Launch Agents that launchd will run on login.

The best way to get Maid running on login is to use a Launch Agent. A Launch Agent is a .plist file that describes how launchd should run the program.

Put the following code in ~/Library/LaunchAgents/ninja.jurriaan.maid.plist (assuming you use chruby and fish-shell):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
          "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>ninja.jurriaan.maid</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/bin/fish</string>
      <string>-l</string>
      <string>-c</string>
      <string>chruby 2.2.0; and maid daemon</string>
    </array>
    <key>KeepAlive</key>
    <true/>
  </dict>
</plist>

Load the daemon using launchctl load ~/Library/LaunchAgents/ninja.jurriaan.maid.plist so you don’t have to logout for it to start.

Comments

Fish Shell and Fasd

Recently I switched to the friendly interactive shell after being a ZSH user for a few years. While ZSH is a big improvement over Bash it’s also a lot slower and requires quite some configuration to get it to work the way you want to. In search for a better alternative I found out about the fish shell. I was immediately hooked.

Fish is a lot faster and provided a lot of functionality out of the box (auto completion, syntax highlighting and a lot more). The most impressing feature is the way it autocompletes commands while you type. This is a big improvement over the history search I was using in ZSH.

Fish’s configuration is stored in ~/.config/fish/config.fish. You can of course edit this file using your favorite editor, but you can also use the fish_config command which spawns a fancy angular based web interface where you can edit your settings, change your bindings and even inspect your command history.

Fasd

One thing that I was missing from my ZSH setup was Fasd. Fasd is a tool to quickly jump to recently used files or directories (similar to autojump and z). It turns out that I was in luck, because they quite recently added the necessary interface that was needed to port Fasd to fish.

To use fasd with fish you have to add the following lines to your config.fish:

1
2
3
4
5
6
7
function -e fish_preexec _run_fasd
  fasd --proc (fasd --sanitize "$argv") > "/dev/null" 2>&1
end

function j
  cd (fasd -d -e 'printf %s' "$argv")
end

And upgrade to the git master version of fish shell using brew install --HEAD fish.

You can now use the j command to quickly jump to directories using fuzzy matching. You can also create functions to for example edit/view files. See the Fasd README for more details.

I’m quite happy with my current configuration, it’s fast and productive.

Comments