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

2,994 阅读3分钟

关于文章

本文章为原创内容,首发于我的个人博客语雀,转载请注明,谢谢。

文章作者:1874

个人博客文章链接:程序员必备——Mac效率工具Alfred Workflows配置

语雀文章链接:程序员必备——Mac效率工具Alfred Workflows配置

引言


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

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

最近一琢磨,可以利用AflfedWorkflows功能去尝试一下,于是真被我搞出来了,这里记录一下。

开始

Open in WebStorm

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

运行流程

设置

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

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

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

Basic Setup基础设置。

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

Scope搜索范围

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

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

  1. 左键单击并选择添加Actions-Run Script,配置如下:

  • Language: /bin/zsh终端,也可以选择/bin/bash终端
  • with input as {query}: true
  • running instances: Sequentially
  • Script: /usr/local/bin/wstorm "{query}"

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

Open in iTerm

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

运行流程

设置

  1. 新建一个Blank workflow,配置如下:

  1. 新增File Filter,配置如下:

  1. 左键单击并选择添加Actions-Run NSAppleScript,配置如下:

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

大功告成