Learning Hammerspoon中英文互译
本书主要介绍mac的脚本软件Hammerspoon的基础使用,仅提供可阅读部分翻译。
Unleash the power of automation on your Mac
在您的Mac上释放自动化的力量
Diego Zamboni
This book is for sale at http://leanpub.com/learning-hammerspoon
This version was published on 2020-08-10
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
© 2018 - 2020 Diego Zamboni
Para Susi, Kari, Fabi y Nube
Table of Contents
1 Preface to the book sample 前言 Preface to the early release Release notes 早期版本发行说明的前言
2 Introduction Mac automation 2 Mac自动化简介 What will you learn?
Conventions Used in This Book 本书中使用的约定
3 Getting started with Hammerspoon What is Hammerspoon?
How does Hammerspoon work?
Installing Hammerspoon
Your first Hammerspoon configuration 配置
The Hyper key
Keeping private information separate
将私人信息分开
Debugging tools and the Hammerspoon console
4 Using Spoons in Hammerspoon Using a Spoon to locate your mouse 4 使用锤子中的勺子用勺子定位鼠标
Automated Spoon installation and configuration
5 Just enough Lua to be productive with Hammerspoon Flow control
Dot-vs-colon method access in Lua
Functions
Tables
Tables as namespaces
Patterns
String manipulation
字符串操作
Learning more Lua
6 DRAFT 草稿 Exploring the Hammerspoon API Events and Hotkeys
Window, Menus and Screen Manipulation
On-screen Drawing, Images and Alerts
Application and Process Manipulation
Sound and Music
Networking and Web
System and Device Manipulation
Data Processing and Utilities
Hammerspoon itself
7 DRAFT Hammerspoon cookbook, tips and tricks Tip: be mindful of garbage collection
Show Homebrew package info
Tip: using asynchronous methods
Transform URLs before opening them
Other resources and configuration examples
8 Writing your own extensions and Spoons Writing a new Spoon
Writing a Hammerspoon extension in Lua
9 Using and extending Seal Using Seal
Writing your own Seal plugins
Colophon
1 Preface 前言 to the book sample
Thank you for downloading this book sample! In it you get to key chapters of the book which will help you get started with Hammerspoon and the use of Spoons to make life on your Mac easier.
I hope you will find it useful, and encourage you to get the full book to learn a lot more about advanced uses of Hammerspoon, including how to write your own Hammerspoon configuration in Lua, and how to develop your own Spoons.
In addition to this book sample, please take a look at ther Hammerspoon-related articles in my blog at , and at my “Hammerspoon” channel in YouTube, where you will find short videos that explain in a hands-on way some of the concepts that you find in this book: https://www.youtube.com/playlist?list=PLTZ6fO4RcbeOCZQ8OPTfq6KmUYDIC0OFL.
If you have any feedback or questions about this book, please visit the “Email the Author” page at https://leanpub.com/learning-hammerspoon/email_author/new.
Follow me on Twitter at https://twitter.com/zzamboni for more updates.
Preface to the early release
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
Release notes
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
August 2020
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
December 2019
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
November 2019
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
August 2019
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
April 8th, 2019
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
October 2018
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
2 Introduction
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
Mac automation
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
What will you learn?
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
Conventions Used in This Book
This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.
3 Getting started with Hammerspoon
What is Hammerspoon?
Hammerspoon is a Mac application that allows you to achieve 实现 an unprecedented level 前所未有的水平 of control over your Mac. Hammerspoon enables interaction with the system at multiple layers–from low-level file system or network access, mouse or keyboard event capture and generation, all the way to manipulating applications or windows, processing URLs and drawing on the screen. It also allows interfacing with AppleScript, Unix commands and scripts, and other applications. Hammerspoon configuration is written in Lua, a popular embedded programming language.
Hammerspoon是一款Mac应用程序,它可以让你实现前所未有的实现级别的前所未有的水平控制。Hammerspoon支持在多个层次与系统进行交互-从低级文件系统或网络访问、鼠标或键盘事件捕获和生成,一直到操作应用程序或窗口、处理URL和在屏幕上绘图。它还允许与AppleScript、Unix命令和脚本以及其他应用程序接口。Hammerspoon配置是用流行的嵌入式编程语言Lua编写的。
Using Hammerspoon, you can replace many stand-alone Mac utilities for controlling or customizing specific aspects of your Mac (the kind that tends to overcrowd the menubar). For example, the following are doable using Hammerspoon (these are all things I do with it on my machine - you can see the configuration for these in my own Hammerspoon config file):
使用Hammerspoon,你可以取代许多独立的Mac实用程序来控制或定制你的Mac的特定方面(这类工具往往会挤满菜单栏)。例如,使用Hammerspoon可以执行以下操作(这些都是我在我的机器上使用它执行的所有操作-您可以在我自己的Hammerspoon配置文件中查看这些配置):
Add missing or more convenient keyboard shortcuts to applications, even for complex multi-step actions. For example: automated tagging and filing in Evernote, mail/note archival in Mail, Outlook and Evernote, filing items from multiple applications to OmniFocus using consistent keyboard shortcuts, or muting/unmuting a conversation in Skype.
向应用程序添加缺少的或更方便的键盘快捷键,即使是复杂的多步骤操作也是如此。例如:Evernote中的自动标记和归档,Mail、Outlook和Evernote中的邮件/便笺归档,使用一致的键盘快捷键将多个应用程序中的项目归档到OmniFocus,或者在Skype中静音/取消静音对话。
Open URLs in different browsers based on regular expression patterns. When combined with Site-specific Browsers (I use Epichrome), this allows for highly flexible management of bookmarks, plugins and search configurations.
基于正则表达式模式在不同浏览器中打开URL。当与特定于站点的浏览器(我使用的是Epichrome)结合使用时,可以高度灵活地管理书签、插件和搜索配置。
Replace Spotlight, Lacona and other launchers with a fully configurable, extensible launcher, which allows not only to open applications, files and bookmarks, but to trigger arbitrary Lua functions.
用一个完全可配置、可扩展的启动器取代Spotlight、Lacona和其他启动器,它不仅允许打开应用程序、文件和书签,还可以触发任意的Lua功能。
Manipulate windows using keyboard shortcuts to resize, move and arrange them.
Set up actions to happen automatically when switching between WiFi networks–for example for reconfiguring proxies in some applications.
Keyboard-triggered translation of selected text between arbitrary human languages.
键盘触发的选定文本在任意人类语言之间的翻译。
Keep a configurable and persistent clipboard history.
Automatically pause audio playback when headphones are unplugged.
拔下耳机插头后自动暂停音频播放。
Hammerspoon is the most powerful Mac automation utility I have ever used. If you are a programmer, it can make using your Mac vastly more fun and productive.
Hammerspoon是我用过的最强大的Mac自动化工具。如果你是一名程序员,它可以让使用Mac变得更加有趣和高效。
How does Hammerspoon work?
Hammerspoon acts as a thin layer between the operating system and a Lua-based configuration language. It includes extensions for querying and controlling many aspects of the system. Some of the lower-level extensions are written in Objective-C, but all of them expose a Lua API, and it is trivial to write your own extensions or modules to extend its functionality.
Hammerspoon充当操作系统和基于Lua的配置语言之间的薄层。它包括用于查询和控制系统的许多方面的扩展。一些较低级别的扩展是用Objective-C编写的,但所有这些扩展都公开了Lua API,编写自己的扩展或模块来扩展其功能是微不足道的。
From the Hammerspoon configuration you can also execute external commands, run AppleScript or JavaScript code using the OSA scripting framework, establish network connections and even run network servers; you can capture and generate keyboard events, detect network changes, USB or audio devices being plugged in or out, changes in screen or keyboard language configuration; you can draw directly on the screen to display whatever you want; and many other things. Take a quick look at the Hammerspoon API index page to get a feeling of its extensive capabilities. And that is only the libraries that are built into Hammerspoon. There is an extensive and growing collection of Spoons, modules written in pure Lua that provide additional functionality and integration. And of course, the configuration is simply Lua code, so you can write your own code to do whatever you want.
在Hammerspoon配置中,您还可以执行外部命令,使用OSA脚本框架运行AppleScript或JavaScript代码,建立网络连接,甚至运行网络服务器;您可以捕获和生成键盘事件、检测网络更改、插入或拔出USB或音频设备、更改屏幕或键盘语言配置;您可以直接在屏幕上绘制以显示您想要的任何内容;以及许多其他功能。快速浏览Hammerspoon API索引页,了解其广泛的功能。这只是Hammerspoon内置的图书馆。有一个广泛且不断增长的勺子集合,