打开节点_Mac小技巧-任意目录节点下快速打开iTerm、Terminal

8e54190f8d2d71f55bd3a84be7d460b1.png

使用Mac的用户有时候会遇到需要当前目录下打开Terminal的情况,例如需要对某一级目录下的文件进行授权、执行等操作,一般的做法是先复制该目录的路径,然后打开Terminal,最后在Terminal中执行cd命令,网上也有一些办法可以在Finder工具栏添加打开Terminal的方式,但是操作起来都不是很方便,因此特地开发出该Alfredworkflow,目前已能实现在任意目录一键打开Terminal并自动执行cd 命令,基本原理就是利用AppleScript获取到当前文件/文件夹所在的Finder目录,唤醒Terminal或者iTerm并执行cd命令。

2、准备工作

脚本为Alfredworkflow,使用者需要自行进行安装Alfred,安装后直接导入即可。

3、脚本介绍

3.1、iTerm脚本

on alfred_script(q)tell application "Finder"set pathFile to selection as textif pathFile is "" then set pathFile to (path to desktop folder from user domain)set pathFile to get POSIX path of pathFile--防止目录存在空格跳转不了set pathFile to quoted form of pathFileset realPath to pathFileif pathFile is "/" then return realPath--如果是文件夹路径直接返回if pathFile ends with "/" then set pathFile to (characters 1 thru -2 of pathFile) as string--分割set tid to AppleScript's text item delimitersset AppleScript's text item delimiters to "/"set allComponents to every text item of pathFileset AppleScript's text item delimiters to ""--判断是否为空目录if (count allComponents) is 1 then set realPath to ""set allComponents to (items 1 thru -2) of allComponentsset AppleScript's text item delimiters to "/"set newPath to allComponents as textset AppleScript's text item delimiters to ""if newPath does not end with "/" then set newPath to (newPath & "/") as stringset AppleScript's text item delimiters to tidset realPath to newPathset realPath to "cd " & realPath & "'"log realPath--set the clipboard to pathFile--set realPath to realPath & "'"--set the clipboard to pathFiletell application "iTerm"create window with default profiletell current session of current windowwrite text realPathend tellend tellend tellend alfred_script

3.2、Terminal脚本

on alfred_script(q)tell application "Finder"set pathFile to selection as textif pathFile is "" then set pathFile to (path to desktop folder from user domain)set pathFile to get POSIX path of pathFile--防止目录存在空格跳转不了set pathFile to quoted form of pathFileset realPath to pathFileif pathFile is "/" then return realPath--如果是文件夹路径直接返回if pathFile ends with "/" then set pathFile to (characters 1 thru -2 of pathFile) as string--分割set tid to AppleScript's text item delimitersset AppleScript's text item delimiters to "/"set allComponents to every text item of pathFileset AppleScript's text item delimiters to ""--判断是否为空目录if (count allComponents) is 1 then set realPath to ""set allComponents to (items 1 thru -2) of allComponentsset AppleScript's text item delimiters to "/"set newPath to allComponents as textset AppleScript's text item delimiters to ""if newPath does not end with "/" then set newPath to (newPath & "/") as stringset AppleScript's text item delimiters to tidset realPath to newPathset realPath to "cd " & realPathlog realPath--set the clipboard to pathFileset realPath to realPath & "'"tell application "Terminal"do script (realPath)activateend tellend tellend alfred_script

4、总结

目前该项目在GitHub,有需要的可以移步我的博客http://codersoft.gitee.io 或者点击阅读原文进行查看、下载脚本,感谢您的关注!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值