skip to content
1874

程序员必备——Mac效率工具Alfred Workflows配置

/ 5 min read

最后更新:

引言

最近发现Alfred简直不要太好用,快速打开网址功能(Web Search)在工作中使用起来行云流水,再也不用去一堆标签中寻找常用的网址。

Fv_hSWNtWTOroC6OS4YOH23qjov0.png

但是在项目开发过程中,经常需要打开或运行各种项目,所以需要经常使iTerm或者IDE进入到各种项目文件夹中。在VSCode中,大家经常会使用code命令打开项目,再配合环境变量的别名设置,使用起来很方便。但是!我是忠诚的WebStorm党派,每次打开项目都得先打开应用然后找到需要打开的项目(不过后来发现,WebStorm其实也有命令行启动器,效果也一样)。但是这么做也有一个痛点,就是需要事先配置环境变量的别名才能做到快速打开。 最近一琢磨,可以利用AflfedWorkflows功能去尝试一下,于是真被我搞出来了,这里记录一下。

Open in WebStorm

WebStrom打开项目。通过code命令打开指定目录下的文件夹,快速打开项目。效果如下:

Untitled.gif

运行流程

Fi1CSGPgbhxcEmVJsWkhTRKofLx0.png

设置

  1. 右下角新建一个Blank Workflow,配置如下:

注意Bundle id必须是唯一的才行。

FkbsFFLjrHAiiyFhMyAnsYxAG1cu.png
  1. 右键新增一个File Filter,配置如下:
FtNswxM5ZqzKXFXYXRPMO5gdQMc4.png

Basic Setup基础设置。

  • Keyword: code
  • with space: true
  • Placeholder Title: Open in WebStorm
  • Placeholder Subtext: 请继续输入以打开项目
  • File Types: 文件类型,随便拖一个文件夹进去表示只识别文件夹,过滤掉文件
Fteyhe06uV8m5mE7wZoNN6IJ-GE1.png

Scope搜索范围

  • Search Scope: 指定搜索范围,将目录拖拽进去即可
FgE6bMn1YgtGzP1bkxhMhdRUvGn6.png

FieldsLimit and Sort保持不变,可以根据自己的习惯修改

  1. 左键单击并选择添加Actions-Run Script,配置如下:
FusgOROVeGtM5HvHHey1GxHOKdxL.png
  • Language: /bin/zsh 终端,也可以选择/bin/bash 终端
  • with input as {query}: true
  • running instances: Sequentially
  • Script: /usr/local/bin/wstorm “{query}”
Fj3KjVMT_TxIJCWRGGZdcplCQViA.png

注意:使用/usr/local/bin/wstorm命令需要在WebStorm中开启工具-创建命令行启动器,配置脚本位置为/usr/local/bin,并配置脚本命令为wstorm

FiyIJ9N-DdDJK4NvGJwCwO_EqjeW.png

Open in iTerm

iTerm中打开文件夹。通过cd命令进入指定目录下的文件夹,快速打开项目并运行。效果如下:

ltjnpxJnEnmaDsxBBb2Q3M11U_tJ.gif

运行流程

FlehoM7bdsPdu33pG4_DfRDM06wl.png

设置

  1. 新建一个Blank workflow,配置如下:
FoiKddQ7QD7GegR1SoQ3Em0xGpwT.png
  1. 新增File Filter,配置如下:
Fltm6hEMqabbF4xluHh9ph0HYpoG.png
  1. 左键单击并选择添加 Actions-Run NSAppleScript,配置如下:
FoE5xUh8YN6K2pZSd6a976xz5KF_.png
-- For the latest version:
-- https://github.com/vitorgalvao/custom-alfred-iterm-scripts
-- Set this property to true to always open in a new window
property open_in_new_window : false
-- Set this property to false to reuse current tab
property open_in_new_tab : true
-- Handlers
on new_window()
tell application "iTerm" to create window with default profile
end new_window
on new_tab()
tell application "iTerm" to tell the first window to create tab with default profile
end new_tab
on call_forward()
tell application "iTerm" to activate
end call_forward
on is_running()
application "iTerm" is running
end is_running
on has_windows()
if not is_running() then return false
if windows of application "iTerm" is {} then return false
true
end has_windows
on send_text(custom_text)
tell application "iTerm" to tell the first window to tell current session to write text custom_text
end send_text
-- Main
on alfred_script(query)
if has_windows() then
if open_in_new_window then
new_window()
else if open_in_new_tab then
new_tab()
else
-- Reuse current tab
end if
else
-- If iTerm is not running and we tell it to create a new window, we get two
-- One from opening the application, and the other from the command
if is_running() then
new_window()
else
call_forward()
end if
end if
-- Make sure a window exists before we continue, or the write may fail
repeat until has_windows()
delay 0.01
end repeat
send_text(query)
call_forward()
end alfred_script