[Mac技巧] Finder当前目录下打开Terminal、创建TXT

3,276 阅读3分钟

需求

作为一个用 Mac OS X 作为开发环境的程序员,经常会有在当前 Finder 路径下:

  • 直接打开 Terminal
  • 创建 txt

的需求。但 Mac 下操作起来比较麻烦。

解决

自己动手,在 Finder 工具栏定义了两个快捷方式。先来看看最终效果:

TL;DR 太长不看

上传了这两个 automator app 到百度网盘:

pan.baidu.com/s/14IFSiYGc…

下载后可以按住 command,拖动到 Finder 的工具栏中。

Automator(自动操作)

Automator 是 Mac OS X 自带的一个 app ,可以帮你自动完成一些批量任务。有点类似于 iOS 12 的“捷径”。

Apple Script 是 Mac 内置的一种功能强大的脚本语言。它可以实现 app 间的消息传递,还可以获取各种上下文信息。

而上面的效果,就是用 Automator 配合 Apple Script 实现的。它们更多强大的功能可以自行搜索。

可以在 Spotlight 或者 LaunchPad 中搜索 “auto” 找到它。

自动操作

创建 Automator 应用程序

我们打开 Automator,新建一个应用程序。

搜索 AppleScript,然后把“运行 AppleScript”拖入右边的窗口中。
这样我们就创建了一个运行 AppleScript 的 app 。

当前 Finder 窗口新建 txt

复制下面的脚本,到脚本编辑栏:

on run {input, parameters}
	tell application "Finder"
		set newFile to make new file at (the target of the front window) as alias
		open newFile
	end tell
end run

复制后如下图:

然后保存,起一个你喜欢的名字。这里我命名为 New Text File.app。

当前 Finder 窗口打开终端

重复上面的步骤,新建一个 app 运行如下脚本:

on run {input, parameters}
    tell application "Finder"
        set myWin to window 1
		set thePath to (quoted form of POSIX path of (target of myWin as alias))
		if application "Terminal" is running then
			tell application "Terminal"
				do script "cd " & thePath
				activate
			end tell
		else
			tell application "Terminal"
				do script "cd " & thePath in window 1
				activate
			end tell
		end if
	end tell
end run

然后保存。这里我命名为 Open Termimal.app

Finder 工具栏快捷方式的创建

我们可以把刚刚创建的 app ,拖动到 Finder 的工具栏中。方法是按住 Command 并拖动。

至此,我们已经完成了所需的功能。点击这两个图标,就可以实现“当前目录打开终端”和“当前目录创建 txt”。

美化图标

现在功能都实现了,想给我们的 app 换一个图标。

当然可以通过右键“显示包内容”来替换资源文件实现,但是这里介绍一种简单的方法。

我们知道右键“显示简介”(快捷键 command + i)可以查看文件信息。其实左上角那个“图标”,是可以选中并复制的!

如图,就是这个图标,点击可以选中,选中状态会显示蓝色边框。

选中后我们就可以复制、粘贴了。分别选中“终端”和“文本编辑”的图标,command + c 复制,然后选中我们创建的 app 图标,command + v粘贴。大工告成!

修改后:

用这个方法,也可以给其他 app 定义自己喜欢的图标。