先看成果
【cdto】一键关上终端,并切换到当前目录
【code】一键关上 vscode,并加载当前目录
【serve】一键启动动态服务器,并加载当前目录
【mtp】一键视觉无损、原地、递归压缩、当前目录所有图片
【ecs】一键登录服务器
【技巧一】将利用搁置到 finder
- 固定:按住 command,拖拽脚本到 finder 顶部工具栏,固定快捷脚本
- 勾销固定:按住 command,将脚本拖离 finder 工具栏,勾销该固定
- 任意文件夹,利用,脚本都能够固定在 finder 上快捷拜访
【技巧二】自定义 icon
- 先复制想要的 icon 图片
- 查看文件详情简介,左上角 icon 可点击选中
- 选中后,command + v 即可将图片设置为该文件的 icon
- 选中后,按下 delete 可将自定义 icon 删除,复原默认 icon
- 任意文件夹,利用,文件均可设置自定义 icon
【技巧三】获取 finder 当前目录门路
#!/bin/bash# 应用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//\'/}
其中要害代码为osascript -e 'tell application "Finder" to set thePath to (quoted form of POSIX path of (target of front window as alias)
- 这段代码应用 osascript 命令调用 AppleScript 脚本来获取 macOS Finder 以后关上窗口所在的文件夹门路。
- -e 选项用于指定要执行的脚本内容。在脚本内容中,应用tell application "Finder"通知以后正在运行的应用程序为Finder,并且要在其上下文环境中执行前面的操作。
- set thePath to 来创立一个名为 thePath 的变量,并将其赋值为“(target of front window as alias)”(即以后激活窗口的门路)。
- 最初,应用(quoted form of POSIX path of ...)将门路进行引号本义和POSIX门路格式化,以确保能够正确处理蕴含空格等特殊字符的门路。
示例脚步源码
【cdto.sh】一键关上终端,并切换到当前目录
十行代码实现 cdto
#!/bin/bash# 应用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//\'/}# 定义信号处理函数,当 iterm 执行失败,则应用默认终端function signal_handler() {# 切换到当前目录cd "$thePath"bash -i}# 注册信号处理函数trap signal_handler ERR INT TERM# 尝试关上 iTerm 终端if ! open -a iTerm "$thePath"; then# 如果关上失败,则手动触发谬误信号echo "Failed to open iTerm"kill -s ERR $$fi
【code.sh】一键关上 vscode,并加载当前目录
需首先装置 vscode code 脚本
#!/bin/bash# 应用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 快捷关上以后文件目录code "$thePath"
【ecs.sh】一键登录服务器
#! /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
【mtp.sh】一键视觉无损、原地、递归压缩、当前目录所有图片
依赖全局 mtp 脚本
npm install -g tinypng-script-with-cache
#!/bin/bash# 应用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//\'/}# 切换到目录cd "$thePath"# 应用 anywhere 关上当前目录的 servermtp# 防止敞开本脚本sleep 1000000
【serve.sh】一键启动动态服务器,并加载当前目录
- 依赖全局 anywhere
npm install -g anywhere
不想装置 anywhere 的话,间接应用 python 性能即可,应用这个脚本
#!/bin/bash# 应用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 关上当前目录的 serveranywhere -p $random -d "$thePath"