关于效率工具:finder-快捷脚本one-click-在当前目录执行命令

7次阅读

共计 2677 个字符,预计需要花费 7 分钟才能阅读完成。

先看成果

【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 关上当前目录的 server
    mtp
    
    # 防止敞开本脚本
    sleep 1000000

【serve.sh】一键启动动态服务器,并加载当前目录

  • 依赖全局 anywherenpm 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 关上当前目录的 server
    anywhere -p $random -d "$thePath"

就挺有意思

正文完
 0