Learning Hammerspoon中英文互譯

Learning Hammerspoon中英文互譯

本書主要介紹mac的腳本軟件Hammerspoon的基礎(chǔ)使用,僅提供可閱讀部分翻譯迫筑。

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
早期版本發(fā)行說明的前言

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 使用錘子中的勺子用勺子定位鼠標(biāo)

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 實現(xiàn) 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應(yīng)用程序且轨,它可以讓你實現(xiàn)前所未有的實現(xiàn)級別的前所未有的水平控制乍楚。Hammerspoon支持在多個層次與系統(tǒng)進(jìn)行交互-從低級文件系統(tǒng)或網(wǎng)絡(luò)訪問睡互、鼠標(biāo)或鍵盤事件捕獲和生成玻淑,一直到操作應(yīng)用程序或窗口栗柒、處理URL和在屏幕上繪圖礁扮。它還允許與AppleScript、Unix命令和腳本以及其他應(yīng)用程序接口瞬沦。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可以執(zhí)行以下操作(這些都是我在我的機器上使用它執(zhí)行的所有操作-您可以在我自己的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.

向應(yīng)用程序添加缺少的或更方便的鍵盤快捷鍵,即使是復(fù)雜的多步驟操作也是如此曙痘。例如:Evernote中的自動標(biāo)記和歸檔芳悲,Mail、Outlook和Evernote中的郵件/便箋歸檔屡江,使用一致的鍵盤快捷鍵將多個應(yīng)用程序中的項目歸檔到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.

基于正則表達(dá)式模式在不同瀏覽器中打開URL耙饰。當(dāng)與特定于站點的瀏覽器(我使用的是Epichrome)結(jié)合使用時,可以高度靈活地管理書簽塑崖、插件和搜索配置文黎。

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.

用一個完全可配置惹苗、可擴(kuò)展的啟動器取代Spotlight、Lacona和其他啟動器耸峭,它不僅允許打開應(yīng)用程序桩蓉、文件和書簽,還可以觸發(fā)任意的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.

鍵盤觸發(fā)的選定文本在任意人類語言之間的翻譯院究。

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充當(dāng)操作系統(tǒng)和基于Lua的配置語言之間的薄層。它包括用于查詢和控制系統(tǒng)的許多方面的擴(kuò)展样漆。一些較低級別的擴(kuò)展是用Objective-C編寫的为障,但所有這些擴(kuò)展都公開了Lua API,編寫自己的擴(kuò)展或模塊來擴(kuò)展其功能是微不足道的放祟。

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配置中鳍怨,您還可以執(zhí)行外部命令,使用OSA腳本框架運行AppleScript或JavaScript代碼跪妥,建立網(wǎng)絡(luò)連接鞋喇,甚至運行網(wǎng)絡(luò)服務(wù)器;您可以捕獲和生成鍵盤事件骗奖、檢測網(wǎng)絡(luò)更改确徙、插入或拔出USB或音頻設(shè)備醒串、更改屏幕或鍵盤語言配置执桌;您可以直接在屏幕上繪制以顯示您想要的任何內(nèi)容;以及許多其他功能芜赌⊙稣酰快速瀏覽Hammerspoon API索引頁,了解其廣泛的功能缠沈。這只是Hammerspoon內(nèi)置的圖書館膘壶。有一個廣泛且不斷增長的勺子集合,即用純Lua編寫的模塊洲愤,它們提供額外的功能和集成颓芭。當(dāng)然,配置只是Lua代碼柬赐,所以您可以編寫自己的代碼來做任何您想做的事情亡问。

Interested? Let’s get started!

Installing Hammerspoon

Hammerspoon is a regular Mac application. To install it by hand, you just need to download it from https://github.com/Hammerspoon/hammerspoon/releases/latest, unzip the downloaded file and drag it to your /Applications folder (or anywhere else you want).

If you are automation-minded like me, you probably use Homebrew and its plugin Cask to manage your applications. In this case, you can use Cask to install Hammerspoon:

brew cask install hammerspoon

When you run Hammerspoon for the first time, you will see its icon appear in the menubar, and a notification telling you that it couldn’t find a configuration file. Let’s fix that!

If you click on the initial notification, your web browser will open to the excellent Getting Started with Hammerspoon page, which I highly recommend you read for more examples.

Your first Hammerspoon configuration

Let us start with a few simple examples. As tradition mandates, we will start with a “Hello World” example. Open $HOME/.hammerspoon/init.lua (Hammerspoon will create the directory upon first startup, but you need to create the file) in your favorite editor, and type the following:

hs.hotkey.bindSpec({ { "ctrl", "cmd", "alt" }, "h" }, function() hs.notify.show("Hello World!", "Welcome to Hammerspoon", "") end )

Save the file, and from the Hammerspoon icon in the menubar, select “Reload config”. Apparently nothing will happen, but if you then press Ctrl?-?Alt?-???-?h on your keyboard, you will see a notification on your screen welcoming you to the world of Hammerspoon.

保存文件,并從菜單欄的Hammerspoon圖標(biāo)中選擇“重新加載配置”肛宋。顯然什么都不會發(fā)生州藕,但是如果您隨后按下鍵盤上的CtrlAltAlt?-???-?h,您將在屏幕上看到一個通知酝陈,歡迎您來到Hammerspoon的世界床玻。

Although it should be fairly self-explanatory, let us dissect this example to give you a clearer understanding of its components:

雖然它應(yīng)該是相當(dāng)不言自明的,但讓我們仔細(xì)分析這個示例沉帮,讓您更清楚地了解它的組件:

All Hammerspoon built-in extensions start with hs. In this case, hs.hotkey is the extension that handles keyboard bindings. It allows us to easily define which functions will be called in response to different keyboard combinations. You can even differentiate between the keys being pressed, released or held down if you need to. The other extension used in this example is hs.notify, which allows us to interact with the macOS Notification Center to display, react and interact with notifications.

所有Hammerspoon內(nèi)置擴(kuò)展名都以hs開頭锈死。在本例中贫堰,hs.hotkey是處理鍵盤綁定的擴(kuò)展。它允許我們輕松地定義將調(diào)用哪些函數(shù)來響應(yīng)不同的鍵盤組合待牵。如果需要严嗜,您甚至可以區(qū)分按下、松開或按住的鍵洲敢。本例中使用的另一個擴(kuò)展是hs.Notify漫玄,它允許我們與MacOS通知中心交互,以顯示压彭、反應(yīng)和交互通知睦优。

Within hs.hotkey, the hs.hotkey.bindSpec function allows you to bind a function to a pressed key. Its first argument is a key specification which consists of a list (Lua lists and table literals are represented using curly braces) with two elements: a list of the key modifiers, and the key itself. In this example, { { "ctrl", "cmd", "alt" }, "h" } represents pressing Ctrl?-?Alt?-???-?h.

在hs.hotkey中,hs.hotkey.bindSpec函數(shù)允許您將函數(shù)綁定到按下的鍵壮不。它的第一個參數(shù)是一個鍵規(guī)范汗盘,它由一個列表(LUA列表和表字面值用大括號表示)和兩個元素組成:鍵修飾符列表和鍵本身。在本例中询一,{{“ctrl”隐孽,“cmd”,“alt”}健蕊,“h”}表示按CtrlAlt?-??-???-?h菱阵。

The second argument to bindSpec is the function to call when the key is pressed. Here we are defining an inline anonymous function using function() ... end.

bindSpec的第二個參數(shù)是按鍵時要調(diào)用的函數(shù)。在這里缩功,我們使用function()定義內(nèi)聯(lián)匿名函數(shù).結(jié)束晴及。

The callback function uses hs.notify.show to display the message. Take a quick look at the hs.notify documentation to get an idea of its extensive capabilities, including configuration of all aspects of a notification’s appearance and buttons, and the functions to call upon different user actions.

回調(diào)函數(shù)使用hs.notfy.show顯示消息〉招浚快速瀏覽h.tify文檔以了解其廣泛的功能虑稼,包括配置通知外觀和按鈕的所有方面,以及調(diào)用不同用戶操作的功能势木。

Try changing the configuration to display a different message or use a different key. After every change, you need to instruct Hammerspoon to reload its configuration, which you can do through its menubar item (although we will learn how to automate it below).

嘗試更改配置以顯示不同的消息或使用不同的密鑰蛛倦。每次更改之后,您都需要指示Hammerspoon重新加載其配置啦桌,這可以通過其菜單欄項目來完成(盡管我們將在下面學(xué)習(xí)如何自動化)溯壶。

The Hyper key

You will notice through this book that we use the Ctrl?-?Alt?-?? combination very frequently in our keybindings. The idea behind this is to use a modifier key combination which is never used by other applications, so that we can setup global Hammerspoon keybindings without worrying about conflicts with application-specific key bindings.

通過本書,您會注意到我們在鍵綁定中非常頻繁地使用Ctrl-Alt?-??組合震蒋。這背后的想法是使用其他應(yīng)用程序從未使用過的修飾鍵組合茸塞,這樣我們就可以設(shè)置全局Hammerspoon鍵綁定,而不必?fù)?dān)心與特定于應(yīng)用程序的鍵綁定沖突查剖。

