【程序員的實用工具推薦】 Mac 效率神器 Alfred


Alfred 是一款功能非常強大,能有效提升 Mac 電腦使用效率的神器。可以說有了 Alfred 你就基本上可以脫離鼠標實現各種操作。相比 Mac 自帶的聚焦搜索,完全可以稱得上擁有碾壓性的優勢。

下圖是 Alfred 圖標, 官網為:https://www.alfredapp.com/

在介紹它的使用前,我們先來了解一下它的基本功能。

基本功能介紹

首先使用快捷鍵 Alt + 空格 打開 Alfred 操作界面。

Alfred 的常用基礎功能為查詢文檔、指定網站搜索、剪切板歷史、集成 iTerm2、計算機字典翻譯、集成 1password、系統功能、放大顯示內容等等 接下來挑選其中幾個為大家做簡單展示。

查詢文檔

可以通過以下四種快捷方式進行文檔查詢操作:\

  • open: 打開文件
  • find: 打開文檔目錄
  • in: 在文件中搜索
  • tags: 指定文件標簽

下圖是 find 命令的使用示例。

指定網站進行搜索

Alfred 可以指定搜索引擎關鍵詞,簡化搜索方式。

以自定義百度為搜索引擎為例,如果我們要讓 bd 作為百度搜索引擎的關鍵詞,那我們可以進行如下配置:

完成配置后就可以使用 bd 關鍵字指定百度作為搜索引擎了。

剪貼板歷史

我們可以設置文件保存的時長、激活剪切板的快捷鍵、或者直接使用 clipboard 激活、使用 clear 清除剪切板。

集成 iTerm2

作為 Mac 最好用的命令行工具 iTerm2,Alfred 也是擁有的。

我們可以對它設置自定義命令,例如:

on alfred_script(q)
 if application "iTerm2" is running or application "iTerm" is running then
  run script "
   on run {q}
    tell application \"iTerm\"
     activate
     try
      select first window
      set onlywindow to true
     on error
      create window with default profile
      select first window
      set onlywindow to true
     end try
     tell the first window
      if onlywindow is false then
       create tab with default profile
      end if
      tell current session to write text q
     end tell
    end tell
   end run
  " with parameters {q}
 else
  run script "
   on run {q}
    tell application \"iTerm\"
     activate
     try
      select first window
     on error
      create window with default profile
      select first window
     end try
     tell the first window
      tell current session to write text q
     end tell
    end tell
   end run
  " with parameters {q}
 end if
end alfred_script

效果展示

輸入 ls -al 回車會將命令自動在 iTerm2 中執行。

使用工作流程

了解基本功能后,重點還是回歸到 Alfred 的工作流程,官方提供了接口文檔方便用戶進行調用。\

接口文檔:https://www.deanishe.net/alfred-workflow/api/index.html

上圖是 Alfred 的工作流程示意圖,我們通過使用 code 命令根據項目目錄選擇 pycharm 或 vscode 打開項目文件夾,這一例子來看一下 Alfred 的工作流程。

首先新增一個工作流,指定 Name 為 code。

然后設置項目目錄公共變量。

右鍵新增一個 script filter 腳本。

如果需要新增腳本文件,可以右鍵點擊 Open in Finder 打開該工作流所在目錄,從 GitHub 下載最新版本的 Alfred-Workflow(https://github.com/deanishe/alfred-workflow/releases/latest),解壓並將其中的 workflow 目錄復制到打開的這個工作流目錄中。

新建 index.py 文件,代碼如下:

import sys
import os
from os import listdir
from os.path import isdir, join, expanduser

from workflow import Workflow3, web, ICON_WEB


# 獲取文件列表
def getFileList():
  args_list = wf.args
  result = []
  for path in args_list[1:]:
    # path = wf.args[1]
    log.debug('path: ' + path)
    path = expanduser(path)
    result.extend([{"file": f, "path": path} for f in listdir(path) if isdir(join(path, f))])
  return result

def main(wf):
  # 獲取搜索參數
  searchKey = wf.args[0]
  log.debug('searchKey: ' + searchKey)
  # 文件列表緩存 3s
  fileList = wf.cached_data('projects', getFileList, max_age=3)
  # 根據 query 過濾目錄
  for item in fileList:
    if (searchKey and (searchKey in item.get('file'))):
      title = item.get('file')
      wf.add_item(title=title, subtitle=item.get('path'), arg=os.path.join(item.get('path'), title), valid=True)

  # 把應該展示的內容發送給 Alfred
  wf.send_feedback()

if __name__ == '__main__':
  wf = Workflow3()
  log = wf.logger
  sys.exit(wf.run(main))

回到 Alfred 工作流,添加傳遞參數變量。

新增列表選擇,添加用戶選擇列表。

添加條件判斷,辨別用戶選擇的軟件是哪一個。

然后新增兩個分支 Open File 操作,並使用相應程序打開文件。

最后在完成全部設置后打開 Alfred 彈框,輸入 code + 項目目錄,開始你的 Alfred 使用之旅吧。

推薦閱讀

## Golang 常見設計模式之選項模式

## 《逆局》最終 boss 隱藏自己的方式是?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM