AppleScirpt设置打开Terminal快捷键

前几天学习了下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键,就设置上了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值