2 Introduction

Even if you are an experienced Mac user, you may find yourself often doing the same tasks by hand. Maybe you are reconfiguring your network proxy when you switch between home and work, filing your email and notes, or arranging your windows just the way you like them. Why are you spending valuable time doing these tasks by hand when you can automate them? Most importantly, you can have fun while doing it!

Mac automation

You may already be familiar with some automation tools. Apple includes the powerful Automator application, and there are many utilities, both free and commercial, that focus on different aspects of automation. In this book we go beyond these “consumer ready” automation tools and into the fascinating world of automating your Mac using more developer-friendly tools:

  • Lua, a powerful and easy-to-learn programming language, geared to embedded applications. Lua has gained fame as the embedded programming language in many games, but it is also found in many other applications and its lightweight syntax and extensibility make it applicable to many other domains.
  • Hammerspoon, a powerful open source automation framework that allows programming almost any aspect of your Mac using Lua. Hammerspoon acts as a bridge between Lua and most aspects of your system configuration and operation, AppleScript scripts, GUI elements (menus, dialogs, menubar items, etc.) This is a surprisingly powerful combination that gives you unprecedented control over your Mac. Using Hammerspoon you can replace many of the specialized applications for things like window manipulation, menubar configuration, keyboard shortcut creation, etc.

What will you learn?

We will explore the languages, techniques and tools you need to continuously personalize your Mac, to create for yourself a more productive and enjoyable environment. And as a programmer, I can assure you, you will have fun in the process!

These are some of the topics that we will cover:

  • How Hammerspoon works and how you can structure your configuration using both your own code and Spoons, Hammerspoon’s ready-to-use modules;
  • The basics of the AppleScript and Lua programming languages;
  • The Hammerspoon API and its capabilities;
  • Many specific examples of Hammerspoon configurations of varying complexity, to progressively teach you and give you a feeling of its power. The tasks you will automate include the following:
    • Pop-ups displaying output from arbitrary commands based on the selected text, without having to execute the commands by hand;
    • Automatically pausing audio playback in different applications when your headphones are unplugged;
    • Adding missing keyboard shortcuts to arbitrary applications, such as mute and unmute in Skype, automated tagging and filing in Evernote, and many others;
    • Keeping a configurable and persistent clipboard history;
    • Automatically opening URLs in different applications based on regular expression patterns, and combining this with site-specific Browsers to more flexibly manage your bookmarks, plugins and search configurations;
    • Replace Spotlight, Lacona and other launchers with your own, customized to your needs;
    • Colorizing your menubar according to the currently selected input language;
    • Manipulating your windows in fun and easy ways, using keyboard shortcuts to resize, move and arrange your windows, including moving them between multiple monitors;
    • Rotating and configuring your screens;
    • Setting up common keyboard shortcuts to “archive” items in different applications including Mail, Outlook, Evernote and others;
    • Creating OmniFocus tasks from multiple application using a common set of keyboard shortcuts;
    • Setting up actions to happen automatically when switching between WiFi networks, and which can be used for things like reconfiguring proxies, setting up printers, starting/stopping services, etc.
    • Automatically translate selected text between languages of your choice using keyboard shortcuts.

Conventions Used in This Book

The following typographical conventions are used in this book:

Constant width
Used for program listings, as well as within paragraphs to refer to URLs, filenames, and program elements such as variable or function names, Hammerspoon libraries, environment variables, and keywords.