前几天学习了下appleScirpt,用它写了个在当前文件夹打开Terminal的小程序,配置成service方便平时使用.
基本思路
1.编写脚本:appleScirpt获取当前文件夹路径.启动Termianl,cd到当前文件夹路径
2.在Automator中设置service,调用脚本
3.在快捷键设置中增加该service的快捷键
编写脚本
```
on run -- 当程序开始运行https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_handlers.html#//apple_ref/doc/uid/TP40000983-CH7g-SW2
try
tell application "Finder"
(*
POSIX path是类似这种 /usr/Documents
http://www.satimage.fr/software/en/smile/external_codes/file_paths.html
*)
set currFolder to POSIX path of (folder of the front window as string)
end tell
on error
set currFolder to (path to home folder as string)
end try
tell application "Terminal"
if it is running then
set itermisrunning to true
else
set itermisrunning to false
end if
activate -- 文档解释Brings an application to the front, and opens it if it is on the local computer and not already running.
if itermisrunning = true then
tell application "System Events" to keystroke "t" using command down
end if
do script ("cd " & "'" & currFolder & "'") in front window
end tell
end ru
```
Terminal支持的applescriptAPI查询:
Scirpt Editor -> File -> Open Directionary -> Terminal -> Choose
添加service
打开Automator,按下图找到run applescript,然后双击.这个操作是创建调用applescript的service.
如下图,将代码复制到输入框,Service receives为 no input
保存command + s,名称命名为open
这样一个service就建成了
在快捷键设置中增加该service的快捷键
打开快捷键设置 System Preferences -> Keyboard -> Short cuts -> Services -> 选中 openTerminal
然后选中右边,设置快捷键.快捷键如果设置为按住control + o,则按住control键,再按住o键,就设置上了