【程序员的实用工具推荐】 Mac 效率神器 Alfred
Alfred 是一款功能非常強大,能有效提升 Mac 電腦使用效率的神器??梢哉f有了 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 thenrun script "on run {q}tell application \"iTerm\"activatetryselect first windowset onlywindow to trueon errorcreate window with default profileselect first windowset onlywindow to trueend trytell the first windowif onlywindow is false thencreate tab with default profileend iftell current session to write text qend tellend tellend run" with parameters {q}elserun script "on run {q}tell application \"iTerm\"activatetryselect first windowon errorcreate window with default profileselect first windowend trytell the first windowtell current session to write text qend tellend tellend 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, expanduserfrom workflow import Workflow3, web, ICON_WEB# 獲取文件列表 def getFileList():args_list = wf.argsresult = []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 resultdef main(wf):# 獲取搜索參數searchKey = wf.args[0]log.debug('searchKey: ' + searchKey)# 文件列表緩存 3sfileList = 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)# 把應該展示的內容發送給 Alfredwf.send_feedback()if __name__ == '__main__':wf = Workflow3()log = wf.loggersys.exit(wf.run(main))回到 Alfred 工作流,添加傳遞參數變量。
新增列表選擇,添加用戶選擇列表。
添加條件判斷,辨別用戶選擇的軟件是哪一個。
然后新增兩個分支 Open File 操作,并使用相應程序打開文件。
最后在完成全部設置后打開 Alfred 彈框,輸入 code + 項目目錄,開始你的 Alfred 使用之旅吧。
總結
以上是生活随笔為你收集整理的【程序员的实用工具推荐】 Mac 效率神器 Alfred的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机视觉 专业术语,计算机视觉常用术语
- 下一篇: ida “ failed to load