To avoid having to type {"ctrl","alt","cmd"} every time in the configuration file, we can define them as variable. For example, I have the following at the top of my init.lua:

hyper = { "ctrl", "alt", "cmd" } shift_hyper = { "shift", "ctrl", "alt", "cmd" }

Then we can simply use hyper or shift_hyper in our key binding declarations. The example above becomes:

hs.hotkey.bindSpec({ hyper, "h" }, function() hs.notify.show("Hello World!", "Welcome to Hammerspoon", "") end )

I find Ctrl?-?Alt?-?? handy because the three keys are next to each other in a row right next to the spacebar in my keyboard, so I can easily hit them as a chord. You are of course free to use a different combination depending on your preferences and your keyboard layout.

我發(fā)現(xiàn)CtrlAlt?-??-??很方便钾虐,因為這三個鍵緊挨著鍵盤上的空格鍵,所以我可以很容易地按下它們作為和弦笋庄。當(dāng)然效扫,根據(jù)您的偏好和鍵盤布局倔监,您可以自由使用不同的組合。

All the keybinding examples in this book will assume you have defined the hyper variable to represent the modifier key combination you want to use for most of your global keybindings.

本書中的所有鍵綁定示例都假定您已經(jīng)定義了hyper變量來表示要用于大多數(shù)全局鍵綁定的修改鍵組合菌仁。

Mapping a single key as Hyper using Karabiner Elements

If you have a real, physical key to spare in your keyboard, you may want to map it as Hyper. For example, some people like to use the Caps Lock key as Hyper (I remap my Caps Lock key as a second Ctrl key, which I find more useful). To achieve this, you can use another free utility called Karabiner Elements, which allows you to do low-level keyboard remapping with use. You first need to install Karabiner:

如果您的鍵盤上有一個真正的物理按鍵可供備用浩习,您可能希望將其映射為Hyper。例如济丘,有些人喜歡使用Caps Lock鍵作為Hyper(我將Caps Lock鍵重新映射為第二個Ctrl鍵谱秽,我覺得這個鍵更有用)。要實現(xiàn)這一點摹迷,您可以使用另一個名為Karabiner Elements的免費實用程序疟赊,它允許您使用USE進(jìn)行低級鍵盤重新映射。您首先需要安裝Karabiner:

