12
头图

Alfred is a very powerful artifact that can effectively improve the efficiency of your Mac computer. It can be said that with Alfred, you can basically achieve various operations without the mouse. Compared with the focus search that comes with Mac, it can be called the advantage of rolling.

The picture below is the Alfred icon, the official website is: https://www.alfredapp.com/

Before introducing its use, let's take a look at its basic functions.

basic

First use the shortcut key Alt + Space to open the Alfred interface.

Alfred's common basic functions are querying documents, specifying website search, clipboard history, integrating iTerm2, computer dictionary translation, integrating 1password, system functions, magnifying display content, etc. . Next, I will select a few of them for a simple display.

Query document

Document query operations can be performed through the following four shortcuts:\

  • open: open the file
  • find: open the document directory
  • in: search in file
  • tags: specify file tags

The following figure is an example of the use of the find command.

Specify the website to search

Alfred can specify search engine keywords to simplify the search method.

Taking custom Baidu as the search engine as an example, if we want to use bd as the keyword of Baidu search engine, then we can configure as follows:

After completing the configuration, you can use the bd keyword to specify Baidu as the search engine.

Clipboard History

We can set the length of time the file is saved, activate the shortcut key of the clipboard, or activate it directly with clipboard, and use clear to clear the clipboard.

Integrated iTerm2

As the best command-line tool for Mac, iTerm2, Alfred also owns it.

We can set custom commands on it, for example:

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

效果展示

Entering ls -al and pressing enter will automatically execute the command in iTerm2.

uses workflow

After understanding the basic functions, the key point is to return to the workflow of Alfred. The official interface documentation is provided for users to call. \

Interface documentation: https://www.deanishe.net/alfred-workflow/api/index.html

The above picture is a schematic diagram of Alfred's workflow. We open the project folder by using the code command to select pycharm or vscode according to the project directory. Let's take a look at Alfred's workflow in this example.

First add a workflow and specify the Name as code.

Then set the project directory public variable.

Right-click to add a script filter script.

If you need to add a new script file, you can right-click Open in Finder to open the directory where the workflow is located, and download the latest version of Alfred-Workflow from GitHub ( https://github.com/deanishe/alfred-workflow/releases/latest ) , unzip and copy the workflow directory in it to the open workflow directory.

Create a new index.py file with the following code:

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))

Back in the Alfred workflow, add the pass parameter variable.

Add list selection, add user selection list.

Add conditional judgment to identify which software the user selects.

Then add two branches Open File operation, and use the corresponding program to open the file.

Finally, after completing all the settings, open the Alfred pop-up box, enter the code + project directory, and start your journey of using Alfred.

Recommended reading

## Golang common design pattern option pattern

## What is the way the final boss of "Reverse Game" hides himself?


云叔_又拍云
5.9k 声望4.6k 粉丝

又拍云是专注CDN、云存储、小程序开发方案、 短视频开发方案、DDoS高防等产品的国内知名企业级云服务商。