前言
Mac平台默认启动终端的方式有些繁琐
为了减少大脑压力 简化实现流程 我们可以使用自定义脚本的方式 快速在当前目录下启动某个应用
但是单纯使用sh脚本话 依然有些繁琐 因为免不了需要手动输入执行指令 不过好在Mac
中Finder
给我们提供的添加自定义菜单的功能:
我们只需将编写好的.sh
脚本文件拖入到窗口菜单面板中 然后默认使用第三方终端打开脚本 那么当我们点击图标的时候 就能快速执行脚本内容了
至于如何将脚本文件拖入到面板中 只需按住command
键即可 如果你想移除它 也是同样的操作
下面列出了一些常用的脚本 大家可以依葫芦画瓢 自行发挥
脚本
一键打开终端,并切换到当前目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 获取当前打开的目录
thePath=$(osascript -e 'tell application "Finder" to set thePath to POSIX path of (target of front window as alias)')
# 去除路径中的引号
thePath=$(sed "s/\'//g" <<< "$thePath")
# 尝试打开 Terminal 终端
open -a Terminal "$thePath" && sleep 2 && osascript -e 'tell application "Terminal" to close (first window whose name contains "terminal.command")'
# 如果打开失败,则切换到当前目录并启动默认终端
cd "$thePath"
bash
一键打开 vscode,并加载当前目录
1
2
3
4
5
6
7
8
9
10
11
12
13
# 使用AppleScript获取当前打开的目录
thePath=$(osascript -e 'tell application "Finder" to set thePath to (quoted form of POSIX path of (target of front window as alias))')
# 去除路径中的引号
thePath=${thePath//\'/}
# 输出获取到的目录路径
echo $thePath
# 启动 vs,并加载所在目录
open -a "Visual Studio Code" "$thePath"一键登录服务器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#! /usr/bin/expect
# 依赖 expect 命令,https://www.linuxprobe.com/linux-expect-auto.html
# 启动 ssh 进程
spawn /usr/bin/ssh root@这里替换ip
# 匹配标准输出中的字符串
expect "root@这里替换ip's password"
# 向标准输入填充密码并换行
send "这里替换密码\r"
# 匹配标准输出中的字符串
expect "to Alibaba Cloud"
# 脱离控制,将控制权交还给用户,允许用户交互
interact一键启动静态服务器,并加载当前目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 使用AppleScript获取当前打开的目录
thePath=$(osascript -e 'tell application "Finder" to set thePath to (quoted form of POSIX path of (target of front window as alias))')
# 去除路径中的引号
thePath=${thePath//\'/}
# 随机端口
random=$((RANDOM%10000+20000))
# 打印运行的路径
echo "http://127.0.0.1:$random"
echo $thePath
# 使用 anywhere 打开当前目录的 server
python3 -mhttp.server $random -d "$thePath"创建指定的目录结构
比如这种:
1
2
3
4
5new_directory
├── outputs
├── sounds
├── subtitles
└── videos实现指令如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
# 获取当前打开的目录
thePath=$(osascript -e 'tell application "Finder" to set thePath to POSIX path of (target of front window as alias)')
# 去除路径中的引号
thePath=$(sed "s/\'//g" <<< "$thePath")
# 创建一个新的目录
mkdir -p "$thePath/new_directory"
# 在新目录下创建多个子目录
mkdir -p "$thePath/new_directory"/{sounds,videos,subtitles,outputs}
关于应用图标的更换
如果我们想给脚本添加上对应的图标 比如将启动vscode
的脚本换成vscode
应用的图标 那么我们可以:
- 选中
vscode
应用右键拷贝 (不是复制) - 选中脚本文件 右键打开显示简介
- 在显示简介窗口中 点击图标栏 然后
command+v
粘贴即可
相关问题
如果你想使用
Mac
自带的终端启动该脚本 只需将后缀.sh
改成.command
即可如果出现权限问题 需要手动修改权限
执行以下命令修改脚本访问权限:
1
chmod 777 xxx.command
当应用启动后 终端窗口未关闭
这种情况 我们需要去终端偏好设置中进行相关设置 将关闭是否询问改为永不 当
shell
退出窗口时关闭终端:
本文为作者原创 转载时请注明出处 谢谢