brew cask install karabiner-elements

Karabiner needs to install a kernel extension to do its work, and recent versions of macOS will block it by default. You will get a dialog notifying you about this, and asking you to use the Security Preferences Pane to allow it if you want. Once you click “Allow” in this pane, Karabiner should be ready to use:

Karabiner需要安裝內(nèi)核擴(kuò)展才能完成工作峡碉,MacOS的最新版本默認(rèn)情況下會對其進(jìn)行擋路操作近哟。您將看到一個對話框,通知您這一點鲫寄,并要求您根據(jù)需要使用Security Preferences窗格進(jìn)行允許吉执。在此窗格中單擊“允許”后,Karabiner就可以使用了:

Once you run the Karabiner-Elements app, you can remap the Caps Lock key to any other key. Within the “Simple Modifications” tab you could, for example, remap Caps Lock to a nonexisting function key such as F20:

一旦你運行了Karabiner-Elements應(yīng)用程序地来,你就可以將Caps Lock鍵重新映射到任何其他鍵戳玫。例如,在“Simple Modiments”(簡單修改)選項卡中靠抑,您可以將Caps Lock(大寫鎖定)重新映射到不存在的功能鍵量九,如F20:

You need to map the hyper and shift_hyper variables in your Hammerspoon configuration according to the key you used. For example:

hyper = { "f20" } shift_hyper = { "shift", "f20" }

Afterwards, you can use hyper and shift_hyper in your keybindings as shown before.

Keeping private information separate

It makes sense to keep your configuration files (as you should most other files) under control of a version control system like Git or Mercurial. This allows you to keep track of changes you make to your files, and it also makes it easy to share your configuration with others, for example by keeping them in Github or BitBucket.

將您的配置文件(就像您應(yīng)該控制的大多數(shù)其他文件一樣)置于Git或Mercurial等版本控制系統(tǒng)的控制之下是有意義的适掰。這使您可以跟蹤您對文件所做的更改颂碧,還可以輕松地與其他人共享您的配置,例如类浪,通過將它們保存在Github或BitBucket中载城。

However, it is also common to have in your configuration pieces of information that you do not want to share publicly: passwords, authentication tokens, or simply experimental code that you are not ready to share yet. In theses cases, you can keep some configuration in separate files that are not committed to your shared files. In Lua, you can read an external file as code using the dofile() function. You can have a “l(fā)ocal-only” configuration file which is read from your main init.lua file:

但是,在您的配置中有一些您不想公開共享的信息也是很常見的:密碼费就、身份驗證令牌诉瓦,或者只是您還沒有準(zhǔn)備好共享的實驗性代碼。在這些情況下力细,您可以將一些配置保存在不提交到共享文件的單獨文件中睬澡。在Lua中,可以使用dofile()函數(shù)將外部文件作為代碼讀取眠蚂。您可以擁有從主init.lua文件讀取的“僅本地”配置文件:

local localfile = hs.configdir .. "/init-local.lua" if hs.fs.attributes(localfile) then dofile(localfile) end

A couple of noteworthy points about this code:

We use the hs.configdir variable instead of hardcoding the path. This ensures that the code will execute properly even if (for some reason) the configuration directory is stored somewhere else.

我們使用hs.configdir變量煞聪,而不是對路徑進(jìn)行硬編碼。這確保了即使(由于某些原因)配置目錄存儲在其他位置逝慧,代碼也將正確執(zhí)行昔脯。

The dofile() function throws an error if the file contains a syntax error which we want, but also if the file does not exist, which we do not want. For this reason we enclose the call to dofile in a check for existence of the file. Lua does not have a function to explicitly check for file existence, but we can use hs.fs.attributes, which returns nil if the file cannot be found.

如果文件包含我們想要的語法錯誤啄糙,但如果文件不存在(我們不想要的),則dofile()函數(shù)會拋出錯誤云稚。出于這個原因隧饼,我們將對dofile的調(diào)用包含在檢查文件是否存在的過程中。Lua沒有顯式檢查文件是否存在的函數(shù)静陈,但是我們可以使用hs.fs.tributes燕雁,如果找不到文件,它將返回nil鲸拥。

Debugging tools and the Hammerspoon console

As you start modifying your configuration, errors will happen, as they always do when coding. To help in development and debugging, Hammerspoon offers a console window where you can see any errors and messages printed by your Lua code as it executes, and also type code to be evaluated. It is a very useful tool while developing your Hammerspoon configuration.

當(dāng)您開始修改配置時贵白,將會出現(xiàn)錯誤,就像它們在編碼時經(jīng)常發(fā)生的那樣崩泡。為了幫助開發(fā)和調(diào)試禁荒,Hammerspoon提供了一個控制臺窗口,您可以在其中看到Lua代碼在執(zhí)行時打印的任何錯誤和消息角撞,還可以鍵入要評估的代碼呛伴。在開發(fā)Hammerspoon配置時,它是一個非常有用的工具谒所。

To invoke the console, you normally choose “Console…” from the Hammerspoon menubar item. However, this is such a common operation, that you might find it useful to also set a key combination for showing the console. Most of Hammerspoon’s internal functionality is also accessible through its API. In this case, looking at the documentation for the main hs module reveals that there is an hs.toggleConsole function. Using the knowledge you have acquired so far, you can easily configure a hotkey for opening and hiding the console:

