AppleScirpt设置打开Terminal快捷键

来源:互联网 发布:minecraft java版 编辑:程序博客网 时间:2024/04/30 22:42

前几天学习了下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-SW2try    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 tellon error set currFolder to (path to home folder as string)end trytell application "Terminal"    if it is running then        set itermisrunning to true    else        set itermisrunning to false    end ifactivate -- 文档解释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 ifdo script ("cd " & "'" & currFolder & "'") in front windowend tellend 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键,就设置上了

0 0