要調(diào)用控制臺热康,通常選擇“Console…”從Hammerspoon菜單欄項目中拿到的。但是劣领,這是一個非常常見的操作姐军,因此您可能會發(fā)現(xiàn)還可以設(shè)置用于顯示控制臺的組合鍵。Hammerspoon的大部分內(nèi)部功能也可以通過其API訪問尖淘。在本例中奕锌,查看主hs模塊的文檔會發(fā)現(xiàn)有一個hs.toggleConsole函數(shù)。使用到目前為止所學(xué)的知識村生,您可以輕松配置用于打開和隱藏控制臺的熱鍵:

hs.hotkey.bindSpec({ hyper, "y" }, hs.toggleConsole)

``

tell application "Hammerspoon"
  execute lua code "hs.toggleConsole()"
end tell

Once you reload your configuration, you should be able to use Ctrl?-?Alt?-???-?y to open and close the console. Any Lua code you type in the Console will be evaluated in the main Hammerspoon context, so you can add to your configuration directly from there. This is a good way to incrementally develop your code before committing it to the init.lua file.

重新加載配置后惊暴,您應(yīng)該能夠使用Ctrl?-?Alt?-???-?y打開和關(guān)閉控制臺。您在控制臺中鍵入的任何Lua代碼都將在主Hammerspoon上下文中求值趁桃,因此您可以直接從那里添加到您的配置中辽话。這是在將代碼提交到init.lua文件之前增量開發(fā)代碼的好方法。

You may have noticed by now another common operation while developing Hammerspoon code: reloading the configuration, which you normally have to do from the Hammerspoon menu. So why not set up a hotkey to do that as well? Again, the hs module comes to our help with the hs.reload method:

到目前為止卫病,您可能已經(jīng)注意到開發(fā)Hammerspoon代碼時的另一個常見操作:重新加載配置油啤,這通常需要從Hammerspoon菜單執(zhí)行。那么蟀苛,為什么不設(shè)置一個熱鍵來實現(xiàn)這一點呢益咬?同樣,hs模塊通過hs.reload方法向我們提供幫助:

hs.hotkey.bindSpec({ hyper, "r" }, hs.reload)

Another useful development tool is the hs command, which you can run from your terminal to get a Hammerspoon console. To install it, you can use the hs.ipc.cliInstall function, which you can just add to your init.lua file to check and install the command every time Hammerspoon runs.

另一個有用的開發(fā)工具是hs命令屹逛,您可以從終端運行該命令來獲取Hammerspoon控制臺础废。要安裝它汛骂,您可以使用hs.ipc.cliInstall函數(shù),您只需將其添加到init.lua文件中评腺,即可在Hammerspoon每次運行時檢查并安裝該命令帘瞭。

The hs.ipc.cliInstall function creates symlinks under /usr/local/ to the hs command and its manual page file, located inside the Hammerspoon application bundle. Under some circumstances (particularly if you build Hammerspoon from source, or if you install different versions of it), you may end up with broken symlinks. If the hs command stops working and hs.ipc.cliInstall() doesn’t fix it, look for broken symlinks left behind from old versions of Hammerspoon. Remove them and things should work again.

hs.ipc.cliInstall函數(shù)在/usr/local/下創(chuàng)建指向hs命令及其手冊頁文件的符號鏈接,這些文件位于Hammerspoon應(yīng)用程序包中蒿讥。在某些情況下(特別是如果您從源代碼構(gòu)建Hammerspoon蝶念,或者如果您安裝了不同版本的Hammerspoon),您可能會得到斷開的符號鏈接芋绸。如果hs命令停止工作媒殉,并且hs.ipc.cliInstall()無法修復(fù)它,請查找舊版本的Hammerspoon留下的斷開的符號鏈接摔敛。把它們移走廷蓉,事情就會恢復(fù)正常。

Now you have all the tools for developing your Hammerspoon configuration.

4 Using Spoons in Hammerspoon

Spoons are modules written in Lua which can be easily installed and loaded into Hammerspoon to provide ready-to-use functionality. Spoons provide a predefined API to configure and use them. They are also a good way to share your own work with other users.

勺子是用Lua編寫的模塊马昙,可以很容易地安裝和加載到Hammerspoon中桃犬,以提供隨時可用的功能。勺子提供預(yù)定義的API來配置和使用它們行楞。它們也是與其他用戶共享您自己的工作的好方法攒暇。

Using a Spoon to locate your mouse

As a first example, we will use the MouseCircle spoon, which allows us to set up a hotkey that displays a color circle around the current location of the mouse pointer for a few seconds, to help you locate it.

作為第一個示例,我們將使用MouseCircle勺子子房,它允許我們設(shè)置一個熱鍵形用,該熱鍵在鼠標(biāo)指針的當(dāng)前位置周圍顯示一個顏色圓圈幾秒鐘,以幫助您定位它证杭。

To install the spoon, download its zip file from https://www.hammerspoon.org/Spoons/MouseCircle.html, unpack it, and double-click on the resulting MouseCircle.spoon file. Hammerspoon will install the Spoon under ~/.hammerspoon/Spoons/.

Once a Spoon is installed, you need to use the hs.loadSpoon() function to load it. Type the following in the Hammerspoon console, or add it to your init.lua file and reload the configuration:

hs.loadSpoon("MouseCircle")

After a spoon is loaded, and depending on what it does, you may need to configure it, assign hotkeys, and start it. A spoon’s API is available through the spoon.<SpoonName> namespace. To learn the API you need to look at the spoon documentation page. In the case of MouseCircle, a look at http://www.hammerspoon.org/Spoons/MouseCircle.html reveals that it has two methods (bindHotkeys() and show()) and one configuration variable (color) available under spoon.MouseCircle.

加載勺子后田度,根據(jù)它的功能,您可能需要配置它躯砰、分配熱鍵并啟動它每币。勺子的API可以通過spoon.<SpoonName>名稱空間獲得。要了解API琢歇,您需要查看勺子文檔頁面。在MouseCircle的例子中梦鉴,查看http://www.hammerspoon.org/Spoons/MouseCircle.html會發(fā)現(xiàn)它在spoon.MouseCircle下有兩個方法(bindHotkey()和show())和一個配置變量(COLOR)李茫。

The first API call is spoon.MouseCircle:bindHotkeys(), which allows us to set up a hotkey that shows the mouse locator circle around the location of the mouse pointer. Let’s say we wanted to bind the mouse circle to Ctrl?-?Alt?-???-?d. According to the MouseCircle documentation, the name for this action is show, so we can do the following:

第一個API調(diào)用是spoon.MouseCircle:bindHotkey(),它允許我們設(shè)置一個熱鍵來顯示鼠標(biāo)指針位置周圍的鼠標(biāo)定位器圓肥橙。假設(shè)我們想要將鼠標(biāo)圈綁定到Ctrl?-?Alt?-???-?d魄宏。根據(jù)鼠標(biāo)圈文檔,此操作的名稱為SHOW存筏,因此我們可以執(zhí)行以下操作:

spoon.MouseCircle:bindHotkeys({ show = { hyper, "d" } })

See The “Hyper” key for instructions on how to set up the hyper variable, if you have not done so yet.

有關(guān)如何設(shè)置HYPER變量的說明宠互,請參見“Hyper”鍵(如果您尚未這樣做)味榛。

Once you do this, press the hotkey and you should see a red circle appear around the mouse cursor, and fade away after 3 seconds.

一旦你這樣做了,按下熱鍵予跌,你應(yīng)該會看到一個紅色的圓圈出現(xiàn)在鼠標(biāo)光標(biāo)周圍搏色,并在3秒后消失。

All spoons which offer the possibility of binding hotkeys have to expose it through the same API:

所有提供綁定熱鍵可能性的勺子都必須通過相同的API公開它:

spoon.SpoonName:bindHotkeys({ action1 = keySpec1, action2 = keySpec2, ... })

Each actionX is a name defined by the spoon, which refers to something that can be bound to a hotkey, and each keySpecX is a table with two elements: a list of modifiers and the key itself, such as { { "ctrl", "cmd", "alt" }, "d" } (or equivalently, { hyper, "d"})

每個actionX是由勺子定義的名稱券册,它指的是可以綁定到熱鍵的東西频轿,而每個keySpecX是一個包含兩個元素的表:修飾符列表和鍵本身,例如{{“ctrl”烁焙,“cmd”航邢,“alt”},“d”}(或者等價地骄蝇,{hyper膳殷,“d”})

The second API call in the MouseCircle spoon is show(), which triggers the functionality of showing the locator circle directly. Let’s try it! Type the following in the console:

MouseCircle勺子中的第二個API調(diào)用是show(),它觸發(fā)直接顯示定位器圓的功能九火。讓我們試一試吧秽之!在控制臺中鍵入以下內(nèi)容:

spoon.MouseCircle:show()

Most spoons are structured like this: you can set up hotkeys to trigger the main functionality, but you can also trigger it via method calls. Normally you won’t use these methods, but their availability makes it possible for you to use spoon functionality from our own configuration, or from other spoons, to create further automation.

大多數(shù)勺子的結(jié)構(gòu)是這樣的:您可以設(shè)置熱鍵來觸發(fā)主要功能,但也可以通過方法調(diào)用來觸發(fā)它吃既。通常情況下考榨,您不會使用這些方法,但是它們的可用性使您可以從我們自己的配置或從其他勺子中使用勺子功能鹦倚,以創(chuàng)建進(jìn)一步的自動化河质。

spoon.MouseCircle.color is a public configuration variable exposed by the spoon, which specifies the color that will be used to draw the circle. Colors are defined according to the documentation for the hs.drawing.color module. Several color collections are supported, including the OS X system collections and a few defined by Hammerspoon itself. Color definitions are stored in Lua tables indexed by their name. For example, you can view the hs.drawing.color.hammerspoon table, including the color definitions, by using the convenient hs.inspect method on the console:

color是由勺子公開的公共配置變量,它指定將用于繪制圓的顏色震叙。顏色是根據(jù)hs.draing.color模塊的文檔定義的掀鹅。支持幾個顏色集合,包括OS X系統(tǒng)集合和由Hammerspoon本身定義的幾個集合媒楼。顏色定義存儲在按其名稱索引的LUA表中乐尊。例如,您可以在控制臺使用方便的hs.spect方法查看hs.draing.Color.hammerspoon表划址,包括顏色定義:

 hs.inspect(hs.drawing.color.hammerspoon) { black = { alpha = 1, blue = 0.0, green = 0.0, red = 0.0 }, green = { alpha = 1, blue = 0.0, green = 1.0, red = 0.0 }, osx_red = { alpha = 1, blue = 0.302, green = 0.329, red = 0.996 }, osx_green = { ...

Lua does not include a function to easily get the keys of a table so you have to use the pairs() function to loop over the key/value pairs of the table. The hs.inspect function is convenient, but to get just the list of tables and the color names, without the color definitions themselves, you can use the following code (if you type this in the console you have to type it all in a single line – and beware, the output is a long list):

LUA不包括輕松獲取表的鍵的函數(shù)扔嵌,因此您必須使用PILES()函數(shù)來循環(huán)表的鍵/值對。hs.check函數(shù)很方便夺颤,但是要只獲得表和顏色名稱的列表痢缎,而不是顏色定義本身,您可以使用以下代碼(如果您在控制臺中鍵入這些內(nèi)容世澜,則必須在一行中全部鍵入-請注意独旷,輸出是一個很長的列表):

for listname,colors in pairs(hs.drawing.color.lists()) do print(listname) for color,def in pairs(colors) do print(" " .. color) end end

對于listname,成對的顏色(hs.draing.Color.list())執(zhí)行打印(Listname);對于顏色嵌洼,成對定義(Colors)執(zhí)行打印(“”.顏色)結(jié)束端

If we wanted to make the circle green, we can assign the configuration value like this:

如果我們想讓圓變成綠色案疲,我們可以這樣分配配置值:

spoon.MouseCircle.color = hs.drawing.color.hammerspoon.green

The next time you invoke the show() method, either directly or through the hotkey, you will see the circle in the new color.

下次直接或通過熱鍵調(diào)用show()方法時,您將看到新顏色的圓麻养。

You may have noticed that we accessed the configuration variable with a dot (spoon.MouseCircle.color), and we also used it for some function calls (e.g. hs.notify.show, whereas for show() we used a colon (spoon.MouseCircle:show()). The latter is Lua’s object-method-call notation, and its effect is to implicitly pass the object as an implicit first argument called self. This is simply a syntactic shortcut, i.e. the following two are equivalent:

您可能已經(jīng)注意到褐啡,我們使用圓點(spoon.MouseCircle.color)訪問配置變量,并且還將其用于一些函數(shù)調(diào)用(例如回溺,hs.notfy.show春贸,而對于show(),我們使用冒號(spoon.MouseCircle:show()遗遵。后者是Lua的對象-方法-調(diào)用表示法萍恕,其效果是隱式地將對象作為名為self的隱式第一個參數(shù)傳遞。這只是一個語法快捷方式车要,即以下兩個是等效的:

spoon.MouseCircle:show() spoon.MouseCircle.show(spoon.MouseCircle)

Normally you would use colon notation, but the alternative can be useful when constructing function pointers. For example, if you wanted to bind a second key to show the mouse circle, you might initially try the following:

通常情況下允粤,您會使用冒號表示法,但在構(gòu)造函數(shù)指針時翼岁,另一種方法可能會很有用类垫。例如,如果要綁定第二個關(guān)鍵點以顯示鼠標(biāo)圓琅坡,最初可能會嘗試以下操作:

hs.hotkey.bindSpec({ hyper, "p" }, spoon.MouseCircle:show)

But this results in an error. The correct way is to wrap the call in an anonymous function:

但這會導(dǎo)致錯誤悉患。正確的方法是將調(diào)用封裝在匿名函數(shù)中:

hs.hotkey.bindSpec({ hyper, "p" }, function() spoon.MouseCircle:show() end)

Alternatively, you can use the hs.fnutils.partial function to construct a function pointer that includes the correct first argument:

或者,您可以使用hs.fnutils.part函數(shù)構(gòu)造包含正確第一個參數(shù)的函數(shù)指針:

hs.hotkey.bindSpec({ hyper, "p" }, hs.fnutils.partial(spoon.MouseCircle.show, spoon.MouseCircle))

Lua supports using functions as first-class values, and the hs.fnutils extension includes a number of functions that make it easy to use them.

LUA支持將函數(shù)用作一級值榆俺,并且hs.fnutils擴(kuò)展包括許多函數(shù)售躁,使其易于使用。

By now you know enough to use spoons with Hammerspoon’s native capabilities: look for the ones you want, download and install them by hand, and configure them in your init.lua using their configuration variables and API. In the next section we will explore how to install and configure spoons in a more automated way.

到目前為止茴晋,您已經(jīng)足夠了解如何使用具有Hammerspoon原生功能的勺子了:查找所需的勺子陪捷,手動下載并安裝它們,然后使用它們的配置變量和API在init.lua中配置它們诺擅。在下一節(jié)中市袖,我們將探索如何以更自動化的方式安裝和配置勺子。

Automated Spoon installation and configuration

Once you develop a complex Hammerspoon configuration using spoons, you may start wondering if there is an easy way to manage them. There are no built-in mechanisms for automatically installing spoons, but you can use a spoon called SpoonInstall that implements this functionality. You can download it from http://www.hammerspoon.org/Spoons/SpoonInstall.html. Once installed, you can use it to declaratively install, configure and run spoons. For example, with SpoonInstall you can use the MouseCircle spoon as follows:

一旦您使用勺子開發(fā)了一個復(fù)雜的Hammerspoon配置烁涌,您可能會開始想知道是否有一種簡單的方法來管理它們苍碟。沒有自動安裝勺子的內(nèi)置機制,但是您可以使用一個名為SpoonInstall的勺子來實現(xiàn)此功能烹玉。您可以從http://www.hammerspoon.org/Spoons/SpoonInstall.html.下載安裝后驰怎,您可以使用它來聲明性地安裝、配置和運行勺子二打。例如,對于SpoonInstall掂榔,您可以按如下方式使用MouseCircle勺子:

hs.loadSpoon("SpoonInstall") spoon.SpoonInstall:andUse("MouseCircle", { config = { color = hs.drawing.color.osx_red, }, hotkeys = { show = { hyper, "d" } }})

If the MouseCircle spoon is not yet installed, spoon.SpoonInstall:andUse() will automatically download and install it, and set its configuration variables and hotkeys according to the declaration.

如果尚未安裝MouseCircle勺子继效,spoon.SpoonInstall:anduse()將自動下載并安裝症杏,并根據(jù)聲明設(shè)置其配置變量和熱鍵。

If there is nothing to configure in the spoon, spoon.SpoonInstall:and-Use("SomeSpoon") does exactly the same as hs.loadSpoon("SomeSpoon"). But if you want to set configuration variables, hotkey bindings or other parameters, the following keys are recognized in the map provided as a second parameter:

如果在勺子中沒有需要配置的內(nèi)容瑞信,則spoon.SpoonInstall:and-use(“SomeSpoon”)的作用與hs.loadSpoon(“SomeSpoon”)完全相同厉颤。但是,如果您想要設(shè)置配置變量凡简、熱鍵綁定或其他參數(shù)逼友,則會在提供的映射中將以下鍵識別為第二個參數(shù):

config is a Lua table containing keys corresponding to configuration variables in the spoon. In the example above, config = { color = hs.drawing.color.osx_red } has the same effect as setting spoon.MouseCircle.color = hs.drawing.color.osx_red

CONFIG是一個LUA表,其中包含與勺子中的配置變量相對應(yīng)的密鑰秤涩。在上面的示例中帜乞,config={color=hs.draing.Color.osx_red}與設(shè)置spoon.MouseCircle.color=hs.draing.Color.osx_red的效果相同

hotkeys is a Lua table with the same structure as the mapping parameter passed to the bindHotkeys method of the spoon. In our example above, hotkeys = { show = { hyper, "d" } } automatically triggers a call to spoon.MouseCircle:bindHotkeys({ show = { hyper, "d" } }).

HotKey是一個Lua表,其結(jié)構(gòu)與傳遞給勺子的bindHotkey方法的映射參數(shù)具有相同的結(jié)構(gòu)筐眷。在上面的示例中黎烈,hotkey={show={hyper,“d”}}自動觸發(fā)對spoon.MouseCircle:bindHotkey({show={hyper匀谣,“d”}})的調(diào)用照棋。

loglevel sets the log level of the logger attribute within the spoon, if it exists. The valid values for this attribute are ‘nothing’, ‘error’, ‘warning’, ‘info’, ‘debug’, or ‘verbose’.

loglevel設(shè)置勺子內(nèi)記錄器屬性的日志級別(如果存在)。此屬性的有效值為‘Nothing’武翎、‘Error’烈炭、‘Warning’、‘info’宝恶、‘debug’或‘Verbose’符隙。

start is a boolean value which indicates whether to call the Spoon’s start() method (if it has one) after configuring everything else.

start是一個布爾值,它指示在配置其他所有內(nèi)容之后是否調(diào)用Spoon的start()方法(如果有)卑惜。

fn specifies a function which will be called with the freshly-loaded Spoon object as its first argument. This can be used to execute other startup or configuration actions that are not covered by the other attributes. For example, if you use the Seal spoon (a configurable launcher), you need to call its loadPlugins() method to specify which Seal plugins to use. You can achieve this with something like this: spoon.SpoonInstall:andUse("Seal", { hotkeys = { show = { {"cmd"}, "space" } }, fn = function(s) s:loadPlugins({"apps", "calc", "safari_bookmarks"}) end, start = true, })

fn指定一個函數(shù)膏执,該函數(shù)將使用新加載的Spoon對象作為其第一個參數(shù)進(jìn)行調(diào)用。這可用于執(zhí)行其他屬性未涵蓋的其他啟動或配置操作露久。例如更米,如果您使用Seal Spoon(一個可配置的啟動器),則需要調(diào)用其loadPlugins()方法來指定要使用的Seal插件毫痕。您可以這樣實現(xiàn):spoon.SpoonInstall:anduse(“Seal”征峦,{hotkey={show={{“cmd”},“space”}}消请,fn=function(S)s:loadPlugins({“apps”栏笆,“calc”,“safari_bookmark”})end臊泰,start=true蛉加,})

repo indicates the repository from where the Spoon should be installed if needed. Defaults to "default", which indicates the official Spoon repository at http://www.hammerspoon.org/Spoons/. I keep a repository of unofficial Spoons at http://zzamboni.github.io/zzSpoons/, and others may be available by the time you read this.

repo指示如果需要,應(yīng)該從哪個存儲庫安裝Spoon。缺省為“Default”针饥,表示http://www.hammerspoon.org/Spoons/.的官方勺庫我在http://zzamboni.github.io/zzSpoons/厂抽,有一個非官方的勺子儲存庫,當(dāng)你讀到這篇文章的時候丁眼,可能已經(jīng)有其他的勺子了筷凤。

disable can be set to true to disable the Spoon (easier than commenting it out when you want to temporarily disable a spoon) in your configuration.

可以將DISABLE設(shè)置為TRUE以禁用配置中的勺子(當(dāng)您要臨時禁用勺子時,將其注釋掉比將其注釋掉更容易)苞七。

You can assign functions and modules to variables to improve readability of your code. For example, in my init.lua file I make the following assignment:

您可以將函數(shù)和模塊分配給變量藐守,以提高代碼的可讀性。例如蹂风,在我的init.lua文件中卢厂,我進(jìn)行了以下分配:

Install=spoon.SpoonInstall

Which allows me to write Install:andUse("MouseCircle",...), which is shorter and easier to read.

它允許我編寫install:and use(“MouseCircle”,.)硫眨,它更短足淆,更容易閱讀。

Managing repositories and spoons using SpoonInstall

使用SpoonInstall管理存儲庫和勺子

Apart from the andUse() “all-in-one” method, SpoonInstall has methods for specific repository- and spoon-maintenance operations. As of this writing, there are two Spoon repositories: the official one at http://www.hammerspoon.org/Spoons/, and my own at http://zzamboni.github.io/zzSpoons/, where I host some unofficial and in-progress Spoons.

除了anduse()“All-in-one”方法之外礁阁,SpoonInstall還具有用于特定存儲庫和勺子維護(hù)操作的方法巧号。在撰寫本文時,有兩個Spoon存儲庫:一個是http://www.hammerspoon.org/Spoons/姥闭,的官方存儲庫丹鸿,另一個是我自己的http://zzamboni.github.io/zzSpoons/,存儲庫棚品,我在那里托管一些非官方的和正在進(jìn)行的勺子靠欢。

The configuration variable used to specify repositories is SpoonInstall.repos. Its default value is the following, which configures the official repository identified as “default”:

用于指定存儲庫的配置變量是SpoonInstall.repos。其默認(rèn)值如下铜跑,配置標(biāo)識為默認(rèn)的官方存儲庫:

{ default = { url = "https://github.com/Hammerspoon/Spoons", desc = "Main Hammerspoon Spoon repository", } }

To configure a new repository, you can define an extra entry in this variable. The following code creates an entry named “zzspoons” for my Spoon repository:

要配置新的存儲庫门怪,您可以在此變量中定義額外的條目。下面的代碼為我的Spoon存儲庫創(chuàng)建了一個名為“zzspoons”的條目:

spoon.SpoonInstall.repos.zzspoons = { url = "https://github.com/zzamboni/zzSpoons", desc = "zzamboni's spoon repository", }

After this, both “zzspoons” and “default” can be used as values to the repo attribute in the andUse() method, and in any of the other methods that take a repository identifier as a parameter. You can find the full API documentation at http://www.hammerspoon.org/Spoons/SpoonInstall.html.

此后锅纺,“zzspoons”和“default”都可以用作anduse()方法中的repo屬性的值掷空,也可以在任何其他接受存儲庫標(biāo)識符作為參數(shù)的方法中使用。您可以在http://www.hammerspoon.org/Spoons/SpoonInstall.html.找到完整的api文檔囤锉。

5 Just enough Lua to be productive with Hammerspoon

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Flow control

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Dot-vs-colon method access in Lua

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Functions

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Tables

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Tables as namespaces

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Patterns

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

String manipulation

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Learning more Lua

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

6 DRAFT Exploring the Hammerspoon API

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Events and Hotkeys

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

hs.hotkey and hs.hotkey.modal

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

hs.keycodes and hs.keycodes.map

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

hs.eventtap and hs.eventtap.event

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

hs.mouse

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Window, Menus and Screen Manipulation

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

hs.screen

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

hs.window and other window-manipulation libraries

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Other modules

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

On-screen Drawing, Images and Alerts

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Application and Process Manipulation

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Sound and Music

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Networking and Web

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

System and Device Manipulation

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Data Processing and Utilities

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Hammerspoon itself

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

7 DRAFT Hammerspoon cookbook, tips and tricks

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Tip: be mindful of garbage collection

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Show Homebrew package info

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Tip: using asynchronous methods

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Transform URLs before opening them

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Other resources and configuration examples

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Spacehammer: a Spacemacs-like modal config

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

8 Writing your own extensions and Spoons

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Writing a new Spoon

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

The Spoon API

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Designing a new spoon: Leanpub

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Creating the skeleton for a new spoon

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Spoon metadata and configuration

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Spoon methods

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Testing your Spoon

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Packaging and sharing the new Spoon

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

What’s next?

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Writing a Hammerspoon extension in Lua

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

9 Using and extending Seal

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Using Seal

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Writing your own Seal plugins

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Seal plugin structure and API

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

The myactions Seal plugin

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Adding commands to your Seal plugin

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

Colophon

This content is not available in the sample book. The book can be purchased on Leanpub at http://leanpub.com/learning-hammerspoon.

本文由博客群發(fā)一文多發(fā)等運營工具平臺 OpenWrite 發(fā)布

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坦弟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子官地,更是在濱河造成了極大的恐慌晨雳,老刑警劉巖鳖谈,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铺韧,死亡現(xiàn)場離奇詭異,居然都是意外死亡氯析,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門可霎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來魄鸦,“玉大人宴杀,你說我怎么就攤上這事癣朗。” “怎么了旺罢?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵旷余,是天一觀的道長。 經(jīng)常有香客問我扁达,道長正卧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任跪解,我火速辦了婚禮炉旷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘叉讥。我一直安慰自己窘行,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布图仓。 她就那樣靜靜地躺著罐盔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪救崔。 梳的紋絲不亂的頭發(fā)上惶看,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天,我揣著相機與錄音六孵,去河邊找鬼纬黎。 笑死,一個胖子當(dāng)著我的面吹牛劫窒,可吹牛的內(nèi)容都是我干的本今。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼烛亦,長吁一口氣:“原來是場噩夢啊……” “哼诈泼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起煤禽,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤铐达,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后檬果,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瓮孙,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡唐断,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了杭抠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脸甘。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖偏灿,靈堂內(nèi)的尸體忽然破棺而出丹诀,到底是詐尸還是另有隱情,我是刑警寧澤翁垂,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布铆遭,位于F島的核電站,受9級特大地震影響沿猜,放射性物質(zhì)發(fā)生泄漏枚荣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一啼肩、第九天 我趴在偏房一處隱蔽的房頂上張望橄妆。 院中可真熱鬧,春花似錦祈坠、人聲如沸害碾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蛮原。三九已至,卻和暖如春另绩,著一層夾襖步出監(jiān)牢的瞬間儒陨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工笋籽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蹦漠,地道東北人。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓车海,卻偏偏與公主長得像笛园,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子侍芝,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容